如何使orderby过滤器在字符串数组中工作?

时间:2023-01-14 20:11:52

Here is the code which is not working: Demo: http://jsfiddle.net/8dt94/63/

下面是不工作的代码:Demo: http://jsfiddle.net/8dt94/63/

<div ng-controller="MyCtrl">    
    <input type="text" ng-model="searchText" />
  <ul ng-repeat="strVal in arrVal|orderBy|filter:searchText" >
      <li>{{strVal}}</li>
  </ul>
</div>

var app=angular.module('myApp', []);
app.controller('MyCtrl', function ($scope,$filter) {
  $scope.arrVal = ['one','two','three','four','five','six'];  
});

3 个解决方案

#1


241  

You can order by a method, so you can use the toString method

您可以通过一个方法进行排序,因此可以使用toString方法

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText">

#2


11  

Write a custom filter:

编写一个定制的过滤器:

app.filter('mySort', function() {
    return function(input) {
      return input.sort();
    }
  });

HTML:

HTML:

<ul ng-repeat="strVal in arrVal|filter:searchText|mySort">

Fiddle.

小提琴。

#3


0  

its simple, for example if you have this array object:

它很简单,例如如果你有这个数组对象:

 cars = ["Dodge", "Fiat", "Audi", "Volvo", "BMW", "Ford"];

and you want to have the object sorted in the HTML front, use:

你想要在HTML前端排序对象,使用:

 <li ng-repeat="x in cars | orderBy">{{x}}</li>

By default, strings are sorted alphabetically, and numbers are sorted numerically.

默认情况下,字符串按字母顺序排序,数字按数字顺序排序。


If you have this array with keys:

如果你有这个带有键的数组:

customers = [
    {"name" : "Bottom-Dollar Marketse" ,"city" : "Tsawassen"},
    {"name" : "Alfreds Futterkiste", "city" : "Berlin"},
    {"name" : "Bon app", "city" : "Marseille"},
    {"name" : "Cactus Comidas para llevar", "city" : "Buenos Aires"},
    {"name" : "Bolido Comidas preparadas", "city" : "Madrid"},
    {"name" : "Around the Horn", "city" : "London"},
    {"name" : "B's Beverages", "city" : "London"}
];

Use:

使用:

Sort the array by "city" DESC order:

按“城市”DESC命令对数组进行排序:

 <li ng-repeat="x in customers | orderBy : '-city'">{{x.name + ", " + x.city}}</li>

Sort the array by "city":

按“城市”对数组进行排序:

<li ng-repeat="x in customers | orderBy : 'city'">{{x.name + ", " + x.city}}</li>

#1


241  

You can order by a method, so you can use the toString method

您可以通过一个方法进行排序,因此可以使用toString方法

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText">

#2


11  

Write a custom filter:

编写一个定制的过滤器:

app.filter('mySort', function() {
    return function(input) {
      return input.sort();
    }
  });

HTML:

HTML:

<ul ng-repeat="strVal in arrVal|filter:searchText|mySort">

Fiddle.

小提琴。

#3


0  

its simple, for example if you have this array object:

它很简单,例如如果你有这个数组对象:

 cars = ["Dodge", "Fiat", "Audi", "Volvo", "BMW", "Ford"];

and you want to have the object sorted in the HTML front, use:

你想要在HTML前端排序对象,使用:

 <li ng-repeat="x in cars | orderBy">{{x}}</li>

By default, strings are sorted alphabetically, and numbers are sorted numerically.

默认情况下,字符串按字母顺序排序,数字按数字顺序排序。


If you have this array with keys:

如果你有这个带有键的数组:

customers = [
    {"name" : "Bottom-Dollar Marketse" ,"city" : "Tsawassen"},
    {"name" : "Alfreds Futterkiste", "city" : "Berlin"},
    {"name" : "Bon app", "city" : "Marseille"},
    {"name" : "Cactus Comidas para llevar", "city" : "Buenos Aires"},
    {"name" : "Bolido Comidas preparadas", "city" : "Madrid"},
    {"name" : "Around the Horn", "city" : "London"},
    {"name" : "B's Beverages", "city" : "London"}
];

Use:

使用:

Sort the array by "city" DESC order:

按“城市”DESC命令对数组进行排序:

 <li ng-repeat="x in customers | orderBy : '-city'">{{x.name + ", " + x.city}}</li>

Sort the array by "city":

按“城市”对数组进行排序:

<li ng-repeat="x in customers | orderBy : 'city'">{{x.name + ", " + x.city}}</li>