我在下面的海报中有关于ng-grid中sortFn的相同问题: Sort on underlying data in ng-grid 答案给出了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;
}
}
}]
};
但是,它没有改变列表队列,在我看来,列表将是&#39;二十七,四十三,五十,七十&#39;正确? 如果没有,我如何通过sortFn关键字对ngGrid进行排序?全部,谢谢。
答案 0 :(得分:0)
如果你看一下这个叉子:I've modified it to have the age column as the default sort field and set the direction to ascending。
这需要设置为一个选项才能使其生效:
sortInfo: { fields: ['age'], directions: ['asc'] }