Simple way to programatically remove all grouping and filtering

Viewed 2904

When the grid loads there is no grouping/filtering applied. I want to be able to remove any grouping/filtering which the user has applied manually i.e. get the grid format back to its original state.

3 Answers

Here is how you can remove all filters and row groups. For more info, see GridApi.

gridApi.setFilterModel(null);
gridApi.setRowGroupColumns([]);
gridApi.onFilterChanged();

Live Example

Edit AgGrid - Remove Filter And Group

You can do this with help of gridOptions of ag-grid. Try to do the below changes..

Initialize gridOptions if not yet along with column definitions and set the grid options in ag-grid.

Component.ts

this.gridOptions = {
  defaultColDef: {
    editable: true,
    resizable: true,
    filter: true
  },
  columnDefs: this.columnDefs,
  rowData: this.rowData
};

clear the filters with like below

 ...
 gridOptions.api.setFilterModel(null);
 gridOptions.api.onFilterChanged();
 ...

component.html

<ag-grid ..  [gridOptions] = "gridOptions" ..> </ag-grid>

You can see more about this in the ag-grid documentation.

you can define a function to reset everything

 function ResetGrid(){
   //clear filters
 gridOptions.api.setFilterModel(null);
 //notify grid to implement the changes
 gridOptions.api.onFilterChanged();

 //remove all pivots
 gridOptions.columnApi.setPivotColumns([]);
// disable pivot mode
 gridOptions.columnApi.setPivotMode(false);
 //reset all grouping
 gridOptions.api.setColumnDefs(columnDefs);
//where columDefs is the object you used while creating grid first time.
  }    

the above method does what you want but more sophisticated way to do this will be saving column state(it may be at iniital stage or later after certain operation).

    function saveState() {
     window.colState = gridOptions.columnApi.getColumnState();
     window.groupState = gridOptions.columnApi.getColumnGroupState();
     window.sortState = gridOptions.api.getSortModel();
     window.filterState = gridOptions.api.getFilterModel();
     console.log('column state saved');
      }

    function restoreState() {
      if (!window.colState) {
        console.log('no columns state to restore by, you must save state first');
        return;
      }
      gridOptions.columnApi.setColumnState(window.colState);
      gridOptions.columnApi.setColumnGroupState(window.groupState);
      gridOptions.api.setSortModel(window.sortState);
      gridOptions.api.setFilterModel(window.filterState);
      console.log('column state restored');
    }

    function resetState() {
      gridOptions.columnApi.resetColumnState();
      gridOptions.columnApi.resetColumnGroupState();
      gridOptions.api.setSortModel(null);
      gridOptions.api.setFilterModel(null);
      console.log('column state reset');
    }

here is a demo

Related