簡體   English   中英

基於行數的Ext-JS GridPanel動態高度

[英]Ext-JS GridPanel Dynamic Height Based on Row Count

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM