最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to sort array in ng-options by key? - Stack Overflow

programmeradmin3浏览0评论

There is such array:

month: Array[13]0: "M"1: "January"2: "February"3: "March"4: "April"5: "May"6: "June"7: "July"8: "August"9: "September"10: "October"11: "November"12: "December"

I do:

ng-options="key as value for (key, value) in data.month | orderBy:key"

But I get unsorted select list.

There is such array:

month: Array[13]0: "M"1: "January"2: "February"3: "March"4: "April"5: "May"6: "June"7: "July"8: "August"9: "September"10: "October"11: "November"12: "December"

I do:

ng-options="key as value for (key, value) in data.month | orderBy:key"

But I get unsorted select list.

Share Improve this question edited Jun 20, 2015 at 18:31 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Jun 3, 2015 at 13:08 FaudFaud 4713 gold badges7 silver badges16 bronze badges 1
  • The array is already in calendar order, why are you sorting it? – tvanfosson Commented Jun 3, 2015 at 13:11
Add a comment  | 

3 Answers 3

Reset to default 17

In order to use tracking with filters use track by.

Markup

ng-options="key as value for (key, value) in data.month | orderBy:'key' track by key"

Update

This orderBy will never work because you are having literal array. You need to convert this array to JSON like object which would structured like [{id: 0, value: "M"}, {id: 1, value: "January"},......]

HTML

ng-options="item.id as item.value for items in data.month | orderBy:'id'"

Demo Plunkr

Your missing two ''s. This part, orderBy: key should be orderBy:'key'

Try this:

ng-options="key as value for (key, value) in data.month | orderBy: 'key'"

To resolve this issue need reformat ng-option like as:

day.id as day.value for day in data.month

And array data.month would be how as told user pankajparkar

发布评论

评论列表(0)

  1. 暂无评论