AngularJS按表头对行进行sorting

我有四个表头:

$scope.headers = ["Header1", "Header2", "Header3", "Header4"]; 

我希望能够通过点击标题来sorting我的表格。

所以如果我的桌子是这样的

 H1 | H2 | H3 | H4 AHD etc.... BGC CFB DEA 

我点击

 H2 

我的桌子现在看起来像这样:

 H1 | H2 | H3 | H4 DEA etc.... CFB BGC AHD 

也就是说,每一列的内容都不会改变,但是通过点击我想要sorting的标题,这些行就会重新排列。

我的表的内容是由与Mojolicious完成的数据库调用创build的,并返回给浏览器

 $scope.results = angular.fromJson(data); // This works for me so far 

我拼凑在一起的代码的其余部分看起来像这样:

 <table class= "table table-striped table-hover"> <th ng-repeat= "header in headers"> <a> {{headers[$index]}} </a> </th> <tr ng-repeat "result in results"> <td> {{results.h1}} </td> <td> {{results.h2}} </td> <td> {{results.h3}} </td> <td> {{results.h4}} </td> </tr> </table> 

如何从这一点开始订购专栏,只需点击表格顶部的标题即可?

我认为这个我创build的CodePen例子会告诉你如何做你想做的事情。

模板:

 <section ng-app="app" ng-controller="MainCtrl"> <span class="label">Ordered By: {{orderByField}}, Reverse Sort: {{reverseSort}}</span><br><br> <table class="table table-bordered"> <thead> <tr> <th> <a href="#" ng-click="orderByField='firstName'; reverseSort = !reverseSort"> First Name <span ng-show="orderByField == 'firstName'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span> </a> </th> <th> <a href="#" ng-click="orderByField='lastName'; reverseSort = !reverseSort"> Last Name <span ng-show="orderByField == 'lastName'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span> </a> </th> <th> <a href="#" ng-click="orderByField='age'; reverseSort = !reverseSort"> Age <span ng-show="orderByField == 'age'"><span ng-show="!reverseSort">^</span><span ng-show="reverseSort">v</span></span> </a> </th> </tr> </thead> <tbody> <tr ng-repeat="emp in data.employees|orderBy:orderByField:reverseSort"> <td>{{emp.firstName}}</td> <td>{{emp.lastName}}</td> <td>{{emp.age}}</td> </tr> </tbody> </table> </section> 

JavaScript代码:

 var app = angular.module('app', []); app.controller('MainCtrl', function($scope) { $scope.orderByField = 'firstName'; $scope.reverseSort = false; $scope.data = { employees: [{ firstName: 'John', lastName: 'Doe', age: 30 },{ firstName: 'Frank', lastName: 'Burns', age: 54 },{ firstName: 'Sue', lastName: 'Banter', age: 21 }] }; }); 

这里是一个小提琴,可以帮助你做到这一点与AngularJS
http://jsfiddle.net/patxy/D2FsZ/

 <th ng:repeat="(i,th) in head" ng:class="selectedCls(i)" ng:click="changeSorting(i)"> {{th}} </th> 

然后像这样的数据:

 <tr ng:repeat="row in body.$orderBy(sort.column, sort.descending)"> <td>{{row.a}}</td> <td>{{row.b}}</td> <td>{{row.c}}</td> </tr> 

在你的AngularJS控制器中有这样的function:

 scope.sort = { column: 'b', descending: false }; scope.selectedCls = function(column) { return column == scope.sort.column && 'sort-' + scope.sort.descending; }; scope.changeSorting = function(column) { var sort = scope.sort; if (sort.column == column) { sort.descending = !sort.descending; } else { sort.column = column; sort.descending = false; } }; 

在AngularJS中做到这一点的另一种方法是使用网格。

与网格的优点是,您正在寻找的行sorting行为是默认包含的。

function被很好地封装。 您不需要添加ng-click属性,或者使用范围variables来维护状态:

  <body ng-controller="MyCtrl"> <div class="gridStyle" ng-grid="gridOptions"></div> </body> 

您只需将网格选项添加到您的控制器:

  $scope.gridOptions = { data: 'myData.employees', columnDefs: [{ field: 'firstName', displayName: 'First Name' }, { field: 'lastName', displayName: 'Last Name' }, { field: 'age', displayName: 'Age' }] }; 

