简体   繁体   English

如何在angular js的自动完成下拉列表中对项目列表(按字母顺序)进行排序

[英]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.数据来自 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>

你在寻找这样的东西吗?

Try using angular orderby , the above would look something like this.尝试使用 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>

If your object is an array, you can use the built-in order-by filter .如果您的对象是一个数组,您可以使用内置的order-by 过滤器

Just add this line in your ng-options只需在您的 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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