on 08-07-2013 1:27 PM
Hi,
I have a question concernig the adding of an element to an array within an OData Model in SAPUI5.
Is there any standard functionality to add an element where only the necessary attributes of the element have to specified? Or do I have to rebuild the whole object with its structure (like the PositionSet structure in the attached screenshot) and use the push() method to append the new element?
Thanks a lot for any response! 🙂
Best,
Flo
Hi,
I have been looking for a solution as well, because I have the same problem. Namely, when I add an item to a JSON model using myModel.oData.myArray.push(item), a view doesn't refresh itself (a table hasn't an added item, nevertheless it exists within the model). Only when I use myModel.refresh(true) function at the end of adding items, view refresh itself.
Does anybody have a solution for this issue?
Best regards,
Tomasz Sobkowiak
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Flo ,
I have the same problem 🙂
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
I have found a dirty solution for the append.
with this "dirty coding" in the debugger the entry is appended and also the context. But my Controll doesn't update the aggregation....
Have someone a tipp ?
var oModel = sap.ui.getCore().getModel("po");
var oContext = evt.getSource().getBindingContext("po");
var mainPos = oModel.getProperty("PosNo",oContext);
var data = oModel.oData;
var item_templ = null;
var pos_no = 0;
for (var attr in data) {
if(data.hasOwnProperty(attr)){
item_templ=data[attr];
if ( item_templ.PosNo > pos_no) {
pos_no = item_templ.PosNo;
}
}
}
attr=attr.replace(/PosNo=(\d*)/g , "PosNo=" + (pos_no +1 ) );
var new_item = clone(item_templ);
new_item.PosNo = pos_no + 1;
new_item.Pre_Pos = mainPos;
new_item.Desc = "";
oModel.oData[attr]= new_item;
var path = oContext.sPath.replace(/PosNo=(\d*)/g , "PosNo=" + (pos_no +1 ) );
var new_context = new sap.ui.model.Context(oModel,path,false);
Hi have found the solution!
But if this is best way 🙂 The trick was that I have loop through all bindings which a referenced to the model. And then I must append the context to these binding.
Is there a nicer solution for this f.e. sapui5 standard?
split : function(evt) {
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
oModel = sap.ui.getCore().getModel("po");
var oContext = evt.getSource().getBindingContext("po");
var mainPos = oModel.getProperty("PosNo",oContext); //
var data = oModel.oData;
var item_templ = null;
var pos_no = 0;
// Get the last position number of the position entryset class
for (var attr in data) {
if(data.hasOwnProperty(attr)){
item_templ=data[attr];
if ( item_templ.PosNo > pos_no) {
pos_no = item_templ.PosNo;
}
}
}
attr=attr.replace(/PosNo=(\d*)/g , "PosNo=" + (pos_no +1 ) );
// Clone a existing item to a new
var new_item = clone(item_templ);
// Change some data
new_item.PosNo = pos_no + 1;
new_item.Pre_Pos = mainPos;
new_item.Desc = "";
data[attr]= new_item;
// Generate a new Context Path Entry Replace key PosNo with new PosNo
var path = oContext.sPath.replace(/PosNo=(\d*)/g , "PosNo=" + (pos_no +1 ) );
// Create a new context element
var new_context = new sap.ui.model.Context(oModel,path,false);
// Append the new context to element to the Model
oModel.mContexts[new_context.sPath] = new_context;
// Manual append the the context entry to all Bindings of the model
for (var i=0; i< oModel.aBindings.length; ++i) {
var binding = oModel.aBindings[i];
if (binding.aKeys != undefined ){
// It is a BindingList
binding.aKeys[binding.aKeys.length ] = path;
binding.iLength = binding.iLength + 1;
binding.iLastEndIndex = binding.iLastEndIndex + 1;
binding.aLastContexts[binding.aLastContexts.length] = new_context;
binding.oLastContextData[path.substring(1,path.length)] = new_item;
}
}
// Update fire update to the controls
oModel.updateBindings(true);
},
});
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.