user3349850
user3349850

Reputation: 225

local JSON file and data in controller method

This is my code in controller init method:

 mycontroller = this

 var data = [{
     "No": "456980",
     "Updates": [{
         "Test": "abc"
     }, {
         "Test": "bca"
     }, {
         "Test": "dbd"
     }]
 }, {
     "No": "456980",
     "Updates": [{
         "Test": "abc"
     }, {
         "Test": "bca"
     }, {
         "Test": "dbd"
     }]
 }, {
     "No": "456980",
     "Updates": [{
         "Test": "abc"
     }, {
         "Test": "bca"
     }, {
         "Test": "dbd"
     }]
 }]
 var oModel = new sap.ui.model.json.JSONModel();
 oModel.setData(data);

 //set this model to list control having custom list as aggregation
 mycontroller.List1.setModel(oModel);

 mycontroller.List1.bindAggregation("items", {
     path: "/",
     template: mycontroller.List1_template
 });

 var ListItems = P1Notificationcontroller.list1.getItems();
 var ListLength = ListItems.length;

 for (var i = 0; i < ListLength; i++) {
     var control = "status_vbox-list1-" + i;
     sap.ui.getCore().byId(control).bindAggregation("items", {
         path: "Updates",
         template: new sap.m.Text('', {
             text: '{Update}'
         })
     });
 }

view declaration create content

oController.List1 = new sap.m.List("List1", {
    headerDesign: sap.m.ListHeaderDesign.Standard
});
oController.List1_template = new sap.m.CustomListItem("McustomlistItem", {
            content: [
                new sap.m.VBox('', {
                    items: [
                        new sap.m.Text('', {
                            text: "{No}"
                        })
                        new sap.m.VBox('status_vbox', {
                            items: []
                        })
                    ]
                }));

The above code in view and controllers init method works perfectly fine the items in custom list VBox are aggregated from update array in data.

when the same data when maintained in local json file & by using this line of code

 var jsonmodel = new sap.ui.model.json.JSONModel("mockData/local.json");
 mycontroller.List1.setModel(jsonmodel)

the loop for VBox is failing because we are not able to loop the object .

when i console.log of both models OData attribute of one model is showing as Array and other as object, because of object the loop is not happening

how can we maintain the same data in local json file to loop the updates attribute to the VBox in custom list item as above.

Upvotes: 0

Views: 1951

Answers (1)

user3349850
user3349850

Reputation: 225

the issue resolved by calling jquery ajax call to fill the local json model as below

  jQuery.ajax({
            url: "mockData/Local.json",
            dataType: "json",
            success: function(data, textStatus, jqXHR) {        

            var jsonmodel = new sap.ui.model.json.JSONModel();
                jsonmodel.setData(data);
                sap.ui.getCore().setModel(jsonmodel,'testmodel');

            mycontroller.List1.setModel(jsonmodel)


               }

Upvotes: 1

Related Questions