Cách sắp xếp dữ liệu trong angular
<div ng-app="myApp" ng-controller="namesCtrl">
<table border="1" width="100%">
<tr>
<th ng-click="orderByMe('ten')">Tên</th>
<th ng-click="orderByMe('lop')">Lớp</th>
<th ng-click="orderByMe('diem')">Điểm</th>
</tr>
<tr ng-repeat="x in names | orderBy:myOrderBy:myOrderBy_AD">
<td>{{x.Ten}}</td>
<td>{{x.Lop}}</td>
<td>{{x.Diem}}</td>
</tr>
</table>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.hocsinh= [
{Ten:'Tran A',Lop:'Lớp A',Diem:7},
{Ten:'Tran B',Lop:'Lớp C',Diem:5},
{Ten:'Tran C',Lop:'Lớp B',Diem:9},
];
$scope.myOrderBy_AD=true; // tăng dần, giảm dần
$scope.myOrderBy_AD=!$scope.myOrderBy_AD;
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
}
});
</script>
Chú ý: <tr ng-repeat="x in names | orderBy:myOrderBy:myOrderBy_AD">
myOrderBy: tên cột
myOrderBy_AD: true tăng dần, false là giảm dần trong angular
Cách sắp xếp trong angular có sử dụng tăng dần, giảm dần