基于行计数的Ext-JS GridPanel动态高度

时间:2012-01-26 16:35:31

标签: javascript extjs

我正面临使用Ext-JS 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})
});

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在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()
});