GridPanel from JSON data

| | Comments (1) | TrackBacks (0)



Given the following JSON data:

{"totalCount": 2,"items": [{"ID": 1,"Name": "First"},{"ID": 2,"Name": "Second"}]}


Note: ExtJS requires the above format for GridPanel data. The list of data must be wrapped in an object that contains a "total count" attribute, and the list itself is the data of an "items" attribute. These names of these attributes must match the "totalProperty" and "root" values in the JsonReader of the Store used by the GridPanel. See below.




Here's an ExtJS GridPanel that will load and display it:

ExampleGrid = Ext.extend(Ext.grid.GridPanel, { title: 'Example', border: true, frame: true, closable: true,

        initComponent: function() {

            var proxy = new Ext.data.HttpProxy({
                        url: 'http://host/url/to/get/JSON/data'
                    });

            var store = new Ext.data.Store({
                        remoteSort: true,
                        proxy: proxy,
                        baseParams: {
                            limit: 100
                        },
                        reader: new Ext.data.JsonReader({
                                    totalProperty: 'totalCount',
                                    root: 'items'
                                }, [{
                                            name: 'ID'
                                        }, {
                                            name: 'Name'
                                        }])
                    });

            Ext.apply(this, {
                        loadMask: true,
                        store: store,
                        colModel: new Ext.grid.ColumnModel([{
                                    header: 'ID',
                                    dataIndex: 'ID',
                                    sortable: true
                                }, {
                                    header: 'Name',
                                    dataIndex: 'Name',
                                    sortable: true
                                }])
                    });

            ExampleGrid.superclass.initComponent.apply(this, arguments);
        },

        onRender: function() {
            this.store.load();
            ExampleGrid.superclass.onRender.apply(this, arguments);
        }
    });

Ext.reg('ExampleGrid_panel', ExampleGrid);

0 TrackBacks

Listed below are links to blogs that reference this entry: GridPanel from JSON data.

TrackBack URL for this entry: http://hutten.org/cgi-sys/cgiwrap/hutten/managed-mt/mt-tb.cgi/6

1 Comments

Hi Bill,

thanks for your example.

I tried it using ExtJS 3.4 but unfortunately the grid does not show up.

Is there anything else you need to load static json data to a grid (e.g. add additional informations to the json data, configure your webserver in a certain way, make changes to the java script code) ?


Cheers,
Dr. Code

Leave a comment

About this Entry

This page contains a single entry by Bill Hutten published on May 5, 2009 11:59 AM.

Creating a TreePanel from markup was the previous entry in this blog.

Complex objects in classes (Ext3 & Ext4 examples) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.34-en