Rick Weller
Rick Weller

Reputation: 1258

count a json file

I want to display the count of records in a json file. The json file is for example test.json and i want to display the number of records in my extjs file.

I can't get it to work but it sounds easy. Anyone with an idea?

This is what i tried:

var jsonData = new Ext.data.JsonStore({
  url: 'test.json',
  root: 'ritas',
});
alert(jsonData.length);

The alert will show me "undefined".

When I try a test case like this

var jsonData = [{a:1},{b:2},{c:3}];
alert(jsonData.length);

the alert will show 3.

Upvotes: 0

Views: 633

Answers (1)

RoccoC5
RoccoC5

Reputation: 4213

The Ext.data.JsonStore class does not have a length property. Instead, use the getCount() method of the JsonStore to get the number of cached records:

var jsonData = new Ext.data.JsonStore({
  url: 'test.json',
  root: 'ritas',
  fields: [ 'field1', 'field2' ] // record property names go here
});
alert(jsonData.getCount());

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.JsonStore-method-getCount

This store is configured to consume JSON in the following format:

{
    ritas: [
        {field1: 'field1Value', field2: 'field2Value'}, // record 1
        {field1: 'field1Value2', field2: 'field2Value2'} // record 2
    ]
}

Is your server returning an object in this format?

Upvotes: 1

Related Questions