ExtJS GridPanel基于行计数的动态高度



我在使用ExtJS GridPanel时面临着一个难题:用于加载其存储的数据是动态的,所以我事先不知道需要在网格上显示多少行。

因此,我很难处理网格的高度:我曾尝试将autoHeight设置为true,但网格只显示第一行,隐藏其余行;当我明确设置它的高度时,如果行数没有填满由高度指定的空间,就会在网格上显示空白。

理想情况下,网格应垂直展开/收缩以显示其所有行。有没有任何方法可以根据网格包含的行数使其高度动态?

我可以等待网格渲染,获得行数,然后根据行数重新计算网格的高度,但这似乎很麻烦,我正在寻找更干净的东西。

这是我的参考代码:

var store = new Ext.data.ArrayStore({fields:[{name: 'sign_up_date'}, {name: 'business_name'}, {name: 'owner_name'}, {name: 'status'}]});
// buildResultsArray is a method that returns arrays of varying lengths based on some business logic. The arrays can contain no elements or up to 15
store.loadData(buildResultsArray()); 
var resultsGrid = new Ext.grid.GridPanel({
    store: store,
    columns: [
        {id: "sign_up_date", header: "Sign Up Date", dataIndex: "sign_up_date", width: 70}, 
        {id: "business_name", header: "Business Name", dataIndex: "business_name", width: 100}, 
        {id: "owner_name",header: "Owner Name", dataIndex: "owner_name", width: 100},
        {id: "status", header: "Sign Up Status", dataIndex: "status", width: 70}
    ],
    stripeRows: true,
    columnLines: true,
    enableColumnHide: false,
    enableColumnMove: false,
    enableHdMenu: false,
    id: "results_grid",
    renderTo: "results_grid_div", 
    //height: 300,
    autoHeight: true, 
    selModel: new Ext.grid.RowSelectionModel({singleSelect: false})
});

谢谢你的帮助。

在ExtJS 3中,它不是开箱即用的,但通过扩展GridView:很容易实现

AutoGridView = Ext.extend(
    Ext.grid.GridView,
    {
        fixOverflow: function() {
            if (this.grid.autoHeight === true || this.autoHeight === true){
                Ext.get(this.innerHd).setStyle("float", "none");
                this.scroller.setStyle("overflow-x", this.scroller.getWidth() < this.mainBody.getWidth() ? "scroll" : "auto");
            }
        },
        layout: function () {
            AutoGridView.superclass.layout.call(this);
            this.fixOverflow();
        },
        render: function(){
            AutoGridView.superclass.render.apply(this, arguments);
            this.scroller.on('resize', this.fixOverflow, this);
            if (this.grid.autoHeight === true || this.autoHeight === true){
                this.grid.getStore().on('datachanged', function(){
                    if (this.ownerCt) { this.ownerCt.doLayout(); }
                }, this.grid, { delay: 10 });
            }
        }
    }
);

用法:

new Ext.grid.GridPanel({
    autoHeight: true,
    view: new AutoGridView()
});

最新更新