javascript 如何按键对 ng-options 中的数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30621053/
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
How to sort array in ng-options by key?
提问by Faud
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.
但我得到未排序的选择列表。
回答by Pankaj Parkar
In order to use tracking with filters use track by
.
为了使用带有过滤器的跟踪,请使用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"},......]
这orderBy
永远不会起作用,因为您拥有文字数组。您需要将此数组转换为类似 JSON 的对象,其结构类似于[{id: 0, value: "M"}, {id: 1, value: "January"},......]
HTML
HTML
ng-options="item.id as item.value for items in data.month | orderBy:'id'"
回答by solid_luffy
Your missing two '
's. This part, orderBy: key
should be orderBy:'key'
你少了两个'
。这部分,orderBy: key
应该是orderBy:'key'
Try this:
试试这个:
ng-options="key as value for (key, value) in data.month | orderBy: 'key'"
回答by Faud
To resolve this issue need reformat ng-option
like as:
要解决此问题,需要重新格式化,ng-option
如:
day.id as day.value for day in data.month
And array data.month
would be how as told user pankajparkar
和数组data.month
将是如何告诉用户pankajparkar