namita kalita
namita kalita

Reputation: 79

how to push data to existing sapui5 model

I have a Json Model in sapui5 as - //console.log(dModel);

enter image description here

My new data response is as follows - //console.log(response);

enter image description here

Now I want to push new data(only the data part) to the existing model, inside /modelData/data.

code I am trying -

sap.ui.getCore().getModel().getProperty("/modelData/data").push(response.data);

This code is pushing the data but as -

enter image description here

After 19(old values) it is pushing all the objects inside 20th as 0, 1, 2... The Ideal way should be after 19 I should get 20, 21, 22 and so on.

What changes I need to make to get this, Thank you ... please suggest.

Upvotes: 2

Views: 31416

Answers (2)

Qualiture
Qualiture

Reputation: 4920

If you need to add new items to your model, you should use it like this:

var oModel = sap.ui.getCore().getModel();
var aData  = oModel.getProperty("/modelData/data");
aData.push.apply(aData, response.data);
oModel.setProperty("/modelData/data", aData);

The difference is you first retrieve the array with data, add to the array, and then set the property with the updated array

Edit: Ok, makes sense now: you are adding an array to an array. And using 'push' just adds a new entry with whatever object you are adding. So you are adding a single entry (which happens to be an array) See updated answer

Upvotes: 3

solrac
solrac

Reputation: 629

Try this:

    for(var i = 0; i < response.data.length; i++){

       sap.ui.getCore().getModel().getProperty("/modelData/data").push(response.data[i]) 

    }

Upvotes: 0

Related Questions