Angular Datatable Pagination, Sorting and Searching Using Ajax

Angular Datatable is an angular module for provides a datatable directive along with datatable options helpers. Angular Datatable module helps to create beautiful grid listing in your angular application using angular directive.

There are many angular directives and modules available for table grid but datatable is the most popular table grid plugin.

The main benefit of Angular Data-table is open source and light weighted. You can use this plugin with other css frameworks like bootstrap and foundation. You can learn more from the official website angular-datatables.

I got a huge response from readers on the previous datatable tutorial which was DataTables Example – Server-side Processing with PHP, Here I am letting you know how to integrate datatable angular module with your application.

You can also check other tutorials of angular,

We are using the below Files:

Index.html - This file will responsible to create an HTML table, angular module and instance datatable using angular code.
Library files - I have used jquery,angular-datatable and bootstrap lib files.

Simple Example of Angular Datatable Module

Step 1: We will include jquery data-table and jquery library in (index.html).

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="angular-datatables.min.js"></script>
<script src="jquery.dataTables.min.js"></script>

Here, I have included all necessary library files which are useful to integrate angular datatable with application.

Step 2: Create an Angular app for listing in (index.html).

angular.module('TestApp', ['TestApp.controllers','datatables']);

Here I have injected datatables module within our angular app.

Step 3: Now created sample JSON data and injected datatable options into the listing controller in (index.html).

angular.module('TestApp.controllers', []).controller('testController', function($scope,DTOptionsBuilder, DTColumnBuilder) {
    $scope.userList = [
        {
          
          Name: 'Parvez Alam',
          Age: '28'
        },
        
       {
        Name: 'Sameer',
        Age: '13'
        },
        {
        Name: 'Rakesh',
        Age: '55'
        },
        {
        Name: 'Ramesh',
        Age: '44'
        },
        {
        Name: 'Aman',
        Age: '34'
        },
        {
        Name: 'John',
        Age: '23'
        }
      ];

These datatable parameters DTOptionsBuilder, DTColumnBuilder help to control datatable listing table, You can set a number of columns, map column to value, sort column and order of column etc.

Step 4: Configured datatable option and column order using the below code in (index.html).

$scope.vm = {};
$scope.vm.dtOptions = DTOptionsBuilder.newOptions().withOption('order', [0, 'asc']);

Step 5: Created HTML table and iterate records using foreach loop in (index.html).

<table class="table table-striped table-bordered" datatable="ng">
	<thead>
		<tr>
			<th>Sr</th>
			<th>Name</th>
			<th>Age</th>
			<th>Action</th>
		</tr>
	</thead>
	<tbody>
		<tr ng-repeat="user in userList">
			<td>{{$index + 1}}</td>
			<td>{{user.Name}}</td>
			<td>{{user.Age}}</td>
			<td>
				<div class="btn-group">
				   <button type="button" class="btn btn-default btn" ng-click="edit($index);"><i class="glyphicon glyphicon-pencil"></i></button>  
				   <button type="button" class="btn btn-default btn" ng-click="delete();"><i class="glyphicon glyphicon-trash"></i></button> 
				</div>
			</td>
		</tr>
	</tbody>
</table>

Conclusion

We have learned about the simple integration of angular datatable into an angular application. We have converted HTML table into an wonderful grid using datatable directive without any any complex coding.

You can download the source code and Demo from the below link.

2 thoughts on “Angular Datatable Pagination, Sorting and Searching Using Ajax

Leave a Reply

Your email address will not be published.