完整的工作代码附加:

 var app = angular.module('myApp', ['ngGrid', 'ngAnimate']); app.controller('MyCtrl', function($scope) { $scope.myData = { employees: [{ firstName: 'John', lastName: 'Doe', age: 30 }, { firstName: 'Frank', lastName: 'Burns', age: 54 }, { firstName: 'Sue', lastName: 'Banter', age: 21 }] }; $scope.gridOptions = { data: 'myData.employees', columnDefs: [{ field: 'firstName', displayName: 'First Name' }, { field: 'lastName', displayName: 'Last Name' }, { field: 'age', displayName: 'Age' }] }; }); 
 /*style.css*/ .gridStyle { border: 1px solid rgb(212,212,212); width: 400px; height: 200px } 
 <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> <meta charset="utf-8"> <title>Custom Plunker</title> <link rel="stylesheet" type="text/css" href="http://angular-ui.github.com/ng-grid/css/ng-grid.css" /> <link rel="stylesheet" type="text/css" href="style.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.3/angular.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.3/angular-animate.js"></script> <script type="text/javascript" src="http://angular-ui.github.com/ng-grid/lib/ng-grid.debug.js"></script> <script type="text/javascript" src="main.js"></script> </head> <body ng-controller="MyCtrl"> <div class="gridStyle" ng-grid="gridOptions"></div> </body> </html> 

这是一个按头部sorting的例子。 该表是dynamic的,并且随JSON大小而变化。

我能够build立一个dynamic的表格,其他人的例子和文档。 http://jsfiddle.net/lastlink/v7pszemn/1/

 <tr> <th class="{{header}}" ng-repeat="(header, value) in items[0]" ng-click="changeSorting(header)"> {{header}} <i ng-class="selectedCls2(header)"></i> </tr> <tbody> <tr ng-repeat="row in pagedItems[currentPage] | orderBy:sort.sortingOrder:sort.reverse"> <td ng-repeat="cell in row"> {{cell}} </td> </tr> 

虽然这些列是不合序列的,但是在我的.NET项目中,它们是有序的。

你可以使用这个代码没有箭头…..即通过点击标题它会自动显示元素的升序和降序

  <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="scripts/angular.min.js"></script> <script src="Scripts/Script.js"></script> <style> table { border-collapse: collapse; font-family: Arial; } td { border: 1px solid black; padding: 5px; } th { border: 1px solid black; padding: 5px; text-align: left; } </style> </head> <body ng-app="myModule"> <div ng-controller="myController"> <br /><br /> <table> <thead> <tr> <th> <a href="#" ng-click="orderByField='name'; reverseSort = !reverseSort"> Name </a> </th> <th> <a href="#" ng-click="orderByField='dateOfBirth'; reverseSort = !reverseSort"> Date Of Birth </a> </th> <th> <a href="#" ng-click="orderByField='gender'; reverseSort = !reverseSort"> Gender </a> </th> <th> <a href="#" ng-click="orderByField='salary'; reverseSort = !reverseSort"> Salary </a> </th> </tr> </thead> <tbody> <tr ng-repeat="employee in employees | orderBy:orderByField:reverseSort"> <td> {{ employee.name }} </td> <td> {{ employee.dateOfBirth | date:"dd/MM/yyyy" }} </td> <td> {{ employee.gender }} </td> <td> {{ employee.salary }} </td> </tr> </tbody> </table> </div> <script> var app = angular .module("myModule", []) .controller("myController", function ($scope) { var employees = [ { name: "Ben", dateOfBirth: new Date("November 23, 1980"), gender: "Male", salary: 55000 }, { name: "Sara", dateOfBirth: new Date("May 05, 1970"), gender: "Female", salary: 68000 }, { name: "Mark", dateOfBirth: new Date("August 15, 1974"), gender: "Male", salary: 57000 }, { name: "Pam", dateOfBirth: new Date("October 27, 1979"), gender: "Female", salary: 53000 }, { name: "Todd", dateOfBirth: new Date("December 30, 1983"), gender: "Male", salary: 60000 } ]; $scope.employees = employees; $scope.orderByField = 'name'; $scope.reverseSort = false; }); </script> </body> </html> 

使用第三方JavaScript库。 它会给你,还有更多。 一个很好的例子是数据表(如果你也使用jQuery): https : //datatables.net

或者在点击标题的时候在$ scope.results中定义你的绑定数组。

我只是让我的脚湿润,但我发现这个伟大的教程。
这是一个工作,我把信贷斯科特·艾伦和上述教程放在一起。 点击search以显示可sorting的表。

对于每个列标题,您需要使其可点击 – 单击链接将工作。 这将设置要sorting的列的sortName。

 <th> <a href="#" ng-click="sortName='name'; sortReverse = !sortReverse"> <span ng-show="sortName == 'name' && sortReverse" class="glyphicon glyphicon-triangle-bottom"></span> <span ng-show="sortName == 'name' && !sortReverse" class="glyphicon glyphicon-triangle-top"></span> Name </a> </th> 

