我有同样的问题。状态只能在初始化期间而不是在初始化之后应用于组件。因此它第一次起作用,但之后不起作用。幸运的是,您使用的是 EextJs 3,以下代码在 ExtJS 3 中为我工作,但在 4 中没有。基本上,当您将新/其他状态应用于网格时,只需调用
MyGrid = Ext.extend(Ext.grid.GridPanel,
{
/* @private */
constructor: function(config)
{
MyGrid.superclass.constructor.call(this, config);
},
initComponent: function()
{
var cfg =
{
border: true,
bodyBorder: false,
stateful: true,
stateId: 'grid',
tbar:
[{
text: 'View name'
},{
xtype: 'combo',
width: 80,
typeAhead: true,
triggerAction: 'all',
mode: 'local',
forceSelection: true,
hiddenName: 'stateId',
selectOnFocus:true,
displayField:'name',
ref: '../stateCombo',
valueField: 'id',
listeners:
{
scope: this,
change: function(combo, newValue, oldValue)
{
newValue = parseInt(newValue, 10); var grd = this;
grd.el.mask('Applying the view please wait');
var data = combo.store.data.items;
Ext.each(data, function(d)
{
d.data.id = parseInt(d.data.id, 10);
if(d.data.id === newValue)
{
Ext.util.Cookies.set('reportStateName', d.data.machineName);
grd.applyState(MyStateProvider.decodeState(d.data.value));
var conf = grd.getColumnModel().config;
grd.getColumnModel().setConfig(conf);
grd.doLayout();
grd.getView().updateAllColumnWidths();
grd.getView().refresh(true);
return;
}
});
grd.el.unmask();
grd = null;
}
},
store: new Ext.data.JsonStore
({
fields:
[{
name: 'id',
type: 'int'
}],
proxy : new Ext.data.HttpProxy
({
method: 'GET',
url: BASEURL + 'getsavedstate'
}),
idProperty: 'id'
})
}]
};
//we use applyIf for configuration attributes that should be left configurable
Ext.applyIf(this, cfg);
//ensure that any extra configurations that are set in initComponent method are applied to initialConfig:
Ext.applyIf(this.initialConfig, cfg);
/*
* Properties specified here are safe/protected from being overridden
* by an instance, so any 'private' default properties might be specified here.
*/
//some attributes we may want to keep unaltered, so we use Ext.apply non-alterable configurables
cfg =
{
sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
stripeRows: true,
loadMask: true
};
Ext.apply(this, cfg);
//need this to work sometimes:
Ext.apply(this.initialConfig, cfg);
MyGrid.superclass.initComponent.apply(this, arguments);
this.on
({
afterrender: function(grid)
{
var grd = this;
this.stateCombo.getStore().load
({
callback: function(data, options, success)
{
grd.saveAsStateBtn.enable();
var applied = false;
var setStateValue = Ext.util.Cookies.get('reportStateName');
Ext.each(data, function(d)
{
//meaning no cookie was set so first time.
if(setStateValue === null)
{
if(d.data.machineName === 'default')
{
if(d.data.value !== '')
{
grd.applyState(MyStateProvider.decodeState(d.data.value));
applied = true;
grd.stateCombo.setValue(d.data.id);
return;
}
applied = true;
grd.stateCombo.setValue(d.data.id);//Here value should be fetched from cookie
return;
}
}
else//Cookie was set
{
if(d.data.machineName === setStateValue)
{
if(d.data.value !== '')
{
var s = MyStateProvider.decodeState(d.data.value);
grd.applyState(s);
applied = true;
grd.stateCombo.setValue(d.data.id);//Here value should be fetched from cookie
return;
}
else
{
return;
}
}
}
});
//grd.doLayout();
if(data.length !== 0)
{
grd.stateCombo.enable();
grd.saveStateBtn.enable();
}
var conf = grd.getColumnModel().config;
grd.getColumnModel().setConfig(conf);
grd.doLayout();
grd.getView().updateAllColumnWidths();
grd.getView().refresh(true);
grd.doLayout();
grd.el.unmask();
}
});
this.el.mask('Applying the saved view please wait');
},
scope: this
});
},
onRender: function (container, position)
{
MyGrid.superclass.onRender.apply(this, arguments);
var el = this.el;
var grd = this;
this.saveStateWindow = new MySaveStateWindow
({
grid: grd,
renderTo: el
});
},
// template method
/* @private */
initEvents: function()
{
MyGrid.superclass.initEvents.apply(this, arguments);
},
afterRender: function()
{
MyGrid.superclass.afterRender.apply(this, arguments);
},
// template method
/* @private */
beforeDestroy: function()
{
//Call parent
MyGrid.superclass.beforeDestroy.apply(this, arguments);
},
onDestroy: function()
{
this.saveStateWindow.destroy();
MyGrid.superclass.onDestroy.apply(this, arguments);
}
});