質問

Let's assume I have the following data array in this form:

var data = [{group:GroupA, label: BB}, {group:GroupB, label: DD}.....].

My binding would be something like:

<select data-ng-options="c as c.label group by c.group for c in data"></select>

I would like the dropdown to list all the items with GroupA before GroupB while having them also sorted under each group.

Something like this:

GroupA
AA
BB
CC

GroupB
DD
EE
FF

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 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']

Here is a fiddle

他のヒント

It seems like orderBy would work exactly like you want.

Just take your expression and add the orderBy at the end:

c as c.label group by c.group for c in data | orderBy:'label'

This will first order the data array by the value of each item's label property, and then group them. Because the data will have been ordered correctly, each group will show up sorted correctly.

Here's a fiddle.

Note how the initial array is defined in backwards order, but is sorted correctly in each group in the select.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top