When my ag-grid is initialized, it sorts the data by a default column indicated by this.options.defaultSortColumn
. If a user then sorts by another column and removes the sort (by clicking on the header three times), I want to revert back to the default sort and sort by the default column again. Here's what I have tried:
postSort () {
if (this.gridApi) {
let currentSort = this.gridApi.getSortModel()
console.log(currentSort)
if (this.options.defaultSortColumn && currentSort && currentSort.length === 0) {
// reset default sort if no other sort is active
this.gridApi.setSortModel({
colId: this.options.defaultSortColumn,
sort: (this.options.defaultSortDir || 'asc').toLowerCase()
})
}
}
}
However, I keep receiving a Maximum call stack size exceeded
error. It seems like the function calls itself repeatedly because it sets the sort and triggers the postSort
event before the getSortModel()
updates. Am I making a mistake here? Are there alternate methods to restore the default sort when no other column is sorted?