Introduction
In this article, I am going to explain how to use and integrate the jQuery datatable with AngularJS. DataTable is a prebuild functionality and a plug-in for the jQuery JavaScript library. It is a highly flexible tool based upon the foundations of progressive enhancement. It adds advanced interaction controls to any HTML table.
Overview of the process
- Create a web page to display the data.
- Get data from a SharePoint list.
- Reference jQuery datatable in the header of HTML file.
- Integrate the jQuery datatable with AngularJS.
Step1
Create a web page with a Table view to display the data which we get from the script code.
HTML
Here is the HTML code for the look and feel of the above webpage. We must need the table view to integrate the jQuery datatable.
- <table width="100%" cellspacing="0" border="1">
- <thead id="TblHead">
- <tr>
- <td>View</td>
- <td>Req No.</td>
- <td>Vendor</td>
- <td>Date Required </td>
- <td>Requisition Date</td>
- <td>Approval Status</td>
- <td>Order Status</td>
- <td>Notify</td>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="MyOrder in MyOrders">
- <td><a title='Click to view' href="#" target='_blank' class='btn btn-info btn-md'><span class='glyphicon glyphicon-user'></span> View</a></td>
- <td> {{MyOrder.OrderFrom}}</td>
- <td>{{MyOrder.Vendor}}</td>
- <td>{{MyOrder.DateRequired | date}}</td>
- <td>{{MyOrder.RequisitionDate | date}}</td>
- <td>{{MyOrder.WorkStatus}}</td>
- <td>{{MyOrder.OrderStatus}}</td>
- <td>Notify</td>
- </tr>
- </tbody>
- </table>
Where all the <tr> tags will repeat as per the data in the list using the ng-repeat directive of AngularJS.
Step 2
Get all the data from the SharePoint list on page load. This is the script code for retrieving the data from a SharePoint list using Angular with REST call.
- $http({
- method: 'GET',
- url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('RequisitionOrders')/Items",
- headers: {
- "Accept": "application/json;odata=verbose"
- }
- }).success(function(data, status, headers, config) {
- Console.log(data.d.results);
- }).error(function(data, status, headers, config) {});
Step 3
Integrate the jQuery Datatable with AngularJS as below.
In HTML file
Type the below line in your HTML file. The ID in HTML will help to bind the datatable functionalities to the entire table.
- <table width="100%" cellspacing="0" id="user_table" class="users list dtable" border="1">
In Script
Store the retrieved data to a scope.
- $scope.MyOrders = data.d.results;
Then, the defined scope variable named $scope.MyOrders will be used as settings for the data tables.
Integrate the Datatable to the AngularJS by adding the following code in success.
- angular.element(document).ready(function() {
- dTable = $('#user_table')
- dTable.DataTable();
- });
References
Please add the following JS & CSS files to your code in the following order.
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
- <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
- <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
- <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
- <script src="https://code.angularjs.org/1.3.0/angular.js"></script>
Let’s see the result on screen as below.
Hence, we have integrated the jQuery datatable to an Angular application successfully. Now, all the functionalities will work with the AngularJS application just same as they work in a jQuery application.
Conclusion
In modern web applications, most of us are likely to develop single page applications. To do so, we use AngularJS. Usually, table manipulation in AngularJS is easier than in normal jQuery. But still, search and server-side pagination are not straight forward in AngularJS. Hence, we can use datatable in AngularJS too.