Link to home
Start Free TrialLog in
Avatar of strimp101
strimp101

asked on

ExtJS Metachange event with GroupingStore

Greetings all,

I am simply trying to use the metachange event to dynamically load data in from the server. In this case, the metachange event is not even firing (no alerts). Any ideas?

Ext.BLANK_IMAGE_URL = '/bin/js/ext-3.2.1/resources/images/default/s.gif';

Ext.ns('Weekend');

Open in new window


This is the grid (extending a GridPanel):
Weekend.Grid = Ext.extend(Ext.grid.GridPanel, {
    initComponent:function() {
        var config = {

            columns:[]
           
        };

        Ext.apply(this, Ext.apply(this.initialConfig, config));

        this.store = weekendStore;
        this.viewConfig = {forceFit: true};
        this.loadMask = true;

        this.id = 'weekend';

        Weekend.Grid.superclass.initComponent.apply(this, arguments);

        this.on({
            activate: {scope:this, single:true, fn:function() {
                this.store.load({params:{start:0, limit:10}});
            }}
        });

    }

});

Open in new window


...and this is the the GroupingStore:

Ext.reg('weekendGrid', Weekend.Grid);

var weekendStore = new Ext.data.GroupingStore({
   
    url: '/bin/php/do/weekend.do.php',
   
    reader: new Ext.data.JsonReader({
        root: 'data',

        listeners: {

            metachange: function(store, meta) {
               
                var g = Ext.getCmp('weekendGrid');
               
                alert(g);
               
                var cm = g.getColumnModel();
               
                alert(cm);
               
                cm.setConfig(meta.colModel);
                g.reconfigure(store, cm);
            }
        }
    }),

    sortInfo:{field: 'company', direction: "ASC"},
    groupField:'industry',
    storeId: 'weekendStore'
});

Open in new window


Any clue on what might be the issue here?
Avatar of Gjslick
Gjslick
Flag of United States of America image

The metachange event is only going to fire if you dynamically re-configure your store's fields by having the store load json data with a special 'metaData' property inside of it.  What does your response data look like when your store loads?  There is an example in the docs for JsonReader of how your data should look to achieve this dynamic configuration by metaData: http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.JsonReader

Is your store loading at all by the way?  You may want the 'autoLoad: true' config in there. You will be able to tell if your store is loading by using a utility like Firebug, and seeing if an ajax request is being made for the store's data (i.e. an ajax request is being made to /bin/php/do/weekend.do.php).

Are you sure that you want to do this configuration by metaData though?  Will your store's fields be changing with different requests to load the store?  If not, and you just want the dynamic data, then you should really just specify the fields that are going to be used directly on the store (or reader) with the 'fields' config, as well as directly specifying your columns for the grid too.

Hope that helps, and let me know if you have any more questions.

-Greg
Avatar of strimp101
strimp101

ASKER

Thanks for the reply.

The is the JSON response from the server which includes the metaData property:

{
    "metaData":{
        "root":"data",
        "fields":["company","price","change","pctChange","lastChange","industry"],
        "colModel":[
            {"id":"company","header":"Company","sortable":true,"dataIndex":"company"},
            {"header":"Price","sortable":true,"dataIndex":"price"},
            {"header":"Change","sortable":true,"dataIndex":"change"},
            {"header":"Percent Updated","sortable":true,"dataIndex":"pctChange"},
            {"header":"Last Updated","sortable":true,"dataIndex":"lastChange"},
            {"header":"Industry","sortable":true,"dataIndex":"industry"}
        ]
    },
    "data":[
        {"company":"3m Co","price":71.72,"change":0.02,"pctChange":0.03,"lastChange":"09\/09\/1010","industry":"Manufacturing"},
        {"company":"Alcoa Inc","price":29.01,"change":0.42,"pctChange":1.47,"lastChange":"09\/09\/1010","industry":"Manufacturing"},
        {"company":"Altria Group Inc","price":83.81,"change":0.28,"pctChange":0.34,"lastChange":"09\/09\/1010","industry":"Manufacturing"},
        {"company":"American Express Company","price":52.55,"change":0.01,"pctChange":0.02,"lastChange":"09\/09\/1010","industry":"Finance"},
        {"company":"American International Group, Inc.","price":64.13,"change":0.31,"pctChange":0.49,"lastChange":"09\/09\/1010","industry":"Services"},
        {"company":"AT&T Inc.","price":31.61,"change":-0.48,"pctChange":-1.54,"lastChange":"09\/09\/1010","industry":"Services"},
        {"company":"Boeing Co.","price":75.43,"change":0.53,"pctChange":0.71,"lastChange":"09\/09\/1010","industry":"Manufacturing"},
        {"company":"Caterpillar Inc.","price":67.27,"change":0.92,"pctChange":1.39,"lastChange":"09\/09\/1010","industry":"Services"},
        {"company":"Citigroup, Inc.","price":49.37,"change":0.02,"pctChange":0.04,"lastChange":"09\/09\/1010","industry":"Finance"},
        {"company":"E.I. du Pont de Nemours and Company","price":40.48,"change":0.51,"pctChange":1.28,"lastChange":"09\/09\/1010","industry":"Manufacturing"},
        {"company":"Exxon Mobil Corp","price":68.1,"change":-0.43,"pctChange":-0.64,"lastChange":"09\/09\/1010","industry":"Manufacturing"}
    ]
}

I know that the data is loaded because I copied the above response right from Firebug.

I do want to load the columns dynamically because I am using this code to return many different sizes of data grids, some with upwards of 20 columns!

The thing that is really getting me is that it seems the metachange event is not even firing (hence no alerts). If I change the metachange event to a load event, it fires but

var g = Ext.getCmp('weekendGrid');


returns g as undefined.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it!

Code

Ext.BLANK_IMAGE_URL = '/bin/js/ext-3.2.1/resources/images/default/s.gif';

Ext.ns('Weekend');

Weekend.Grid = Ext.extend(Ext.grid.GridPanel, {
      initComponent: function() {
            var config = {
                  columns:[],
                  id: 'weekendGrid',
                  store: weekendStore,
                  viewConfig: {forceFit: true},
                  loadMask: true,
            };

        Ext.apply(this, Ext.apply(this.initialConfig, config));

            Weekend.Grid.superclass.initComponent.apply(this, arguments);
            
            this.on({
                  activate: {scope:this, single:true, fn:function() {
                        this.store.load({params:{start:0, limit:10}});
                  }}
            });
      },
      id: 'weekendGrid',
});

Ext.reg('weekendGrid', Weekend.Grid);

var weekendStore = new Ext.data.GroupingStore({
      reader: new Ext.data.JsonReader(),
      url: '/bin/php/do/weekend.do.php',
      root: 'data',
      listeners: {
            metachange: function(store, meta) {
                  var g = Ext.getCmp('weekendGrid');
                  var cm = g.getColumnModel();
                  cm.setConfig(meta.colModel);
                  g.reconfigure(store, cm);
            }
      },
      sortInfo:{field: 'company', direction: "ASC"},
      groupField:'industry',
      storeId: 'weekendStore'
});