Sorting, Paging and Filter grid using AngularJS

Now I am going to explain that how can we achieve paging, sorting and filter in grid by using ng-table api of angularjs.

Functionality we will achieve after doing this article:

1)      Paging.
2)      Sorting.
3)      Filtering.

Let’s start step by step so that we can achieve our objective at the end of this article. After completing this demo our application should look like this:

1)      Include this package in your existing application:

  1. a) ngTable

It supports sorting, filtering and pagination. Header row with titles and filters automatic generated on compilation step.

2)This will add the reference of ngTable api of angularjs in our application. After adding this only we can use its sorting, paging and filter features.


Layout = null;

<!DOCTYPE html>
<html ng-app="EmpApp">
<meta name="viewport" content="width=device-width" />
<link href="@Url.Content("~/Content/bootstrap.min.css")"rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/ng-table.css")" rel="stylesheet"type="text/css" />
<script src="@Url.Content("~/Scripts/angular.min.js")"type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/angular-route.js")"type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/ng-table.min.js")"type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/app/app.js")"type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/app/controller.js")"type="text/javascript"></script>
<div class="main container" ng-view>

3)In list.html, replace the code with the following. I highlighted some of the changes part so it will be easy for us identity the code of functionalities given by this tutorial.

<a href="#/create" class="btn">Create</a>


<div class="row">

<div class="col-lg-offset-8 col-lg-4">

<input type="text" class="form-control" placeholder="Search for..."ng-model="search" />



<hr />

<div class="table-responsive">

<p class="text-right">

<strong>Page:</strong> {{}}, <strong>Count per page:</strong>


<table class="table table-striped table-bordered" ng-table="tableParams" template-pagination="custom/pager">

<tr ng-repeat="item in $data | filter:search">

<td data-title="'Employee Id'" sortable="'EmployeeId'">

{{ item.EmployeeId }}


<td data-title="'First Name'" sortable="'FirstName'">

{{ item.FirstName }}


<td data-title="'Last Name'" sortable="'LastName'">

{{ item.LastName }}


<td data-title="'Description'">

{{ item.Description }}


<td data-title="'Salary'" sortable="'Salary'">

{{ item.Salary | number: 2 }}


<td data-title="'Country'" sortable="'Country'">

{{ item.Country }}


<td data-title="'State'" sortable="'State'">

{{ item.State }}


<td data-title="'Date of Birth'" sortable="'DateofBirth'">

{{ item.DateofBirth | date }}


<td data-title="'Is Active'">

<span class="label" ng-class="{true:'label-success', false:'label-danger', '':'hidden'}[item.IsActive]">

{{ item.IsActive ? 'Active' : 'In Active' }}</span>



<a href="#/edit/{{item.EmployeeId}}" class="glyphicon glyphicon-edit"></a>



<a href="#/delete/{{item.EmployeeId}}" class="glyphicon glyphicon-trash"></a>




<script type="text/ng-template" id="custom/pager">

<ul class="pager ng-cloak">

<li ng-repeat="page in pages"

ng-class="{'disabled': !, 'previous': page.type == 'prev', 'next': page.type == 'next'}"

ng-show="page.type == 'prev' || page.type == 'next'" ng-switch="page.type">

<a ng-switch-when="prev" ng-click="" href="">&laquo; Previous</a>

<a ng-switch-when="next" ng-click="" href="">Next &raquo;</a>



<div class="btn-group">

<button type="button" ng-class="{'active':params.count() == 5}" ng-click="params.count(5)" class="btn btn-default">5</button>

<button type="button" ng-class="{'active':params.count() == 10}" ng-click="params.count(10)" class="btn btn-default">10</button>

<button type="button" ng-class="{'active':params.count() == 20}" ng-click="params.count(20)" class="btn btn-default">20</button>

<button type="button" ng-class="{'active':params.count() == 50}" ng-click="params.count(50)" class="btn btn-default">50</button>






4) In controller.js, only replace declaration section of EmpControllers and ListController definition.

var EmpControllers = angular.module("EmpControllers", ['ngTable']);

// this controller call the api method and display the list of employees

// in list.html

EmpControllers.controller("ListController", ['$scope', '$http', '$filter','ngTableParams',

function ($scope, $http, $filter, ngTableParams) {

$scope.headers = [

{ column: "FirstName" },

{ column: "LastName" }


$http.get('/api/employee').success(function (data) {

$scope.tableParams = new ngTableParams({

page: 1,              // show first page

count: 5,             // count per page

sorting: {

EmployeeId: 'asc' // initial sorting


}, {

total: data.length,

getData: function($defer, params) {

var orderedData = params.sorting() ?

$filter('orderBy')(data, params.orderBy()) :


$defer.resolve(orderedData.slice(( - 1) * params.count(), * params.count()));







3 thoughts on “Sorting, Paging and Filter grid using AngularJS

Author gravatar
Diwakar April 4, 2017, 5:18 am
Author gravatar
Pawan Shukla April 8, 2017, 8:02 am
Author gravatar
Diwakar April 4, 2017, 5:18 am

Very good

Leave a Reply

Your email address will not be published. Required fields are marked *