admin管理员组文章数量:1333706
I have one json model, model/salesOrder.json,
{
"HeaderSet" : []
}
The initialization,
var SalesOrderModel = new sap.ui.model.json.JSONModel("model/SalesOrder.json");
sap.ui.getCore().setModel(SalesOrderModel, "SOModel");
On the time of creating Sales Order
, creating one object which have direct keys like FirstName
,LatName
,etc. and pushing to this HeaderSet
array without any keys.
var salesOrderModel = this.getView().getModel("SOModel");
var salesOrderData = salesOrderModel.getData();
salesOrderData.HeaderSet.push(SoObject);
salesOrderModel.setData(salesOrderData);
Suppose I have 3 objects in this model, I need to update a specific object in this model? How can I do that?
Master Page View,
<List id="listMyOrder" growing="true" growingThreshold="10" growingScrollToLoad="true" noDataText="No drafts found" mode="SingleSelectMaster" items="{path : 'SOModel>/HeaderSet' }" select="handleMyOrderSelect">
<ObjectListItem title="{SOModel>PurchaseOrderNumber}">
<attributes>
<ObjectAttribute text="Due Date : {path : 'SOModel>PurchaseOrderDate' }" />
</attributes>
</ObjectListItem>
</List>
Master Page Controller,
handleMyOrderSelect: function (evt) {
this._showDetail(evt.getParameter("listItem"));
},
_showDetail: function (item) {
_salesOrderIDForCart = item.getBindingContext("SOModel").getObject().SalesOrderID
sap.ui.getCore().getEventBus().publish("nav", "to", {
id: "SoDetail",
data: {
source: "MyOrders",
SalesOrderID: item.getBindingContext("SOModel").getObject().SalesOrderID,
context: item.getBindingContext("SOModel")
}
});
}
Now, I can directly bind other details in detail page using this context,
<List showSeparators="None" id="tblSummary">
<InputListItem label="PO Number">
<Label text="{SOModel>PurchaseOrderNumber}" design="Bold"/>
</InputListItem>
<InputListItem label="PO Date">
<Label text="{path : 'SOModel>PurchaseOrderDate'}" design="Bold"/>
</InputListItem>
</List>
In this detail page I have a button which will read this context and populate in an editable form. And here I need to update this specific edited item in the correct object of the model. I tried by adding a unique dynamic key to each object, but at that time my binding will not work.
Almost same thread here and
I have one json model, model/salesOrder.json,
{
"HeaderSet" : []
}
The initialization,
var SalesOrderModel = new sap.ui.model.json.JSONModel("model/SalesOrder.json");
sap.ui.getCore().setModel(SalesOrderModel, "SOModel");
On the time of creating Sales Order
, creating one object which have direct keys like FirstName
,LatName
,etc. and pushing to this HeaderSet
array without any keys.
var salesOrderModel = this.getView().getModel("SOModel");
var salesOrderData = salesOrderModel.getData();
salesOrderData.HeaderSet.push(SoObject);
salesOrderModel.setData(salesOrderData);
Suppose I have 3 objects in this model, I need to update a specific object in this model? How can I do that?
Master Page View,
<List id="listMyOrder" growing="true" growingThreshold="10" growingScrollToLoad="true" noDataText="No drafts found" mode="SingleSelectMaster" items="{path : 'SOModel>/HeaderSet' }" select="handleMyOrderSelect">
<ObjectListItem title="{SOModel>PurchaseOrderNumber}">
<attributes>
<ObjectAttribute text="Due Date : {path : 'SOModel>PurchaseOrderDate' }" />
</attributes>
</ObjectListItem>
</List>
Master Page Controller,
handleMyOrderSelect: function (evt) {
this._showDetail(evt.getParameter("listItem"));
},
_showDetail: function (item) {
_salesOrderIDForCart = item.getBindingContext("SOModel").getObject().SalesOrderID
sap.ui.getCore().getEventBus().publish("nav", "to", {
id: "SoDetail",
data: {
source: "MyOrders",
SalesOrderID: item.getBindingContext("SOModel").getObject().SalesOrderID,
context: item.getBindingContext("SOModel")
}
});
}
Now, I can directly bind other details in detail page using this context,
<List showSeparators="None" id="tblSummary">
<InputListItem label="PO Number">
<Label text="{SOModel>PurchaseOrderNumber}" design="Bold"/>
</InputListItem>
<InputListItem label="PO Date">
<Label text="{path : 'SOModel>PurchaseOrderDate'}" design="Bold"/>
</InputListItem>
</List>
In this detail page I have a button which will read this context and populate in an editable form. And here I need to update this specific edited item in the correct object of the model. I tried by adding a unique dynamic key to each object, but at that time my binding will not work.
Almost same thread here http://scn.sap./thread/3464458 and http://scn.sap./thread/3386927
Share Improve this question edited Jun 8, 2014 at 8:18 Anshad Vattapoyil asked Jun 7, 2014 at 12:15 Anshad VattapoyilAnshad Vattapoyil 23.5k19 gold badges90 silver badges134 bronze badges 2- Not exactly sure of what you're asking, but it seems to me that it's more a JavaScript question than an sapui5 question, is that correct? In other words, are you asking how to find the appropriate object in the array to update? – qmacro Commented Jun 8, 2014 at 8:17
- @qmacro Yes, exactly. – Anshad Vattapoyil Commented Jun 8, 2014 at 8:18
1 Answer
Reset to default 2You have a client model (JSON) and want to replace an element of the HeaderSet array with an updated version from the object that es from your editable form. We're assuming here that you're not wanting to directly have two-way binding back to the JSON model, because you want to do input validation first, for example.
You have a unique key SalesOrderID that we can see you already using when you publish onto the event bus. Let's assume this is stored in currentSalesOrderID and use this to find the right object to replace in the array with the new data, in an object we'll call updatedOrder:
// Get the data from the model
var salesOrderData = salesOrderModel.getData();
// Find the index of the object via the SalesOrderID
var index = salesOrderData.HeaderSet
.map(function(order) { return order.SalesOrderID; })
.indexOf(currentSalesOrderID);
// Replace the order in the array
salesOrderData.HeaderSet.splice(index, 1, updatedOrder);
本文标签: javascriptUpdating JSON Model in SAPUI5OpenUI5Stack Overflow
版权声明:本文标题:javascript - Updating JSON Model in SAPUI5OpenUI5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742355842a2459391.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论