Angularjs排序依据的NG-重复不起作用 [英] Angularjs OrderBy on ng-repeat doesn't work
问题描述
我想使用AngularJS我的第一个项目(一个赛事经理)和 NG-重复$ c中的
排序依据
过滤器$ C>不工作:(我读过所有关于那个的文档,但没有做:/
I'm trying to use AngularJS for my first project (a tournaments manager) and the orderBy
filter on ng-repeat
doesn't work :( I have read all the documentation about that, but nothing to do :/
所以,我对 $范围定义瓦尔
这样的:
$scope.order_item = "count_win";
$scope.order_reverse = false;
$scope.teams = {
100 : {
id: 100,
name: "XXX",
count_win: 1,
count_loose: 2,
goal_average: 1,
},
200 : {
id: 200,
name: "XXX",
count_win: 1,
count_loose: 2,
goal_average: 1,
},
[...]
};
现在,在我看来我试图重新排序(第一只有一个订单项目),但从来没有工作...
Now, on my view i'm trying to reorder (first with only one order item) but never work...
<tr ng-repeat="team in teams | orderBy:order_item:order_reverse">
<td>{{team.name}}</td>
<td>{{team.count_loose}}</td>
<td>{{team.goal_average}}</td>
</tr>
第二次,我想从2条信息来重新排序: count_win
和 goal_average
如果第一个是平等的..我尝试更换 $ scope.order_item
这样的,但如果有一个code没有工作,他绝不会与2工作...
The second time, I want to reorder from 2 pieces of information: count_win
and goal_average
if first are equal.. I try to replace $scope.order_item
like that, but if with one the code doesn't work, he'll never work with 2...
$scope.order_item = ['count_win','goal_average'];
感谢大家的阅读和遗憾的投递大小。
Thank you all for reading and sorry for the post size.
推荐答案
$ scope.teams
不是一个数组(这是对象的对象),以及排序依据
过滤仅适用于数组。如果你 $ scope.teams
一个数组,它将工作:
$scope.teams
isn't an array (it's an object of objects), and the orderBy
filter only works with arrays. If you make $scope.teams
an array, it will work:
$scope.teams = [
{
id: 100,
name: "team1",
count_win: 3,
count_loose: 2,
goal_average: 2,
},
{
id: 200,
name: "team2",
count_win: 3,
count_loose: 2,
goal_average: 1,
},
{
id: 300,
name: "team3",
count_win: 1,
count_loose: 2,
goal_average: 1,
}
];
或者,您可以添加一个特殊的过滤器上对象的作品,像这样(从这里借来的>)
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
和使用这样的:
<tr ng-repeat="team in teams | orderObjectBy:order_item:order_reverse">
请注意,这个自定义过滤器不会排序顺序数组作为你问题的第二部分工作。
Note that this custom filter will not work with an array of sort orders as in the second part of your question.
这篇关于Angularjs排序依据的NG-重复不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!