How do you update ng-grid gridOptions.sortInfo?


I am trying to programmatically update the gridOptions.sortInfo on the Angular grid ng-grid, but I can't get it working.

I have both "name" and "age" columns. I am initially setting the sort on the "name" column but would like load some new data and then update it to sort on "age". (Programmatically, not just by clicking the column header).

I can set the $scope.gridOptions.sortInfo to a new value, but the grid does not reflect this. What is the correct way to update the gridOptions.sortInfo ?

Please see plunker:

EDIT: Please note I would like to be able to update the sort direction i.e. ascending/descending as well as the actual column to sort on.

Many thanks

Problem courtesy of: EdL


Yes, according to source code (line number 128 here link) you should be able to just do like this:

$scope.updateSortInfo = function() {

The plunker shows that it works. I forked it here.

Here's another version with sortColumn passed in from a text box: plunker.

Solution courtesy of: marko


Look at this:

Search for this text:

sort An object of sort information, attributes are:

I needed to sort by the id column at the beginning in desc order and that did the trick for me:

var columnsDef = [
            {name:'id',width:50,sort: {
                direction: 'desc',
                priority: 0
$scope.theGrid.columnDefs = columnsDef;
Discussion courtesy of: Natan Rubinstein

Turns out if you call $scope.gridOptions.sortBy() twice on the same column, it will sort in descending order.

Discussion courtesy of: Mitch Penrod

This recipe can be found in it's original form on Stack Over Flow.