使用AngularJS的過(guò)濾器,可以很容易的實(shí)現(xiàn)在表格中,點(diǎn)擊某一列標(biāo)題進(jìn)行排序,實(shí)現(xiàn)過(guò)程如下:
html代碼:
<table class="table table-border" ng-app="myapp" ng-controller="orderByCtrl"> <thead> <tr> <th>inx</th> <th ng-click="col='name';desc=!desc">name</th> <!-- 當(dāng)點(diǎn)擊列標(biāo)題時(shí),執(zhí)行click事件,將排序條件反轉(zhuǎn),即,如果原來(lái)是升序則將按降序,降序亦如此 --> <th ng-click="col='gender';desc=!desc">gender</th> <th ng-click="col='age';desc=!desc">age</th> <th ng-click="col='score';desc=!desc">score</th> </tr> </thead> <tbody> <tr ng-repeat="d in data|orderBy:col:desc"> <td ng-bind="$index+1"></td> <td ng-bind="d.name"></td> <td ng-bind="d.gender"></td> <td ng-bind="d.age"></td> <td ng-bind="d.score"></td> </tr> </tbody></table>
js代碼:
var app = angular.module('myapp', []);app.controller('orderByCtrl', function($scope) { $scope.col = 'name';//默認(rèn)按name列排序 $scope.desc = 0;//默認(rèn)排序條件升序 $scope.data = [{ name: 'name 1', gender: 'male', age: 26, score: 70 }, { name: 'name 2', gender: 'female', age: 24, score: 84 }, { name: 'name 3', gender: 'male', age: 20, score: 76 }, { name: 'name 4', gender: 'female', age: 22, score: 64 }];})讓運(yùn)行界面好看些,使用了bootstrap.min.css樣式庫(kù)。為了交互性考慮,在表頭增加了手指樣式
th { cursor: pointer;}以上所述是小編給大家介紹的妙用Angularjs實(shí)現(xiàn)表格按指定列排序,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)錯(cuò)新站長(zhǎng)站網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選