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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36767713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:32:08  来源:igfitidea点击:

how to push data to existing sapui5 model

javascriptjsonsapui5

提问by namita kalita

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

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

enter image description here

在此处输入图片说明

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

我的新数据响应如下 - //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.

现在我想将新数据(仅数据部分)推送到 /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.

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

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

我需要做哪些改变才能得到这个,谢谢...请提出建议。

采纳答案by carlosfcmendes

Try this:

尝试这个:

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

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

    }

回答by Qualiture

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”只会添加一个包含您正在添加的任何对象的新条目。所以你要添加一个条目(恰好是一个数组)查看更新的答案