Skip to content

May 13, 2011

Extracts non-row data with ExtJS JsonStore

by Joe Kuan

Suppose you have a JsonStore object retrieving a number of records from the server and there are specific values which are the same for all the records, eg. average value.

For whatever the reason, you really just want to return these specific values separately as a field inside the JSON string, like the following:

   root: [ { id: 1, field1: value1, field2: value2, ... },
           { id: 2, field2: value2, field2: value2, ... }
                .... ],
  // or average: [ avg_val1, avg_val2 ]
  // or average: { field1: avg_val1, field2: avg_val2 }
  average1: avg_val1,
  average2: avg_val2,

You can setup with JsonReader‘s metaData property to create dynamic fields. However if you want a quick way to extract these values without bothering with metaData. Then you do it with the following one-liner:

var average1 = jsonStore.reader.jsonData.average1;

Of course, you need to do this within the Store’s load event handler.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: