extjsextjs4extjs4.1extjs4.2extjs-mvc

ExtJS grid combo renderer shows as empty


I have a grid in my ExtJS 4.2.1 application that has an editable column with combo editor.

I need to render the column with the value from the DisplayField of the combo but the comboStore.getCount() = 0

Here is my grid:

Ext.define('App.employee.Grid', {
    extend: 'Ext.grid.Panel',
    requires: ['Ext.grid.plugin.CellEditing'],
    alias: 'widget.employee.grid',
   
    config: {
        LocationId: 0
    },

    initComponent: function() {

        var me = this,
            store = me.buildStore(),
            comboStore = Ext.create('App.store.catalog.Location', { autoLoad: true });

        me.rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
            clicksToMoveEditor: 1,
            autoCancel: false,
            listeners: {
                edit: function(editor, e) {

                }
            }
        });

        me.cellEditing = new Ext.grid.plugin.CellEditing({
            clicksToEdit: 1
        });

      
        Ext.applyIf(me, {
            plugins: [me.rowEditing],
            columns: [{
                xtype: 'rownumberer',
                text: '#',
                width: 50,
                sortable: false,
                align: 'center'
                //locked: true
            },{
                text: 'Number',
                dataIndex: 'EmployeeNumber',
                align: 'center',
                width: 90
            }, {
                text: 'Name',
                dataIndex: 'EmployeeName',
                flex: 1
            }, {
                text: 'Location',
                dataIndex: 'LocationId',
                width: 140,
                renderer: function(value) {
                  
                    // HERE!!!
                    // me.comboStore.getCount() = 0 so I never get a record
                  
                    var record = me.comboStore.findRecord('LocationId', value); 
                    return record.get('Description');
                },
                editor: {
                    xtype: 'combobox',
                    typeAhead: true,
                    triggerAction: 'all',
                    store: comboStore,
                    displayField: 'Description',
                    valueField: 'LocationId',
                    queryMode: 'local',
                    listConfig: {
                        width: 250,
                        // Custom rendering template for each item
                        getInnerTpl: function() {
                            return '<b>{Code}</b><br/>(<span style="font-size:0.8em;">{Description}</span>)';
                        }
                    }
                }
            }],
           
            store: store,
           
        });

        
        me.callParent(arguments);
    }

});

The problem is in the renderer function because the comboStore is always empty. The strange thing is that in my view, if I click to edit the row and open the combo, the combo has values.

Update

What I think is that my comboStore has a delay when loading so the renderer is fired before the comboStore gets loaded. I figure this out because if I debug in chrome and I wait a few seconds, then it works... but don't know how to force to wait until comboStore is loaded.

How can I solve this?


Solution

  • A couple of solutions:

    1. Ensure that the combo store is loaded before the grid store. This can be done by loading the combo first and from load event of its store trigger the grid store load. The disadvantage is that it adds unnecessary delay so this method impairs the user experience.
    2. Load all needed stores in one request. It requires a bit of coding but it saves server roundtrip so it's a very valuable approach. Store loadData method is used to actually load store with the received data. You would, of course, first call it on combo, then on the grid.
    3. The best method that I use almost exclusively is to turn the whole thing upside down and link display field to the store, not value field. Server must return both display and value fields in the grid store and a little piece of code that updates both fields in the grid store after editing is complete is required. This method is demonstrated here: Remote Combo in ExtJS Grid