AngularJS sorting rows by table header

I have four table headers:

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

And I want to be able to sort my table by clicking on the title.

So if my table looks like this

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

and i click

 H2 

Now my table looks like this:

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

That is, the contents of each column never changes, but by clicking on the header I want to order the columns, the rows will be reordered.

The contents of my table are created by a database call made using Mojolicious and returned to the browser using

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

The rest of the code I exhausted looks something like this:

 <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> 

How to arrange columns from this point just by clicking the heading at the top of the table?

+56
angularjs html-table angularjs-ng-repeat
01 Oct '13 at 18:06
source share
9 answers

I think this working CodePen example that I created will show you how to do what you want.

Template:

 <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 Code:

 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 }] }; }); 
+137
Oct 01 '13 at 18:41
source share
— -

Here is a fiddle to help you do this with AngularJS
http://jsfiddle.net/patxy/D2FsZ/

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

Then something like this for your data:

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

With these features in your AngularJS controller:

 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; } }; 
+5
01 Oct '13 at 18:45
source share

Another way to do this in AngularJS is to use a Grid.

The advantage of the grid is that the row sorting behavior you are looking for is enabled by default.

Functionality is well encapsulated. You do not need to add ng-click attributes or use scope variables to maintain state:

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

You simply add grid parameters to your controller:

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

The full working fragment is attached:

 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> 
+2
Feb 04 '15 at 23:45
source share

Here is an example that is sorted by title. This table is dynamic and resizes with JSON size.

I managed to create a dynamic table for some other examples and documentation. 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> 

Although the columns are out of order, they are in order in my .NET project.

+1
May 04 '16 at 21:51
source share

You can use this code without arrows ..... ie, by clicking on the title, it automatically shows the increasing and decreasing order of the elements.

  <!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> 
+1
Dec 13 '16 at 7:29
source share

Use a third-party JavaScript library. This will give you this and more. A good example is datatables (if you also use jQuery): https://datatables.net

Or just order the associated array in $ scope.results when the header is clicked.

0
Oct. 01 '13 at 18:16
source share

I just pee my feet with angular, but I found this great tutorial.
A plunk is working here. I referred to a loan to Scott Allen and the above textbook. Click Search to display the sorted table.

For each column heading you need to make it clickable - ng-click on the link will work. This will sort the sortName column.

 <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> 

Then in the body of the table you can pass in this sortName to orderBy filter orderBy: sortName: sortReverse

 <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> 
0
07 Oct '16 at 22:39
source share
 ----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> 
0
Dec 26 '16 at 12:52
source share

I found the easiest way to resolve this issue. If you are effective, you can use

HTML code: import angular.min.js and the angular.route.js library

 <!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; }) 
0
Jul 01 '17 at 19:09
source share



All Articles