永久隐藏Ext JS网格的列

时间:2015-09-21 12:01:33

标签: javascript extjs extjs5

ExtJS 5

我正在使用ExtJs 5 Grid。我有一个按钮,当我点击它时,将使用下面的行隐藏年龄列。

Ext.getCmp('grdSample').columnManager.columns[2].setVisible(false);

我正在使用 listener - beforecellclick 来获取点击列的索引。但是当我点击最后一列(最后一列=隐藏列旁边)时,它仍然显示列的原始索引。隐藏的列仍然在网格中占据一席之地。

在CSS中 - 如果我们使用visibility:hidden,那么它会隐藏组件或标记,但仍会在网页中占用空间,但如果使用display:none,它会隐藏,也不会占用网页空间。

我希望隐藏列在获取当前单击列的索引时不应占用空间。 (不使用CSS)。

任何人都可以帮我解决这个问题。

Ext.onReady(function () {
    var studentStore = new Ext.data.JsonStore({
        autoLoad: true,
        pageSize: 10,
        fields: ['Name', 'Age', 'Fee'],
        data: {
            items: [
                { "Name": 'Puneet', "Age": '25', "Fee": '1000' },
                { "Name": 'Ankit', "Age": '23', "Fee": '2000' },
                { "Name": 'Rahul', "Age": '24', "Fee": '3000' }
            ]
        },
        proxy: {
            type: 'memory',
            reader: {
                type: 'json',
                rootProperty: 'items'
            }
        }
    });

        var window = new Ext.Window({
        id: 'grdWindow',
        width: 400,
        title: 'Grid Samples',
        items: [
            {
                xtype: 'panel',
                layout: 'fit',
                renderTo: Ext.getBody(),
                items: [
                    {
                        xtype: 'button',
                        text: 'hide age column',
                        handler: function () {
                            Ext.getCmp('grdSample').columnManager.columns[2].setVisible(false);
                        }
                    },
                    {
                        xtype: 'grid',
                        id: 'grdSample',
                        height: 300,
                        selModel: Ext.create('Ext.selection.CheckboxModel',
                        {
                        }),
                        store: studentStore,
                        columns: [
                            {
                                header: 'Name',
                                dataIndex: 'Name',
                            },
                            {
                                header: 'Age',
                                dataIndex: 'Age',
                            },
                            {
                                header: 'Fee',
                                dataIndex: 'Fee'
                            }
                        ],
                        listeners:{
                            beforecellclick: function (el, td, cellIndex, record, tr, rowIndex, e, eOpts) {
                                debugger;
                            }
                        },
                            dockedItems:
                                [   
                                    {
                                        xtype: 'pagingtoolbar',
                                        store:studentStore,
                                        dock:'bottom',
                                        displayInfo:true
                                    }
                                ]
                    }
                ]
            }
        ]
    });

2 个答案:

答案 0 :(得分:0)

    Ext.create('Ext.grid.Panel', {
        store: store,
        columns: [
            {text: "Name", flex : 1, dataIndex: 'Name'},
            {text: "Age", flex : 1, dataIndex: 'Age', id : 'colAge'},
            {text: "Fee", flex : 1, dataIndex: 'Fee'}
        ],
        listeners : {
             'cellclick' : function (me, td, cellIndex, record, tr, rowIndex, e, eOpts ) {
                   me.panel.headerCt.getHeaderAtIndex(cellIndex).dataIndex)}} // here you get the correct value :)
    });

这是工作小提琴:http://jsfiddle.net/Xpe9V/1623/

答案 1 :(得分:0)

也许是一种矫枉过正,但你可以创建一个列数组

var columns= [{dataIndex: 'Name', header: 'Name'}, { dataIndex:'Age', header: 
'Age' }, { dataIndex:'Fee', header: 'Fee'}] 

2)然后使用javascript splice方法从索引中删除数组 并重新配置你的网格

myGrid.reconfigure(myStore, columns);
相关问题