2
votes

I'm facing a dilemma working with an Ext-JS GridPanel: the data used to load its store is dynamic so I don't know beforehand how many rows will need to be shown on the grid.

As such, I'm having a hard time with the grid's height: I've tried setting autoHeight to true but the grid will only display the first row, hiding the rest; when I set its height explicitly, white space appears on the grid if the number of rows doesn't fill the space specified by the height.

Ideally, the grid should expand/shrink vertically to show all of its rows. Is there any way to make the grid's height dynamic, based on the number of rows it'll contain?

I could wait for the grid to render, get a count of rows and recalculate the grid's height based on that but it seems hacky and I'm looking for something cleaner.

This is my code for reference:

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

Thanks for the help.

1

1 Answers

1
votes

In ExtJS 3 it's not working out-of-box, but it's easy to implement by extending 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 });
            }
        }
    }
);

Usage:

new Ext.grid.GridPanel({
    autoHeight: true,
    view: new AutoGridView()
});