2

I have the same question with the below poster about sortFn in ng-grid: Sort on underlying data in ng-grid the answer give the code in Plunker: http://plnkr.co/edit/qmBsneZ3HmFRKSkjbBWU?p=preview

 var myData1 = [{name: "Moroni", age: {age: 50, ageWord: "Fifty"}},
                 {name: "Tiancum", age: {age: 43, ageWord: "Forty-Three"}},
                 {name: "Mildred", age: {age: 70, ageWord: "Seventy"}},
                 {name: "Jacob", age: {age: 27, ageWord: "Twenty-Seven"}}];

 $scope.gridOptions = { 
  data: 'gridData',
   columnDefs: [
    {field: 'name', displayName: 'Name'}, 
    {field:'age', 
     displayName:'Age',
     cellTemplate: '<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{COL_FIELD.ageWord}}</span></div>',
     sortFn: function (a, b) {
       if (a.age < b.age) {
         return -1;
       }
       else if (a.age > b.age) {
         return 1;
       }
       else {
         return 0;
       }
     }
       }]
};

But, it did not change the list queue, in my opinion, the list would be 'Twenty-Seven,Forty-Three,Fifty,Seventy' correct? If not, how can i sort the ngGrid by sortFn keyword? All, thanks.

Community
  • 1
  • 1
Chopper Lee
  • 1,557
  • 2
  • 11
  • 30

1 Answers1

0

If you take a look at this fork: I've modified it to have the age column as the default sort field and set the direction to ascending.

This needs to be set as an option in order for this to take effect:

sortInfo: { fields: ['age'], directions: ['asc'] }
SixteenStudio
  • 1,016
  • 10
  • 24