如何将数据推送到现有的sapui5模型

时间:2021-02-10 16:20:29

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

我在sapui5中有一个Json模型as - //console.log(dModel);

如何将数据推送到现有的sapui5模型

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

我的新数据响应如下 - //console.log(response);

如何将数据推送到现有的sapui5模型

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

现在我想将新数据(仅数据部分)推送到/ modelData / data内的现有模型。

code I am trying -

代码我正在尝试 -

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

This code is pushing the data but as -

此代码正在推送数据但是 -

如何将数据推送到现有的sapui5模型

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.

在19(旧值)之后,它将20内的所有对象推到0,1,2 ......理想的方式应该是在19之后我应该得到20,21,22等等。

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

为了得到这个,我需要做些什么改变,谢谢......请建议。

2 个解决方案

#1


-1  

Try this:

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

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

    }

#2


3  

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

编辑:好的,现在有意义:您正在向数组添加数组。使用'push'只需添加一个新条目即可添加任何对象。所以你要添加一个条目(恰好是一个数组)请参阅更新的答案

#1


-1  

Try this:

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

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

    }

#2


3  

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

编辑:好的,现在有意义:您正在向数组添加数组。使用'push'只需添加一个新条目即可添加任何对象。所以你要添加一个条目(恰好是一个数组)请参阅更新的答案