Copy ODataModel to JSONModel for use in table

4.4k views Asked by At

I need to show a list in a table. This list shall be filtered at client-side, and because of that I need to copy the OData model into a local JSON model.

The OData model looks like this:

OData model

enter image description here

I am able to show the "/ZcountWerksSet" list (from the OData model) inside a table using this code:

oSearchTable.bindAggregation("items", "/ZcountWerksSet", new sap.m.ColumnListItem({
             cells : [
                  new sap.m.Text({
                       text : "{Name1}"
                  }),
                  new sap.m.Text({
                       text : "{Werks}"
                  })
             ]
        }));

Then I am copying this into a JSON model like this:

            var oModel = this.getView().getModel();
            var oModelJson = new sap.ui.model.json.JSONModel();
            oModel.read("/ZcountWerksSet", {
            success: function(oData, response) {
                oModelJson.setData(oData);
                sap.ui.getCore().setModel(oModelJson, "oJSONModel");
                alert("Success!");
            },
            error: function(response) {
                alert("Error");
            }
        });

Then the model looks like this

JSON Model

JSON Model

Trying to show the same list (from JSON model this time), in a table using the following code is NOT working:

oSearchTable.bindAggregation("items", "{oJSONModel>/}", new sap.m.ColumnListItem({
             cells : [
                  new sap.m.Text({
                       text : "{oJSONModel>Name1}"
                  }),
                  new sap.m.Text({
                       text : "{oJSONModel>Werks}"
                  })
             ]
        }));

How can I show the same data in the table, but only from the JSON model this time?

Any ideas what I'm doing wrong?

2

There are 2 answers

0
matbtt On

You need to adopt your binding path to {oJSONModel>/results} as a different binding implementation is used for a JSONModel.

0
Chessinio On

You're right. I did it like this, works fine now.

        var oSearchTable = sap.ui.getCore().byId("searchTableId");
        var oModel = this.getView().getModel();
        var oModelJson = new sap.ui.model.json.JSONModel();
        oModel.read("/ZcountWerksSet", {
            success: function(oData, response) {
                oModelJson.setData(oData);
                var oRow = new sap.m.ColumnListItem({
                     cells : [
                          new sap.m.Text({
                               text : "{Name1}"
                          }),
                          new sap.m.Text({
                               text : "{Werks}"
                          })
                     ]
                });
                oSearchTable.setModel(oModelJson);
                oSearchTable.bindItems("/results", oRow);
                sap.ui.getCore().setModel(oModelJson, "oJSONModel");
            }
        });