0

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 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.