2012-01-26 56 views
2

我面臨着一個使用Ext-JS GridPanel的困境:用於加載其存儲的數據是動態的,因此事先並不知道需要多少行顯示在網格上。因此,我在網格的高度方面遇到困難:我嘗試將autoHeight設置爲true,但網格只顯示第一行,隱藏其餘的一行;當我明確設置其高度時,如果行數未填充高度指定的空間,則網格上會出現空白區域。基於行計數的Ext-JS GridPanel動態高度

理想情況下,網格應垂直展開/縮小以顯示其所有行。 有沒有辦法根據它所包含的行數來使網格的高度動態化?

我可以等待網格渲染,獲得行數並重新計算網格的高度,但它看起來很詭異,我正在尋找更乾淨的東西。

這是我的代碼以供參考:

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

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

SM是不確定的,你有一個工作版本太? –

+0

而不是'sm.GridView'應該有'AutoGridView'。我更正了我的代碼。 – Krzysztof