Is there any function available in grid API or any other way to sort a column from script instead of clicking on the header?
1 Answer
If you want to turn on the auto-sort that clicking the header turns on through your script you can:
- Use
gridApi.grid.sortColumn()(see http://ui-grid.info/docs/#/api/ui.grid.class:Grid) You have to passsortColumn()a column object and a sort direction (either "asc" or "desc"). - After calling
sortColumn(), you will probably need to callgridApi.grid.notifyDataChange(uiGridConstants.dataChange.ALL);This is to refresh the presentation of data in the grid. Be sure to injectuiGridConstantsinto your controller/directive. - Of course, for the above two calls, you will probably have
gridApias a property on$scope
If, instead, you want to do a one time sort of the grid on a given column (so that the auto-sorting does not remain enabled), you will have to sort the gridOptions.data array and then call gridApi.grid.notifyDataChange(uiGridConstants.dataChange.ALL).
A library like underscore or lodash can really simplify the sorting of gridOptions.data.