简体   繁体   中英

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>

This is my custom dropdownlist. Data is coming from the json object.

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

你在寻找这样的东西吗?

Try using angular orderby , the above would look something like this.

//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>

If your object is an array, you can use the built-in order-by filter .

Just add this line in your ng-options

ng-options="p as p.name for p in data | orderBy : 'name'

This assumes you have a 'name' property, if not change it to whatever property you are wanting to sort by.

You can easily reverse the sort order, by using a '-' operator before the property, such as

    ng-options="p as p.name for p in data | orderBy : '-name'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM