AngularJS - ngOptions:如何按组名然后按标签排序 [英] AngularJS - ngOptions: How to order by Group Name and then by Label
问题描述
假设我有以下这种形式的数据数组:
Let's assume I have the following data array in this form:
var data = [{group:GroupA, label: BB}, {group:GroupB, label: DD}.....].
我的绑定类似于:
<select data-ng-options="c as c.label group by c.group for c in data"></select>
我希望下拉列表在 GroupB
之前列出带有 GroupA
的所有项目,同时让它们也排序在每个组下.
I would like the dropdown to list all the items with GroupA
before GroupB
while having them also sorted under each group.
像这样:
GroupA
AA
BB
CC
GroupB
DD
EE
FF
我知道我可以使用 orderBy
Angular 过滤器,但这并不是我真正需要的方式.我的猜测是我必须编写一个自定义过滤器,以我想要的方式手动对列表进行排序,但我想知道是否有更简单的方法来完成任务.
I know I can use the orderBy
Angular filter, but that doesn't really work the way I need. My guess is I have to write a custom filter that manually orders the list the way I want, but I was wondering if there is an easier way to accomplish the task.
推荐答案
orderBy
可以接受多个参数的数组进行排序.所以你可以这样做:
orderBy
can take an array of multiple parameters to order by. So you can do:
c as c.label group by c.group for c in data | orderBy:['group','label']
这篇关于AngularJS - ngOptions:如何按组名然后按标签排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!