[英]How to sort list(Alphabetically) of items in auto-complete drop-down in angular js
<div custom-select name="pSelect" id="pSelect" ng-model="selectedP" ng-trim="false" ng-options="p as p.name for p in data"></div>
这是我的自定义下拉列表。 数据来自 json 对象。
<div custom-select name="pSelect" id="pSelect" ng-model="selectedP" ng-trim="false" ng-options="p as p.name for p in data | orderBy:'name'"></div>
你在寻找这样的东西吗?
尝试使用 angular orderby ,上面看起来像这样。
//last attribute of orderBy would depend on ascending or descending.
<div custom-select name="pSelect" id="pSelect" ng-model="selectedP" ng-trim="false" ng-options="p as p.name for p in data | orderBy: 'p': true"></div>
//or set dynamically
<div custom-select name="pSelect" id="pSelect" ng-model="selectedP" ng-trim="false" ng-options="p as p.name for p in data | orderBy: type : true"></div>
<button ng-click="type='name'"></button>
<button ng-click="type='year'"></button>
<button ng-click="type='other'"></button>
如果您的对象是一个数组,您可以使用内置的order-by 过滤器。
只需在您的 ng-options 中添加这一行
ng-options="p as p.name for p in data | orderBy : 'name'
这假设您有一个“名称”属性,如果没有将其更改为您想要排序的任何属性。
通过在属性前使用“-”运算符,您可以轻松颠倒排序顺序,例如
ng-options="p as p.name for p in data | orderBy : '-name'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.