Ag-grid: gridReady get's called before data is bound

Created on 12 Dec 2016  路  3Comments  路  Source: ag-grid/ag-grid

according to documentation:

gridReady
ag-Grid has initialised. The name 'ready' was influenced by the authors time programming the Commodore 64. Use this event if, for example, you need to use the grid's API to fix the columns to size.

so I added auto sizing logic in there:

            onGridReady: e => {
                var allColumnIds = [];
                columndefs.forEach(function (columnDef) {
                    allColumnIds.push(columnDef.field);
                });
                e.columnApi.autoSizeColumns(allColumnIds);
                console.log('the grid is now ready', e)
          }

this does not work well I think because this event fires before data is bound. So grid doesn't have enough information on how to auto size the columns. I am thinking about maybe calling this logic on model updated ?

Most helpful comment

got it,

so in the latter scenario which event can i use to call column autosize? seems whatever i try calls it too early because columns are not sized properly until i do it one more time manually by clicking a button after data has loaded.

All 3 comments

if your data is set on the gridOptions, then the above is true.

however if you are using a framework to bind the data (eg passing it in as an Angular bound property) then it's up to the framework in what order it creates the grid and binds the properties.

got it,

so in the latter scenario which event can i use to call column autosize? seems whatever i try calls it too early because columns are not sized properly until i do it one more time manually by clicking a button after data has loaded.

depends on your setup. something you can try is putting it in a timeout. eg:

onGridReady: e => {
  setTimeout( () => {
    e.columnApi.autoSizeAllColumns();
  }, 0);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DominicTobias picture DominicTobias  路  3Comments

pupeno picture pupeno  路  3Comments

deepakjan picture deepakjan  路  3Comments

rboughani picture rboughani  路  3Comments

simkessy picture simkessy  路  3Comments