ExtJs 4: How do I hide/show grid columns on the fly?

一曲冷凌霜 提交于 2019-12-19 02:28:31

问题


I need to show/hide columns of a grid on the fly, but it seems that ExtJs 4 has no implemented method for that.

In previous versions I should use columnModel, what doesn't exist anymore.

Just get grid.columns[index] and hide() or show() doesn't affect the grid.

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).


回答1:


The following should work:

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);
        }
    }]
});



回答2:


Get access to "your grid" and then

yourGrid.columnManager.getColumns()[index].setVisible(false);

If required do -- EXT 4

parent.doLayout();

EXT 6

parent.updateLayout();



回答3:


The following should work:

Ext.getCmp('simpsons').down('[dataIndex=ColumnName]').setVisible(false);



回答4:


Ext.getCmp('gridId').columnManager.getColumns()[index].hide();
Ext.getCmp('gridId').doLayout();

This works for me



来源:https://stackoverflow.com/questions/20791685/extjs-4-how-do-i-hide-show-grid-columns-on-the-fly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!