开发者

Automatically hide "empty" columns in ExtJS grid Panel

Is there a plugin for the ExtJS Grid that aut开发者_JAVA技巧omatically hides "empty" columns?

A column should be deemed "empty" if the value of the mapped field for all Records in the underlying store matches a given "emptiness" criteria (a given value or, better, a function).

Run-time add/remove/update operations on the underlying store should hide/un-hide columns accordingly.

Thanks!


I had to do something similar to this... here is a "Hiding Column Model" that will hide/show columns based on the return value of the "fieldHasData" method... it is probably a close start to what you were asking

Ext.ux.grid.HidingColumnModel = function() {

    var Class = Ext.extend(Ext.grid.ColumnModel, {        
        constructor:function(config) {
            Class.superclass.constructor.call(this, config);
        },

        onGridStoreLoad:function(store, records, options) {
            store.fields.each(function(item, index, length) {
                var colIndex = this.findColumnIndex(item.name);
                if (colIndex >= 0) {
                    this.setHidden(colIndex, !this.fieldHasData(item.name, records));
                }
            }, this);
        },

        fieldHasData:function(field, records) {
            var hasData = false;
            Ext.each(Ext.pluck(records, "data"), function(item, index, allItems) {
                if (item[field]) {
                    hasData = true;
                }
            });
            return hasData;
        }
    });

    return Class;
}();

And then in your grid... do add the listener on the column model

var columnModel = new Ext.ux.grid.HidingColumnModel(),
    store = ... {create your store},
    gridPanel = new Ext.grid.GridPanel({
        ...
        store:store,
        columnModel:columnModel,
        ...
    });

store.on('load', columnModel.onGridStoreLoad, columnModel);


Since I could not find a similar plugin anywhere, I just implemented it myself :)

The code is on the extjs/sencha forums here:

http://www.sencha.com/forum/showthread.php?140685-Grid-Plugin-dynamically-hiding-columns-matching-quot-emptiness-quot-criteria&p=626670#post626670

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