javascript - Updating JSON Model in SAPUI5/OpenUI5 -
i have 1 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 time of creating sales order
, creating 1 object have direct keys firstname
,latname
,etc. , pushing headerset
array without keys.
var salesordermodel = this.getview().getmodel("somodel"); var salesorderdata = salesordermodel.getdata(); salesorderdata.headerset.push(soobject); salesordermodel.setdata(salesorderdata);
suppose have 3 objects in model, need update specific object in model? how can 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, can directly bind other details in detail page using 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 detail page have button read context , populate in editable form. , here need update specific edited item in correct object of model. tried adding unique dynamic key each object, @ time binding not work.
almost same thread here http://scn.sap.com/thread/3464458 , http://scn.sap.com/thread/3386927
you have client model (json) , want replace element of headerset array updated version object comes editable form. we're assuming here you're not wanting directly have two-way binding json model, because want input validation first, example.
you have unique key salesorderid can see using when publish onto event bus. let's assume stored in currentsalesorderid , use find right object replace in array new data, in object we'll call updatedorder:
// data model var salesorderdata = salesordermodel.getdata(); // find index of object via salesorderid var index = salesorderdata.headerset .map(function(order) { return order.salesorderid; }) .indexof(currentsalesorderid); // replace order in array salesorderdata.headerset.splice(index, 1, updatedorder);
Comments
Post a Comment