<p ng-app="app" ng-controller="appController">
<table>
<thead>
<tr>
<th>序号</th>
<th>内容</th>
<th>姓名</th>
<th>地点</th>
<th>时间</th>
<th ng-click="sort('id')">序号</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in obj | orderBy: 'id'">
<td ng-if="$index + 1 === 1" style="color: red">{{$index + 1}}</td>
<td ng-if="$index + 1 === 2" style="color: green">{{$index + 1}}</td>
<td ng-if="$index + 1 === 3" style="color: yellow">{{$index + 1}}</td>
<td ng-if="$index + 1 > 3">{{$index + 1}}</td>
<td>{{x.Name}}</td>
<td>{{x.City}}</td>
<td>{{x.Country}}</td>
<td>{{x.Country + x.City}}</td>
<td>{{x.id}}</td>
</tr>
</tbody>
</table>
</p>
//js文件
<script>
var app = angular.module('app', []);
app.controller('appController', function($scope, $http) {
//数据
$scope.obj = [
{
"Name": "Alfreds Futterkiste",
"City": "Berlin",
"Country": "Germany",
"id": 4
},
{
"Name": "Ana Trujillo Emparedados y helados",
"City": "México D.F.",
"Country": "Mexico",
"id": 3
},
{
"Name": "Comércio Mineiro",
"City": "São Paulo",
"Country": "Brazil",
"id": 2
}, {
"Name": "Ana Trujillo Emparedados y helados",
"City": "México D.F.",
"Country": "Mexico",
"id": 5
}
];
});
</script>
怎麼實現點擊序號進行升序降序的排序,新手,求詳細的程式碼,謝謝大家。
html
改下:js
你可以用過濾器 orderBy:'id':desc來控制排序是升序還是降序。
下面的程式碼你按照3-2-1的順序看比較容易理解。
1、點擊一次改一次desc
2、在appController中設定desc初始化值=0
3、在過濾器中加入排序條件