Search code examples
javascriptreactjssortingag-grid

AG-Grid React: How do you collected the sort order of a grid?


How do you change the sort order of the columns and collect that info to save as user settings?
I was trying to use onSortedChanged but it is not working.

const onSortChanged = (event: SortChangedEvent) => {
    console.log(
      'onSortChanged',
      event.columnApi.getColumnState().filter((s) => s.sort)
    );
  };

I followed the documentation on the AG-Grid website but it seems out of date.


Solution

  • Figured out that if I call two different methods either will give me the current column state.

    const saveState = useCallback(() => 
         if (gridRef.current) {
            var value = gridRef.current.columnApi.getColumnState()
         }
    }, []);
    

    if you use these two built-in methods and call them on the grid this will give you the results I was looking for.

    onColumnResized = {saveState}
    onColumnMoved = {saveState}