Penyaringan orderBy AngularJS

Definisi dan penggunaan

orderBy Penyaringan memungkinkan kami untuk mengurutkan array.

Secara baku, string diurutkan dalam urutan abjad, angka diurutkan dalam urutan angka.

Halaman berhubungan

Panduan AngularJS:Penapis Angular

Contoh

Contoh 1

Tampilkan item dalam urutan abjad:

<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x in cars | orderBy">{{x}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
    $scope.cars = ["Dodge", "Fiat", "Audi", "Volvo", "BMW", "Ford"];
]];
);

</script>

Contoh 2

Menurut "city" diurutkan dalam urutan:
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x in customers | orderBy : 'city'">{{x.name + ", " + x.city}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
    $scope.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" : "Around the Horn", "city" : "London"}
    {"name" : "B's Beverages", "city" : "London"}
]];
);

</script>

Contoh 3

Menurut "city" diurutkan dalam urutan turun:
<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="x in customers | orderBy : '-city'">{{x.name + ", " + x.city}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
    $scope.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" : "Around the Horn", "city" : "London"}
    {"name" : "B's Beverages", "city" : "London"}
]];
);

</script>

Coba sendiri

Syarat {{ array Description | orderBy : Array: Jika perlu beberapa properti objek untuk menentukan urutan pengurutan, gunakan array. Item array boleh adalah string dan fungsi. :

}}

}} Parameter
Description

expression

Ungkapan untuk menentukan urutan. Ekspresi boleh adalah jenis berikut:

String: Jika array adalah array objek, maka array boleh diurutkan berdasarkan nilai salah satu properti objek. Lihat contoh di atas.

Fungsi: Anda boleh membuat fungsi untuk mengatur pengurutan.

Array: Jika perlu beberapa properti objek untuk menentukan urutan pengurutan, gunakan array. Item array boleh adalah string dan fungsi. reverse