Sorting in Angular JS along with paging - (in View)

Below is the code for applying sorting in Angular js table along with paging applied. In this code the sorting is done on the view only i.e. sorting is done using attributes in the ng-repeat tag itself.

It is the simplest way to apply sorting in AngularJS.

    <title>Angular sort </title>
    <link rel="stylesheet" href="//">
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>

     var app = angular.module('MyForm', ['ui.bootstrap', 'ngResource']);
     app.controller('myCtrl', function ($scope) {
       $scope.predicate = 'name';
       $scope.reverse = true;
       $scope.currentPage = 1;
       $scope.order = function (predicate) {
           $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
           $scope.predicate = predicate;
       $scope.students = [
         { name: 'Kevin', age: 25, city: 'a' },
         { name: 'John', age: 30, city: 'b' },
         { name: 'Laura', age: 28, city: 'ba' },
         { name: 'Joy', age: 15, city: 'd' },
         { name: 'Mary', age: 28, city: 'c' },
         { name: 'Peter', age: 95, city: 'fe' },
         { name: 'Bob', age: 50, city: 'g' },
         { name: 'Erika', age: 27, city: 'e' },
         { name: 'Patrick', age: 40, city: 'ef' },
         { name: 'Tery', age: 60, city: 'aa' }
       $scope.totalItems = $scope.students.length;
       $scope.numPerPage = 5;
       $scope.paginate = function (value) {
           var begin, end, index;
           begin = ($scope.currentPage - 1) * $scope.numPerPage;
           end = begin + $scope.numPerPage;
           index = $scope.students.indexOf(value);
           return (begin <= index && index < end);
        tbody tr {
            background-color: rgb(243, 240, 240);
            color: #008b8b;

        thead {
            background-color: darkgray;
<body ng-app="MyForm">
    <div ng-controller="myCtrl">
        <h3>List students</h3>
        <div class="container-fluid">
            <table class="table table-responsive">
                        <th><a href="" ng-click="order('name')">Name</a></th>
                        <th><a href="" ng-click="order('age')"> Age</a> </th>
                        <th><a href="" ng-click="order('city')">City</a> </th>
                        <td> <input type="text" ng-model="" /></td>
                        <td> <input type="text" ng-model="search.age" /> </td>
                        <td><input type="text" ng-model="" /> </td>
                    <tr ng-repeat="user in students | orderBy:predicate:reverse | filter:paginate| filter:search" ng-class-odd="'odd'">
                        <td>{{ user.age}}</td>
            <pagination total-items="totalItems" ng-model="currentPage"
                        max-size="5" boundary-links="true"
                        items-per-page="numPerPage" class="pagination-sm">

The above code has a limitation that it applies sorting to items that are on the current page, that is it applies paging first and then only sorts items that are currently visible to the user, rather than sorting the items first and then applying paging. The solution to this problem is sorting the items in the controller, to see how you can do that follow the link.

No comments:

Post a Comment