简体   繁体   English

如何在angularjs中按字母和数字排序列表?

[英]How do I sort a list alphabetically and numerically in angularjs?

Here is my scenario 这是我的情况

 <selectclass="form-control" id="my_select"><optionvalue="0">List is Number</option><option value="1">List is Alphabetical</option></select> <liclass="ui-state-default " ng-repeat="damageResult in damageResultList> <span>{{damageResult.damageMechanismList}}hfgh</span></li> 

Apply orderBy filter on that field. 在该字段上应用orderBy过滤器。 You can learn more here about sorting. 您可以在此处了解有关排序的更多信息。

ng-repeat="damageResult in damageResultList | orderBy:'YOUR_FIELD_NAME'"

what you can do is define a custom function to sort and convert the text to number in case field is number 您可以做的是定义一个自定义函数,以在字段为数字的情况下对文本进行排序并将其转换为数字

$scope.sorterFunc = function(entity){
    return $scope.isFieldNumber? parseInt(entity[$scope.property], 10) : entity[$scope.property];
};

and then in ng-repeat 然后在ng-repeat中

<div ng-repeat="entity in entityArray | orderBy:sorterFunc ">

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

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