Skip to Content

get table row count at load

Hi,

I am deploying over gateway with UI5 version 1.28 and my model is defined in controller and am able to get records in table from json. I am trying to get the number of records displayed in table on the load. I am able to do that on click of a button via sap.ui.getCore().byId("oTable").getModel("MyJsonData").oData.length

but I want to get this without an action and on load only. Tried calling the same at init of controller but it does not work and returns undefined.

Tried few other options as below

sap.ui.getCore().getModel("MyJsonData").oData.lengthvia = undefined.

sap.ui.getCore().byId("oTable").getRows().length - does not work.

_getRowCount() - does not work

oTable.getBinding().getLength() - does not work

Anything else to try?

My code is as below: In View:

<code>var oTable = new sap.ui.table.Table("oTable",{
            selectionMode : sap.ui.table.SelectionMode.Single,
            selectionBehavior : sap.ui.table.SelectionBehavior.Row,
            visibleRowCount : 7,
            firstVisibleRow : 3,
            rowSelectionChange:oController.formatTable,
            fixedColumnCount : 2
        });

oTable.bindRows("MyJsonData>/");

Inside Component.js

<code>var oModel = new sap.ui.model.json.JSONModel("model/data.json");
this.setModel(oModel, "MyJsonData");
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Jun 30, 2017 at 05:33 AM

    HI

    I believe this will work for you

    var oModel = new sap.ui.model.json.JSONModel("model/data.json");
    oModel.attachRequestCompleted(function() {
    console.log(this.getData().length);
    }); this.setModel(oModel, "MyJsonData");
    Add comment
    10|10000 characters needed characters exceeded

    • Hi

      In component.js I updated like this

      var oModel = new sap.ui.model.json.JSONModel("model/data.json");

      oModel.attachRequestCompleted(function() { console.log(this.getData().length);

      oModel.setData(this.getData()); });

      this.setModel(oModel, "MyJsonData");

      console.log(oModel);

      console.log(sap.ui.getCore().getModel("MyJsonData"));

      I do get the length this way but the binding stops working for the table- not sure why - with no changes made to my view. Both the console statements return data.

      If I put my earlier code the binding works.

  • Jul 05, 2017 at 06:08 PM

    Your answer was correct Dennis. Issue with my code. thanks

    Add comment
    10|10000 characters needed characters exceeded