PDA

View Full Version : header or mapping - data in grid?



benny
30 Apr 2007, 3:38 AM
Hi,

This is something that has been unclear for a while, so I guess I should probably ask if someone can shine some clarity on it.

I have a Grid with data coming from Json (externally, but on local domain). I originally used the example paging code, but somewhere alone the line introduced some code from these forums. So I have a hybrid.

The code I'm using has no mapping set in the JsonReader, but instead has 'header' set in the grid.ColumnModel. It seems to do the same thing (but I fear this may not be the entire truth).


Here is my JsonRead:


reader: new Ext.data.JsonReader({
root:"records",
totalProperty: 'totalCount',
id:"id"
},
['id', 'version','report_number','timestamp', 'store_name', 'store_number', 'icc']
)


... as you can see, no mapping.

And this is my ColumnModel:


var cm = new Ext.grid.ColumnModel([

{header: "ID", width: 100, dataIndex: 'id', type: 'int'},
{header: "Version", width: 100, dataIndex: 'version', type: 'int'},
{header: "Report Number", width: 120, dataIndex: 'report_number', renderer: renderBold},
{header: "TimeDate", width: 120, dataIndex: 'timestamp', type: 'date', renderer: renderDate},
{header: "Store Name", width: 150, dataIndex: 'store_name'},
{header: "Store Number", width: 100, dataIndex: 'store_number', type: 'int'},
{header: "ICC", width: 120, dataIndex: 'icc'}
]);


.. as you can see the 'header' seems to do the mapping which JsonReader does.

And here is the original example (from Ext example code) which I am not using:

reader: new Ext.data.JsonReader({
root: 'topics',
totalProperty: 'totalCount',
id: 'topic_id'
}, [
{name: 'title', mapping: 'topic_title'},
{name: 'author', mapping: 'username'},
{name: 'totalPosts', mapping: 'topic_replies', type: 'int'},
{name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
{name: 'lastPoster', mapping: 'user2'},
{name: 'excerpt', mapping: 'post_text'}
]),




What is the difference between using 'mapping' and 'header? and is there any reason to use one over the other?

Many thanks, and sorry for being thick in advance ;)

Animal
30 Apr 2007, 4:01 AM
In the Record definition:

"mapping" tells the reader how to retrieve the data from the data object. So for a JsonReader, it's the javascript path to the data from the row object.

"name" is the name by which the field may be referenced once it is extracted and stored in a Record object, cached in a Store object.

In the ColumnModel defnition:

"header" is the header to display.

"dataIndex" is the name of the field friom the Record to display in that column (must match a "name" in the Record definition)