Javascript Angular ng-repeat groupBy 和保持顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25921427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular ng-repeat groupBy and Keep order
提问by Harry
Im using this filter https://github.com/a8m/angular-filter#groupbyto order my data like so, and it works great:
我使用这个过滤器https://github.com/a8m/angular-filter#groupby像这样对我的数据进行排序,而且效果很好:
<div ng-repeat="(key, value) in tags.tags.objects | groupBy:'category.name' ">
Now Im trying to keep the order of that groups, by category.order.
现在我试图保持这些组的顺序,按category.order。
Is this possible?
这可能吗?
I tried piping it like so:
我试着像这样管道它:
<div ng-repeat="(key, value) in tags.tags.objects | groupBy:'category.name' | orderBy:'category.order' ">
But it does not make any difference
但它没有任何区别
回答by a8m
orderByfilter does not work with objects in ngRepeat. So, what you can do is something like this:
orderBy过滤器不适用于ngRepeat. 所以,你可以做的是这样的:
<!--
Note: toArray filter also attaches a new property $key
to the value containing the original key that was used in the object.
-->
<div ng-repeat="tags in tagsList | groupBy:'prop' | toArray:true | orderBy:'$key'">
Group name: {{ tags.$key }}
<p ng-repeat="tag in tags | orderBy:'prop'">
{{ tag.name }}
</p>
</div>
See: toArrayfilter
请参阅:toArray过滤器
回答by Sijan Gurung
groupBy should always be the last one in the ng-repeat.. so set orderBy first and groupBy last and then you can use track by $index so the normal ng-repeat will be.
groupBy 应该始终是 ng-repeat 中的最后一个..所以首先设置 orderBy 和 groupBy 最后然后你可以使用 track by $index 所以正常的 ng-repeat 将是。
ng-repeat="item in items orderBy:'price' | groupBy:'name' track by $index"
... Hope it helps even its little late!
...希望它即使有点晚也能有所帮助!
回答by r0m4n
To partially of djsiz's answer (track by isn't necessary and it was missing a pipe), you could do this:
对于 djsiz 的部分答案(不需要跟踪并且缺少管道),您可以这样做:
<div ng-repeat="(key, value) in tags.tags.objects | orderBy:'category.order' | groupBy:'category.name'">
The groupBy creates an object but the orderBy needs an array... if you orderBy before you group, it should sort properly
groupBy 创建一个对象,但 orderBy 需要一个数组...如果在分组之前 orderBy,它应该正确排序

