cancel
Showing results for 
Search instead for 
Did you mean: 

How to bind table data to Odata without selecting row index

venkatachala_ck
Active Participant
0 Kudos

Hi Experts,

I want send table data to backend(ODATA) without selecting index.

ex. if have 100 records it should bind at time

i gone through this link

How to send table data to Odata | SCN

here they are sending invidual values and hard coded the data.

i don,t know how to get table data and push it to ODATAModel on click of Button.

Example without using table data:

function batchTest(){   

    var sServiceUrl = "services/employee.xsodata"; 

     

    var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true); 

     

    var oEmp1 = { 

                "EmployeeId": "0000000014", 

                "FirstName": "Laura", 

                "LastName": "Hahn" 

    }; 

    var oEmp2 = { 

                "EmployeeId": "0000000015", 

                "FirstName": "Matthias", 

                "LastName": "Müller" 

    }; 

     

    var batchChanges = [];   

     

    batchChanges.push( oModel.createBatchOperation("Employees", "POST", oEmp1) );   

    batchChanges.push( oModel.createBatchOperation("Employees", "POST", oEmp2) );   

     

    oModel.addBatchChangeOperations(batchChanges);  

     

    oModel.submitBatch(function(data) {   

        oModel.refresh();   

       

        if (data.__batchResponses[0].__changeResponses) { 

            alert("Inserted " + data.__batchResponses[0].__changeResponses.length + " Employee(s)");   

        } else { 

            alert(data.__batchResponses[0].message);   

        } 

       

    }, function(err) {   

      alert("Error occurred", err);   

    });   

var oButton = new sap.m.Button({ 

        text : "Batch Input", 

        tooltip : "trigger batch input", 

        press : batchTest 

}); 

Thanks & Regards,

Venkat

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

With the getItems() method you get an array of all the rows in your table.

e.g. var allItems = oTable.getItems();

var firstItem = allItems[0];

Now if you want the values of a row you can use the getCells() method.

e.g.

var firstColumnOfFirstRow = firstItem.getCells()[0];

var oEmp1 = { 

                "EmployeeId": firstItem.getCells()[0], 

                "FirstName": firstItem.getCells()[1], 

                "LastName": firstItem.getCells()[2] 

    }; 

If you want to get all row data, use a for loop:

JavaScript for Loop

Kind regards,

RW

venkatachala_ck
Active Participant
0 Kudos

Hi Robbe,

thanks for your response, what you mentioned for reading table is right

but if use getCells()[]   like that how many objects i have to create

and push it.

can you tel me how  to push this using single Object.

i mean if have 100 records in table.

thanks,

venkat

Former Member
0 Kudos

I'm sorry but i don't understand what you mean.

You can use a loop to go through all the items, and just add the entity to your batch array:

batchChanges.push( oModel.createBatchOperation("Employees", "POST", oEmp1) ); 

venkatachala_ck
Active Participant
0 Kudos

In the snippet

var oEmp1 = {

                "EmployeeId": firstItem.getCells()[0],

                "FirstName": firstItem.getCells()[1],

                "LastName": firstItem.getCells()[2]

    };

you have created object (oEmp1 ) and pushing it.


what i meant is your object is only used for first row data so

if i want second row data then i have to create one more object  like oEmp2

and push it again?

like that if  have 100 records in table is there any necessary to create that much objects and push?

for that purpose i'm asking you  "how  to push complete table data using single Object."

Former Member
0 Kudos

And that's why you have to use a for loop

for (i = 0; i < allItems.length; i++) {

   var item = allItems[i];

var oEmp = {

                "EmployeeId": item.getCells()[0],

                "FirstName": item.getCells()[1],

                "LastName": item.getCells()[2]

    };

batchChanges.push( oModel.createBatchOperation("Employees", "POST", oEmp) );

}

There's probably another way to do the same, but this is how I do it

Qualiture
Active Contributor
0 Kudos

Personally, I would never ever use getCells()... Rather use the model property directly:


var aTableData = this.byId("myTable").getModel().getData();

for (i = 0; i < aTableData.length; i++) {

    var oEmp = {

        "EmployeeId" : aTableData[0].EmployeeId,

        "FirstName"  : aTableData[0].FirstName,

        "LastName"   : aTableData[0].LastName

    };

    batchChanges.push(oModel.createBatchOperation("Employees", "POST", oEmp));

}

(I assumed your table data uses the same property names as the POST data)

Answers (1)

Answers (1)

venkatachala_ck
Active Participant
0 Kudos

Thank you guys