View Full Version : dynamically add grid column to grid in 4.1?

16 Jun 2012, 11:43 PM
Would appreciate example of dynamically adding a column to grid panel. Cannot get any of the examples I found by googling to work. tia.

just got this to work:

initComponent: function()
var myCols = [
{header: 'Id', dataIndex: 'id', flex: 1},
{header: 'NameA', dataIndex: 'name', flex: 1},
{header: 'Address', dataIndex: 'address', flex:1}];
//{header: 'State', dataIndex: 'state', flex:1 }];
myCols[3] = {header: 'State', dataIndex: 'state', flex:1 };

this.columns =myCols;

is this good Ext JS coding? tia.

17 Jun 2012, 2:41 AM
I'm not sure I'd call that dynamically adding a column, that description would usually refer to adding a column after a grid is created. What you're doing is specifying the columns dynamically at creation time. In general, using initComponent to set dynamic config options is a perfectly good approach. One thing I would note is that dynamic column creation sometimes implies the need for subclassing your grid rather than putting if statements everywhere.

17 Jun 2012, 5:57 AM
For new column there must be data from server, right? There is an interesting example of grid reconfiguration by Response MetaData in Ext.data.reader.Json documentation.

17 Jun 2012, 1:23 PM
thanks for this - I saw a ref to it mentioned in a blog last week but could not locate it.