Kedar Udupa
Kedar Udupa

Reputation: 554

Ag-grid save/restore filter state not working

I've been using Ag-grid in my application, and I was experimenting with saving and restoring table filters. ( I'm not-using the enterprise edition of Ag-Grid ).

I found this link where it says how I can save/restore filters

I'm trying to store the filter state and then hit a URL from which table is updated and then restore the filters

this is my code where I save and restore the filter state

 saveState() {
         window.colState = this.tableOptions.columnApi.getColumnState();
         window.groupState = this.tableOptions.columnApi.getColumnGroupState();
         window.sortState = this.tableOptions.api.getSortModel();
         window.filterState = this.tableOptions.api.getFilterModel();
    }

 restoreState() {
         this.tableOptions.columnApi.setColumnState(window.colState);
         this.tableOptions.columnApi.setColumnGroupState(window.groupState);
         this.tableOptions.api.setSortModel(window.sortState);
         this.tableOptions.api.setFilterModel(window.filterState);
  }

But, I'm not able to restore to the saved state after I refresh the table data. I tried calling onFilterChanged() which didn't seem to do the job for me.

My code looks something like this

Need help. Thank you

Upvotes: 3

Views: 8855

Answers (1)

un.spike
un.spike

Reputation: 5113

According your sample:

saveState() {
    window.colState = this.gridColumnApi.getColumnState();
    window.groupState = this.gridColumnApi.getColumnGroupState();
    window.sortState = this.gridApi.getSortModel();
    window.filterState = this.gridApi.getFilterModel();
    this.http
      .get(
        "https://raw.githubusercontent.com/ag-grid/ag-grid/master/packages/ag-grid-docs/src/olympicWinnersSmall.json"
      )
      .subscribe(data => {
        this.rowData = data;
        //... restore the state here ...
        setTimeout(()=>{
            this.restoreState();
        }, 100)
      });
}

You can ask why the ag-grid doesn't keep current state if rowData is changed.

It happened, cuz you are not updating existing data, you are just replacing the current.

From the description

setRowData(newRowData) is equal of rowData = newRowData

setRowData(rows) Set new rows into the grid.

To achieve state-saving on update operations you should use updateRowData method

Upvotes: 3

Related Questions