[英]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.