-
18 Nov 2008 7:19 AM #1Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
Adding/removing fields and columns
Adding/removing fields and columns
Because there are so much questions in the Help forum on how to add or remove a grid column I thought I'd post some utility code:
Usage example:Code:Ext.override(Ext.data.Store,{ addField: function(field){ field = new Ext.data.Field(field); this.recordType.prototype.fields.replace(field); if(typeof field.defaultValue != 'undefined'){ this.each(function(r){ if(typeof r.data[field.name] == 'undefined'){ r.data[field.name] = field.defaultValue; } }); } }, removeField: function(name){ this.recordType.prototype.fields.removeKey(name); this.each(function(r){ delete r.data[name]; if(r.modified){ delete r.modified[name]; } }); } }); Ext.override(Ext.grid.ColumnModel,{ addColumn: function(column, colIndex){ if(typeof column == 'string'){ column = {header: column, dataIndex: column}; } var config = this.config; this.config = []; if(typeof colIndex == 'number'){ config.splice(colIndex, 0, column); }else{ colIndex = config.push(column); } this.setConfig(config); return colIndex; }, removeColumn: function(colIndex){ var config = this.config; this.config = [config[colIndex]]; config.splice(colIndex, 1); this.setConfig(config); } }); Ext.override(Ext.grid.GridPanel,{ addColumn: function(field, column, colIndex){ if(!column){ if(field.dataIndex){ column = field; field = field.dataIndex; } else{ column = field.name || field; } } this.store.addField(field); return this.colModel.addColumn(column, colIndex); }, removeColumn: function(name, colIndex){ this.store.removeField(name); if(typeof colIndex != 'number'){ colIndex = this.colModel.findColumnIndex(name); } if(colIndex >= 0){ this.colModel.removeColumn(colIndex); } } });
As mentioned in post #37:Code:var grid = new Ext.grid.GridPanel({ store: new Ext.data.SimpleStore({ fields: ['A', 'B'], data: [['ABC', 'DEF'], ['GHI', 'JKL']] }), columns: [ {header: 'A', dataIndex: 'A'}, {header: 'B', dataIndex: 'B'} ] }); new Ext.Viewport({ layout: 'fit', items: grid }); grid.addColumn('C'); grid.addColumn({name: 'D', defaultValue: 'D'}, {header: 'D', dataIndex: 'D'}); grid.removeColumn('B');
Adding and removing fields currently does not update the extractors used to load data. If you want to update these too then you should call:
after modifying fields.Code:delete store.reader.ef; store.reader.buildExtractors();
Last edited by Condor; 31 Mar 2010 at 10:46 PM. Reason: Added extractor info
-
19 Dec 2008 2:35 AM #2
Thanks Condor!! Just what I needed right now
Webdeveloper from Norway
-
24 Dec 2008 9:47 PM #3
!! Oh man, I had my own hack to do this, but I like your's better.
-
30 Dec 2008 12:53 AM #4
-
30 Dec 2008 9:01 AM #5Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,169
- Vote Rating
- 28
i think this should be added to the base!

Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
30 Dec 2008 5:47 PM #6
-
30 Dec 2008 10:32 PM #7
if i want to use it like below,does it work? thanks!
Code:var myReader = new Ext.data.JsonReader({ id:"id", totalProperty: "total", root:"rows" }, new Ext.data.Record.create([ {name: 'id', type: 'int'}, {name: 'name', type: 'string'}, ]) ); var ds= new Ext.data.Store({ proxy:new Ext.data.HttpProxy({url:'/getdata'}), reader: myReader }); var cm = new Ext.grid.ColumnModel([ {header:'ID',dataIndex:'id'}, {header:'NAME',dataIndex:'name'} ]); var grid = new Ext.grid.EditorGridPanel({ store: ds, cm: cm }); grid.addColumn({header:'AGE',dataIndex:'age'});
-
31 Dec 2008 12:38 AM #8Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
Yes, that should work.
You could also use:
Code:grid.addColumn({name: 'age', type: 'int', defaultValue: null}, {header: 'AGE', dataIndex: 'age', sortable: true});
-
15 Feb 2009 11:36 PM #9
is it possible to add column on the designated place? eg. we have 5 columns and i would like to add second column
hmm...and removeColumn doesnt work in checkboxselectionmodel, addColumn is ok
-
16 Feb 2009 2:26 AM #10Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
The addColumn method supports a second parameter: the column index (0-based, so use 1 when inserting the second column).
A CheckboxSelectionModel doesn't have a dataIndex, so you'll have to use the removeColumn method of the columnmodel (and not the grid) and specify the colIndex to remove.


Reply With Quote

up!