BiJ
BiJ

Reputation: 1689

Angular UI grid sort a column through script

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

Answers (1)

Dan J Miller
Dan J Miller

Reputation: 118

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 pass sortColumn() a column object and a sort direction (either "asc" or "desc").
  • After calling 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.
  • Of course, for the above two calls, you will probably have 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

Related Questions