View Full Version : How to add/remove fields from store ExtJs 4.X

28 Jun 2013, 3:01 AM
I was updating the store fields dynamically with the code mentioned in below snippet for earlier versions of Ext js but now i am not getting alternative approach to solve my issue.

var fields="field1~field2~field3";

function reconfigFields(store, fields) {
var fieldArr = fields.split('~');
if (fieldArr.length == 1 && fieldArr[0] == '') { return; }
var i;
while (store.fields.getCount() > 0) {
for (i = 0; i < fieldArr.length; i++) {
store.fields.add(fieldArr[i], new Ext.data.Field({
name: fieldArr[i]
Now, i am able to remove all fields by the function store.removeFields(); but to add new fields i am not getting any method. Please help me to solve my issue with Ext JS upgrade.

28 Jun 2013, 5:18 AM
You can define new fields via the model. A model is automatically created if you set fields in the config property of your store.

The model can be retrieved by

var store.model;

set your new field collection

model.setFields(['name', 'email', 'phone'], 'id');//id is record id

and reload your store


1 Jul 2013, 1:08 AM
As you can see in the code snippet above. I have the list of fields as an Array which are needs to be added to store. If i am setting fields one by one by the solution given by you, all other added fields are getting overwrite and last one remain in the store. So please provide me the way to add fields on by one through js side.

1 Jul 2013, 1:23 AM
The store has no fields anymore it is replaced by a model in extjs 4.

See the constructor of the AbstractStore what happens with your field config

if (!me.model && me.fields) {
me.model = Ext.define('Ext.data.Store.ImplicitModel-' + (me.storeId || Ext.id()), {
extend: 'Ext.data.Model',
fields: me.fields,
proxy: me.proxy || me.defaultProxyType

delete me.fields;

me.implicitModel = true;

So you have to reconfigure the fields in the model. the best way to do it, is just construct a new array of fields (existing and new ones) and set these with the setFields method.

So you create a new set of fields in an array after you did that you set the array with setFields on the model of the store.

1 Jul 2013, 2:42 AM
yes, my problem is solved by assigning array to setfields(). Thanks a lot tvanzoelen!!