Reputation: 1689
Is there any function available in grid API or any other way to sort a column from script instead of clicking on the header?
Upvotes: 0
Views: 3367
Reputation: 118
If you want to turn on the auto-sort that clicking the header turns on through your script you can:
gridApi.grid.sortColumn()
(see http://ui-grid.info/docs/#/api/ui.grid.class:Grid) You have to pass sortColumn()
a column object and a sort direction (either "asc" or "desc").sortColumn()
, you will probably need to call gridApi.grid.notifyDataChange(uiGridConstants.dataChange.ALL);
This is to refresh the presentation of data in the grid. Be sure to inject uiGridConstants
into your controller/directive.gridApi
as 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
.
Upvotes: 2