【问题标题】:Ext-JS GridPanel Dynamic Height Based on Row CountExt-JS GridPanel 基于行数的动态高度
【发布时间】:2012-01-26 16:35:31
【问题描述】:

我在使用 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})
});

感谢您的帮助。

【问题讨论】:

    标签: javascript extjs


    【解决方案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()
    });
    

    【讨论】:

    • 应该是AutoGridView,而不是sm.GridView。我已经更正了我的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多