Skip to content

Is there a way to always have it sorted by a column #2456

Answered by nstepien
vandyliu asked this question in Q&A
Discussion options

You must be logged in to vote

In the latest canary version you can do something like this:

const initialSort = [{ columnKey: 'abc', direction: 'ASC' }];

function MyComponent() {
  const [sortColumns, setSortColumns] = useState(initialSort);

  function onSortColumnsChange(newSortColumns) {
    if (newSortColumns.length === 0) {
      setSortColumns(initialSort);
    } else {
      setSortColumns(newSortColumns);
    }
  }

  return <DataGrid sortColumns={sortColumns} onSortColumnsChange={onSortColumnsChange} />;
}

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by nstepien
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2444 on June 11, 2021 08:32.