5

I am using ag-grid in my project and I have problem with applying state of table. Here is my code fragment from onGridReady function:

 this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;

    if (!window.colState) {
        console.log("no columns state to restore by, you must save state first");
    } else {
        this.gridColumnApi.applyColumnState({
            state: window.colState,
            applyOrder: true,
        });
        console.log("column state restored");
    }

It is taken from ag-grid of course. Right under it I have this.gridColumnApi.getAllColumns() and this.gridColumnApi.autoSizeColumns() functions that works fine.

The problem is that this.gridColumnApi.applyColumnState gives me this error.

TypeError: _this.gridColumnApi.applyColumnState is not a function

I cannot understand why other functions called on this.gridColumnApi works but applyColumnState doesn't. Any suggestions?

@edit I even tried this one:

componentWillUnmount() {
    window.colState = this.props.gridColumnApi.getColumnState();
    console.log("column state saved");
    if (this.props.gridColumnApi.applyColumnState) {
        devLog("IT IS HERE");
    } else {
        devLog("its not");
    }
    this.gridColumnApi.resetColumnState();
    console.log("column state reset");
}

but I get applyColumnState and resetColumnState is not a function errors, what is wrong here?

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
Sowam
  • 1,674
  • 3
  • 12
  • 30

2 Answers2

13

Check your AgGrid version. ColumnApi.applyColumnState() was added in version 24.0.0.

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
  • 1
    yeah you are right, I had to update AgGrid to version 24.0.0, thank you so much! :) problem solved – Sowam Sep 18 '20 at 16:58
5

If you are stuck with a lower ag-grid version (< 24.0.0), then use gridOptions.columnApi.setColumnState(prevState)

With ag-grid version 24.0.0 and above you can use -

gridOptions.columnApi.applyColumnState({
         state:prevState,
         applyOrder: true,
       });
Sarun
  • 161
  • 1
  • 8
  • Just added for anyone who is using a lower version. I was facing the same issue, and was using version 23.2 – Sarun Apr 21 '21 at 13:21