4

So I have a Grid setup that works with the Enterprise Row Model. The columns are very dynamic and so the column defs are not known until the first query for rows is made to the server. This all works fine, but how can I set a default sort state when the column defs are not set until after the request has succeeded?

Alan Dragicevich
  • 391
  • 2
  • 5
  • 13

3 Answers3

7

Once the grid has been set-up with the column defs you can just set the sort on any column

gridOptions.columnApi.getColumn(COLUMN_NAME).setSort("asc")
Jonathan Naim
  • 229
  • 1
  • 7
  • Sorry maybe I wasn't clear enough. My server query also responds with the column definitions. So basically I make a query to get row data from the server, the server responds with the row data as well as the columns. And now I have the columns so I have to make a second request for the same data but with a sort model set. But what I want is to set a sort on a column that doesn't even exist yet :/ So I can avoid making two requests. – Alan Dragicevich Aug 24 '17 at 22:42
  • I think I understood your issue but correct me if I'm wrong! When it calls GetRows for the first time there isn't a SortModel on the IEnterpriseGetRowsParams because you don't already know the columns that will be returned then you make a second request with the SortModel set so you get the sorted data? Just to be sure how do you define the column to be sorted on? Is that the user clicking on the header or there is a programmatic way of finding it? (That's the missing link in my head....) – Jonathan Naim Aug 25 '17 at 08:46
  • Sorry I should have explained that as well. Ok so when the user modifies the grid state (i.e. change sort model, change width/order of columns) we save what they did so next time they view that table we can set that saved column/sortmodel state. So we need to set the sortmodel from the saved GridState before the first initial request. – Alan Dragicevich Aug 26 '17 at 09:49
  • Sorry I've been away on holidays.... From what I remember, the sortModel property of the IEnterpriseGetRowsRequest is built by the sortController by looking at the sort state of each column. Obviously for you that doesn't work since you haven't got the data yet.... So this is me trying to find a workaround and I'm unsure if that works.... If you override the method getSortModel from the SortController to return your persisted sortModel if there aren't any columns in the schema that might work? – Jonathan Naim Sep 14 '17 at 09:24
  • Exactly what I needed. Thanks so much! – kohane15 Nov 18 '20 at 02:55
  • https://stackoverflow.com/a/65257834/470749 is how I sorted onGridReady – Ryan Dec 11 '20 at 19:59
2

Adding a sort attribute to your colDef works too.

Example:

const columnDefs = [
  {
    headerName: 'Created Date',
    field: 'CreateDate',
    sort: 'desc',
    sortingOrder: ['desc','asc'] //optional but for better sorting behaviour
  }
]
kenjigoh
  • 39
  • 4
0

Try this

const sort = [
  {
    colId: "firstName",
    sort: "asc",
  },
  {
    colId: "lastName"
  },
];

this.gridApi.setSortModel(sort);
gowtham ks
  • 427
  • 5
  • 6