在ng-repeat中使用orderBy的自定义订单

时间:2022-12-02 16:50:23

I have objects like this:

我有这样的对象:

students = {name: 'Aa_Student', class: 'A_Class'},
           {name: 'Ab_Student', class: 'A_Class'},
           {name: 'Ac_Student', class: 'B_Class'},
           {name: 'Ba_Student', class: 'B_Class'},
           {name: 'Bb_Student', class: 'C_Class'},
           {name: 'Bc_Student', class: 'C_Class'}

Let's say the students object is shuffled. I use ng-repeat to show the data. I want to sort the objects in the custom order.

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

For example, I want to show the data like this:

例如,我想显示如下数据:

  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

So basically, I want to order by student's class, but it B_Class comes first, then A_Class, then C_Class. Also, I want to order by students name in alphabetic order. How can I do this?

基本上,我想按学生的班级排序,但是B_Class首先是A_Class,然后是C_Class。另外,我想按字母顺序按学生姓名订购。我怎样才能做到这一点?

HTML:

HTML:

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

Controller:

控制器:

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

    });
};

3 个解决方案

#1


19  

Hi you can create custom sort filter please see here http://jsbin.com/lizesuli/1/edit

您好您可以创建自定义排序筛选器,请参阅http://jsbin.com/lizesuli/1/edit

html:

HTML:

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

angularjs filter:

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;
  };
});

#2


10  

Know this is old but may come in handy for others...

知道这是旧的但可能会派上用场其他人...

You could also create a simple custom sort function. "Not quite a filter":

您还可以创建一个简单的自定义排序功能。 “不太过滤”:

$scope.customOrder = function (item) {
        switch (item) {
            case 'A_Class':
                return 2;

            case 'B_Class':
                return 1;

            case 'C_Class':
                return 3;
        }
    };

And then use like you wanted to:

然后使用你想要的:

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

#3


-1  

to set the orderBy as a property of the objects just quote that property name within the markup:

将orderBy设置为对象的属性只需引用标记中的属性名称:

ng-repeat="student in students |orderBy:'name' | orderBy:'class'"

DEMO

DEMO

#1


19  

Hi you can create custom sort filter please see here http://jsbin.com/lizesuli/1/edit

您好您可以创建自定义排序筛选器,请参阅http://jsbin.com/lizesuli/1/edit

html:

HTML:

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

angularjs filter:

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;
  };
});

#2


10  

Know this is old but may come in handy for others...

知道这是旧的但可能会派上用场其他人...

You could also create a simple custom sort function. "Not quite a filter":

您还可以创建一个简单的自定义排序功能。 “不太过滤”:

$scope.customOrder = function (item) {
        switch (item) {
            case 'A_Class':
                return 2;

            case 'B_Class':
                return 1;

            case 'C_Class':
                return 3;
        }
    };

And then use like you wanted to:

然后使用你想要的:

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

#3


-1  

to set the orderBy as a property of the objects just quote that property name within the markup:

将orderBy设置为对象的属性只需引用标记中的属性名称:

ng-repeat="student in students |orderBy:'name' | orderBy:'class'"

DEMO

DEMO