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

4

7

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

Nickell answered 26/12, 2013 at 21:46 Comment(0)
P
11

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);
        }
    }]
});
Peaked answered 26/12, 2013 at 23:15 Comment(2)
For some reason this is not working for me, so I had to use grid.columnManager.headerCt.items.get(0).setVisible(false);Nickell
I can confirm grid.columns[n].setVisible(true | false) to work in Ext JS 4.2.1Kishakishinev
Q
4

The following should work:

Ext.getCmp('simpsons').down('[dataIndex=ColumnName]').setVisible(false);
Quadroon answered 26/10, 2015 at 5:42 Comment(0)
C
3

Get access to "your grid" and then

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

If required do -- EXT 4

parent.doLayout();

EXT 6

parent.updateLayout();
Colure answered 3/8, 2016 at 0:13 Comment(0)
X
0
Ext.getCmp('gridId').columnManager.getColumns()[index].hide();
Ext.getCmp('gridId').doLayout();

This works for me

Xanthus answered 29/10, 2018 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.