【问题标题】:Sort on underlying data in ng-grid_对 ng-grid_ 中的基础数据进行排序
【发布时间】:2014-08-19 18:38:26
【问题描述】:

我对下面关于 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;
       }
     }
       }]
};

但是,它并没有改变列表队列,在我看来,列表应该是“二十七、四十三、五十、七十”对吗? 如果没有,我如何通过 sortFn 关键字对 ngGrid 进行排序?全部,谢谢。

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    如果你看看这个分叉: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'] }
    

    【讨论】:

    • 谢谢,我已经知道 sortInfo 在 ngGrid 中运行良好,但这里的排序键是一个对象,我认为它不适合;
    • 对象键排序与直接值排序一样工作:)
    猜你喜欢
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2014-06-02
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    相关资源
    最近更新 更多