Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setColumnDefs is not working for some ag-grids

I have several ag-grids in my code and the "setColumnDefs" is working for all the grids, however when I am trying to create a new grid, I am getting the error: Cannot read property 'setColumnDefs' of undefined. I don't know what I'm doing wrong. It seems to work for other ag-grids.

vm.newGrid = {
            enableSorting: true,
            enableColResize: true
        };

var newGridColumns = [
            {
                headerName: 'DATA',
                field: 'data',
            }, {
                headerName: 'PERCENT',
                field: 'percent'
            }
        ];

vm.newGrid.api.setColumnDefs(newGridColumns);
like image 678
Vedant Nighojkar Avatar asked Feb 08 '23 12:02

Vedant Nighojkar


1 Answers

you need to wait for the grid to be initialised before the api is ready.

so you have two choices: a) put the grids directly onto the gridOptions

vm.newGrid = {
  enableSorting: true,
  enableColResize: true,
  columnDefs: [
        {
            headerName: 'DATA',
            field: 'data',
        }, {
            headerName: 'PERCENT',
            field: 'percent'
        }
    ]
};

or b) wait for the grid to be initialised

vm.newGrid = {
  enableSorting: true,
  enableColResize: true,
  onGridReady: function(params) {
      params.api.setColumnDefs(newGridColumns);
  }
};
like image 95
Niall Crosby Avatar answered Feb 13 '23 05:02

Niall Crosby