cancel
Showing results for 
Search instead for 
Did you mean: 

Adding elements to arrays in OData model in SAPUI5

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member226851
Participant
0 Kudos

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

0 Kudos

Hi Tomasz,

I believe yours is the cleanest solution, you add an element into array and then you refresh the model so that the bindings are refreshed, what more you want

Regards,

Chidan

daniel_rothmund
Participant
0 Kudos

Hi Flo ,

I have the same problem 🙂

Regards

daniel_rothmund
Participant
0 Kudos

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);

daniel_rothmund
Participant
0 Kudos

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);

          },

});

daniel_rothmund
Participant
0 Kudos

Has someone a better solution ? For appending an entry to odata without any server request ?

former_member182048
Active Contributor
0 Kudos

Daniel

Have you tried


oModel.createEntry("Categories",{CategoryID:99,CategoryName:"Food",Description:"Food Desc"});

hth

jsp