问题描述
我用角过滤器在我的项目当我使用的语法像这样逐页输出对象排序的问题是:
I use angular-filter in my project to sort the output objects by page the problem is when I use syntax like this:
<ul class="catalog-list"
ng-repeat="(key, value) in list | groupBy: 'styl' | toArray | orderBy:'page'">
{{key}}
<li ng-repeat="dziecko in value | filter:search | bookmark:search"
ng-class="{active:isActiveTab(dziecko)}"
ng-click="openItem(dziecko)">
{{dziecko.rodzina}}
<b>{{dziecko.page}}</b>
</li>
</ul>
角转换'STYL的属性['nowoczesny','klasyczny'..]为数字。排序工作正常,但我想获得名字而不是数字。
Angular converts 'styl' properties ['nowoczesny','klasyczny'..] to numbers. Sorting works fine but I want to obtain names instead of numbers.
推荐答案
GROUPBY
返回一个对象,而排序依据
预计数组作为参数,所以这是你应该使用的toArray
过滤器的原因。
groupBy
return an object, and orderBy
expect array as an argument, so that's the reason you should use toArray
filter.
的toArray
工作,这样的:结果
用法: 对象|的toArray:addKey [可选]
结果
如果addKey设置为true,该过滤器还附加了一个新的属性 $键
来包含在该对象使用的原始键的值,我们遍历引用属性
toArray
work like that:
Usage: object | toArray: addKey[optional]
if addKey set to true, the filter also attaches a new property $key
to the value containing the original key that was used in the object we are iterating over to reference the property
所以,你可以做这样的事情示例,或者乘坐的
So, you can do something like this example, or take a look on the jsbin
JS:
$scope.groups = [
{ category: 'alpha', id: 2 },
{ category: 'beta', id: 3 },
{ category: 'gamma', id: 0 },
{ category: 'alpha', id: 4 },
{ category: 'beta', id: 5 },
{ category: 'gamma', id: 1 }
];
HTML:
<ul ng-repeat="group in groups | groupBy:'category' | toArray:true | orderBy:min">
<!-- print the group name -->
<li>{{ group.$key }}</li>
<!-- iterate over the group members and order each group by id -->
<li ng-repeat="item in group | orderBy:'id'">
{{ item }}
</li>
</ul>
结果:
- 字母
- {类别:阿尔法,ID:2}
-
{类别:阿尔法,ID:4}
- alpha
- {"category":"alpha","id":2}
{"category":"alpha","id":4}
测试版
{类别:测试版,ID:5}
{"category":"beta","id":5}
伽玛
这篇关于AngularJS转换的toArray对象键为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!