angularjs – 使用orderBy在ng重复中自定义顺序

前端之家收集整理的这篇文章主要介绍了angularjs – 使用orderBy在ng重复中自定义顺序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这样的对象:
students = {name: 'Aa_Student',class: 'A_Class'},{name: 'Ab_Student',{name: 'Ac_Student',class: 'B_Class'},{name: 'Ba_Student',{name: 'Bb_Student',class: 'C_Class'},{name: 'Bc_Student',class: 'C_Class'}

让我们说学生的对象是洗牌的.我使用ng-repeat来显示数据.我想按照自定义顺序排列对象.

例如,我想显示这样的数据:

Name              Class
-----------------------------
Ac_Student         B_Class
Ba_Student         B_Class
Aa_Student         A_Class
Ab_Student         A_Class
Bb_Student         C_Class
Bc_Student         C_Class

所以基本上,我想按学生的班级排序,但是B_Class先到,然后是A_Class,然后是C_Class.此外,我想按学生姓名按字母顺序排序.我该怎么做?

HTML:

<table>
    <tr ng-repeat="student in students | orderBy:customOrder">
    ...
    </tr>
</table>

控制器:

$scope.customOrder = function(student) {
    $scope.students = $filter('orderBy')(student,function() {

    });
};
嗨,你可以创建自定义排序过滤器请看这里 http://jsbin.com/lizesuli/1/edit

HTML:

<p ng-repeat="s in students |customSorter:'class'">{{s.name}} - {{s.class}} </p>
      </div>

angularjs过滤器:

app.filter('customSorter',function() {

  function CustomOrder(item) {
    switch(item) {
      case 'A_Class':
        return 2;

      case 'B_Class':
        return 1;

      case 'C_Class':
        return 3;
    }  
  }

  return function(items,field) {
    var filtered = [];
    angular.forEach(items,function(item) {
      filtered.push(item);
    });
    filtered.sort(function (a,b) {    
      return (CustomOrder(a.class) > CustomOrder(b.class) ? 1 : -1);
    });
    return filtered;
  };
});
原文链接:https://www.f2er.com/angularjs/143207.html

猜你在找的Angularjs相关文章