cancel
Showing results for 
Search instead for 
Did you mean: 

how to get length of table loaded from oData/JSON model

Former Member
0 Kudos

hi all,

i've a local JSON model data loaded into a sap.m.Table as below in onBeforeRendering function:

var oModel = new sap.ui.model.json.JSONModel();
oModel.loadData("model/model.json", null, true);
this.getView().setModel(oModel);

and in my onAfterRendering function, i've below code:

alert("inside onAfterRendering");
oModel = this.getView().getModel();
var mLength = oModel.getProperty("/data/items/length");
alert(mLength);

and the alert gives me 'undefined', how do i get the total noof rows added to the table. rite its JSON.. but after that i'll have to move to oData model.

JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.json.JSONModel

as per the above api, i've set bAync as ture. even though i get undefined.

any quick way of getting the lenght of table loaded thru either JSON/oData models.

Accepted Solutions (0)

Answers (3)

Answers (3)

MariusStoica
Active Participant
0 Kudos

Hi,

Have you tried the simple method?


var objData = oController.getView().getModel("Invoices").getData();

var length = objData.length

It works for my JSON / OData models of my web app, as long as you get to the container of the model. You can start from the object that has the bound data, or from the page/screen itself.


sap.ui.getCore().byId("App--invoicesView").getModel("Invoices").getData().length

     or


sap.ui.getCore().byId("idIvoicesList").getModel("Invoices").getData().length

Marius

maximilian_lenkeit
Participant
0 Kudos

The only(!) approach that works for both JSON and OData Model is to go via the binding:

oTable.getBinding("items").getLength()

If you have a JSON Model, this will return the same as as oModel.getProperty("/data/items").length (assuming that's the binding path for your items aggregation). If you have an OData Model, it will return the number if records that are potentially available. Remember that the OData Model only manages a chunk of the actual data on the client and loads any other data on demand. So say there are 300 Sales Orders in the Backend and the client currently only has the first 100 records, the above approach will still return 300.

On a side note: You'll most likely (and should) work with asynchronous requests. That means that the data might not necessarily be there in onAfterRendering. To me, it seems like you want to trigger an action when the data has been received by the client. For that, look into the dataChange or dataReceived event of the Binding class. You can attach to these events also via oTable.getBinding("items").

- Max

Former Member
0 Kudos

thanks Max.

i've used oTable.getBinding("items").getLength().

in both before and after rendering functions.. i got it as '0'. And my JSON loading is done onBeforeRendering function not onInit().

requirement: i've an icon in table toolbar, which displays the total no. of records loaded into table.

and where do I use dataReceived or dataChange ? oModel or oTable?

maximilian_lenkeit
Participant
0 Kudos

Neither of them. The events are both fired on the binding (actually, they are called change and dataReceived, my previous post wasn't correct here).

So you'd need to do something like:

oBinding = oTable.getBinding("items");

oBinding.attachDataReceived(function() {

     var iLength = oBinding.getLength();

     // Update UI elmennts based on iLength

});

- Max

Former Member
0 Kudos

thanks Max.

i've my oTable declaration as below:

var oTable = this.getView().byId("itemTable");

and added your piece of code:

oBinding = oTable.getBinding("items");
var iLength;
oBinding.attachDataReceived(function() {
     iLength = oBinding.getLength();
     alert(iLength);
});

Error:

Uncaught TypeError: Cannot read property 'attachDataReceived' of undefined


JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.Binding

its all same per your code and as per API.
i've used with below options as well:

oTable.getBinding("/data/items");

oTable.getBinding("\data\items");

is my oTable declaration suites to call oBinding class?

former_member237257
Discoverer
0 Kudos

var oBinding = oTable.getBinding("rows");                 oBinding.attachDataReceived(function() {                     var iLength = oBinding.getLength();                     // Update UI elmennts based on iLength                     alert(iLength);                 }); This code works, I have tried!  🙂

SAPSeeker
Participant
0 Kudos

If your requirement is just to get the number of JSON items, then you can do it through JavaScript.

Example:

var json = '{"0":"1","1":"2","2":"3","3":"4"}';

var parsed = JSON.parse(json);

var arr = [];

for(var x in parsed){

  arr.push(parsed[x]);

}

// arr.length will give you the number of items.