然后,在表体中,您可以在orderByfilterorderBy:sortName:sortReverse中pipe理该sortName

 <tr ng-repeat="repo in repos | orderBy:sortName:sortReverse | filter:searchRepos"> <td>{{repo.name}}</td> <td class="tag tag-primary">{{repo.stargazers_count | number}}</td> <td>{{repo.language}}</td> </tr> 
 ----Try this---- First change your controller yourModuleName.controller("yourControllerName", function ($scope) { var list = [ { H1:'A', H2:'B', H3:'C', H4:'d' }, { H1:'E', H2:'B', H3:'F', H4:'G' }, { H1:'C', H2:'H', H3:'L', H4:'M' }, { H1:'I', H2:'B', H3:'E', H4:'A' } ]; $scope.list = list; $scope.headers = ["Header1", "Header2", "Header3", "Header4"]; $scope.sortColumn = 'Header1'; $scope.reverseSort = false; $scope.sortData = function (columnIndex) { $scope.reverseSort = ($scope.sortColumn == $scope.headers[columnIndex]) ? !$scope.reverseSort : false; $scope.sortColumn = $scope.headers[columnIndex]; } }); then change code in html side like this <th ng-repeat= "header in headers"> <a ng-click="sortData($index)"> {{headers[$index]}} </a> </th> <tr ng-repeat "result in results | orderBy : sortColumn : reverseSort"> <td> {{results.h1}} </td> <td> {{results.h2}} </td> <td> {{results.h3}} </td> <td> {{results.h4}} </td> </tr> 

我find了解决这个问题的最简单的方法。 如果高效率,您可以使用

HTML代码: import angular.min.jsangular.route.js

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>like/dislike</title> </head> <body ng-app="myapp" ng-controller="likedislikecntrl" bgcolor="#9acd32"> <script src="./modules/angular.min.js"></script> <script src="./modules/angular-route.js"></script> <script src="./likedislikecntrl.js"></script> </select></h1></p> <table border="5" align="center"> <thead> <th>professorname <select ng-model="sort1" style="background-color: chartreuse"> <option value="+name" >asc</option> <option value="-name" >desc</option> </select></th> <th >Subject <select ng-model="sort1"> <option value="+subject" >asc</option> <option value="-subject" >desc</option></select></th> <th >Gender <select ng-model="sort1"> <option value="+gender">asc</option> <option value="-gender">desc</option></select></th> <th >Likes <select ng-model="sort1"> <option value="+likes" >asc</option> <option value="-likes" >desc</option></select></th> <th >Dislikes <select ng-model="sort1"> <option value="+dislikes" >asc</option> <option value="-dislikes">desc</option></select></th> <th rowspan="2">Like/Dislike</th> </thead> <tbody> <tr ng-repeat="sir in sirs | orderBy:sort1|orderBy:sort|limitTo:row" > <td >{{sir.name}}</td> <td>{{sir.subject|uppercase}}</td> <td>{{sir.gender|lowercase}}</td> <td>{{sir.likes}}</td> <td>{{sir.dislikes}}</td> <td><button ng-click="ldfi1(sir)" style="background-color:chartreuse" >Like</button></td> <td><button ng-click="ldfd1(sir)" style="background- color:chartreuse">Dislike</button></td> </tr> </tbody> </table> </body> </html> JavaScript Code::likedislikecntrl.js var app=angular.module("myapp",["ngRoute"]); app.controller("likedislikecntrl",function ($scope) { var sirs=[ {name:"Srinivas",subject:"dmdw",gender:"male",likes:0,dislikes:0}, {name:"Sharif",subject:"dms",gender:"male",likes:0,dislikes:0}, {name:"Chaitanya",subject:"daa",gender:"male",likes:0,dislikes:0}, {name:"Pranav",subject:"wt",gender:"male",likes:0,dislikes:0}, {name:"Anil Chowdary",subject:"ds",gender:"male",likes:0,dislikes:0}, {name:"Rajesh",subject:"mp",gender:"male",likes:0,dislikes:0}, {name:"Deepak",subject:"dld",gender:"male",likes:0,dislikes:0}, {name:"JP",subject:"mp",gender:"male",likes:0,dislikes:0}, {name:"NagaDeepthi",subject:"oose",gender:"female",likes:0,dislikes:0}, {name:"Swathi",subject:"ca",gender:"female",likes:0,dislikes:0}, {name:"Madavilatha",subject:"cn",gender:"female",likes:0,dislikes:0} ] $scope.sirs=sirs; $scope.ldfi1=function (sir) { sir.likes++ } $scope.ldfd1=function (sir) { sir.dislikes++ } $scope.row=8; })