ExtJs 4:如何动态隐藏/显示网格列?
问题描述:
我需要即时显示/隐藏网格的列,但似乎ExtJs 4没有实现此目的的方法.
I need to show/hide columns of a grid on the fly, but it seems that ExtJs 4 has no implemented method for that.
在以前的版本中,我应该使用columnModel,不再存在.
In previous versions I should use columnModel, what doesn't exist anymore.
仅获取grid.columns[index]
和hide()
或show()
不会影响网格.
Just get grid.columns[index]
and hide()
or show()
doesn't affect the grid.
使用grid.columnManaget.getColumns()[index].hide()
可以真正隐藏该列,但无法再次显示(因为getColumns()
之后不会返回该列).
Use grid.columnManaget.getColumns()[index].hide()
can really hide the column, but it cannot be shown again (as getColumns()
does not return that column after that).
答
以下方法应该起作用:
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
id: 'simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' }
],
height: 200,
width: 400,
renderTo: Ext.getBody(),
dockedItems:[{
xtype:'button',
handler: function() {
if(Ext.getCmp('simpsons').columns[0].isVisible())
Ext.getCmp('simpsons').columns[0].setVisible(false);
else
Ext.getCmp('simpsons').columns[0].setVisible(true);
}
}]
});