我有这样的对象:
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
原文链接:https://www.f2er.com/angularjs/143207.htmlHTML:
<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; }; });