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

标签 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})
});

感谢您的帮助。

最佳答案

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

关于javascript - Ext-JS GridPanel 基于行数的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9021591/

相关文章:

javascript - Mongoose.js 使用异常但看似可接受的字符串值保存文档时出错

Javascript 二十一点牌组创建

javascript - 动态复选框、单选、复选框组、单选组 extjs

extjs - 如何将对象数组转换为模型对象数组

javascript - gmappanel 移动到标记坐标 - EXTJS 4

ExtJS 将组件移动到另一个布局

javascript - JS 匿名作用域

javascript - 如何获取通过函数 JavaScript/jQuery 传递的变量

javascript - 使用javascript将动态文本输入div

没有过期日期的 Javascript Cookie