Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  114] [ 3]  / answers: 1 / hits: 19557  / 10 Years ago, sat, june 7, 2014, 12:00:00

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.com/thread/3464458 and http://scn.sap.com/thread/3386927


More From » json

 Answers
33

You have a client model (JSON) and want to replace an element of the HeaderSet array with an updated version from the object that comes 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);

[#70670] Thursday, June 5, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;