将数组结果从一个 json 对象移动到另一个对象
Moving array results from one json object to another
我在 _initializeData 中创建了一个新的 json 数据模型。目的是当用户尝试删除购物车中的商品时,我想将该商品存储在这个新数据模型中。因此,在单击删除按钮时,我正在读取 json 数据模型,其中包含购物车中的所有项目,然后使用已删除元素的索引将结果复制到新模型中。如果我只删除一个项目,它工作正常。但是如果我在另一个项目上按删除,新模型中的先前条目将被新条目覆盖。它不像数组那样包含所有条目。
_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}
// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();
// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");
// reading the removed item data
var removedPart = data[idx];
// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
this.getView().getModel("removedItems").setProperty("/removedParts", removedPart);
使用Array.slice()
避免引用。在这里,引用导致了覆盖行为
问题是您覆盖了模型上的完整对象:removedItems。
既然你覆盖了它,旧数据就丢失了。因此,您需要做的就是获取旧的已删除项目数组,将一个元素推入其中,然后将其设置为模型。
_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}
// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();
// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");
// reading the removed item data
var removedPart = data[idx];
// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
// Get the old deleted items first :
var oDeletedItems = this.getView().getModel("removedItems").getProperty("/removedParts");
oDeletedItems.removedParts.push(removedPart); // the array is removedParts and oDeletedItems is the obejct which stores the array as per _initlaise method
this.getView().getModel("removedItems").setProperty("/removedParts", oDeletedItems);
希望对您有所帮助。
我在 _initializeData 中创建了一个新的 json 数据模型。目的是当用户尝试删除购物车中的商品时,我想将该商品存储在这个新数据模型中。因此,在单击删除按钮时,我正在读取 json 数据模型,其中包含购物车中的所有项目,然后使用已删除元素的索引将结果复制到新模型中。如果我只删除一个项目,它工作正常。但是如果我在另一个项目上按删除,新模型中的先前条目将被新条目覆盖。它不像数组那样包含所有条目。
_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}
// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();
// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");
// reading the removed item data
var removedPart = data[idx];
// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
this.getView().getModel("removedItems").setProperty("/removedParts", removedPart);
使用Array.slice()
避免引用。在这里,引用导致了覆盖行为
问题是您覆盖了模型上的完整对象:removedItems。
既然你覆盖了它,旧数据就丢失了。因此,您需要做的就是获取旧的已删除项目数组,将一个元素推入其中,然后将其设置为模型。
_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}
// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();
// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");
// reading the removed item data
var removedPart = data[idx];
// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
// Get the old deleted items first :
var oDeletedItems = this.getView().getModel("removedItems").getProperty("/removedParts");
oDeletedItems.removedParts.push(removedPart); // the array is removedParts and oDeletedItems is the obejct which stores the array as per _initlaise method
this.getView().getModel("removedItems").setProperty("/removedParts", oDeletedItems);
希望对您有所帮助。