Now, I am going to explain how can we achieve paging, sorting, and filter in grid by using ng-table API of AngularJS. In this article we will replace some code with the previous tutorial so please download the code of the previous article or you can follow the previous article in order to achieve this.
The functionality we will achieve after doing this article:
- Paging
- Sorting
- 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:
- Include this package in your existing application:
a) ngTable
It supports sorting, filtering, and pagination. Header row with titles and filters are automatically generated on compilation step.
- Add the highlighted reference in your Index.cshtml file. This will add the reference of ngTable API of AngularJS in our application. After adding this onlythen can we can use its sorting, paging, and filter features.
- @{
- Layout = null;
- }
-
-
- <!DOCTYPE html>
- <html ng-app="EmpApp">
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- <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>
- </head>
- <body>
- <div class="main container" ng-view></div>
- </body>
- </html>
- In list.html, replace the code with the following. I highlighted some of the changed parts so it will be easy for us identity the code of functionalities given by this tutorial.
- <div>
- <a href="#/create" class="btn">Create</a>
- </div>
- <div class="row">
- <div class="col-lg-offset-8 col-lg-4">
- <input type="text" class="form-control" placeholder="Search for..." ng-model="search" />
- </div>
- </div>
- <hr />
- <div class="table-responsive">
- <p class="text-right">
- <strong>Page:</strong> {{tableParams.page()}},
- <strong>Count per page:</strong>
- {{tableParams.count()}}
- </p>
- <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>
- <td data-title="'First Name'" sortable="'FirstName'">
- {{ item.FirstName }}
- </td>
- <td data-title="'Last Name'" sortable="'LastName'">
- {{ item.LastName }}
- </td>
- <td data-title="'Description'">
- {{ item.Description }}
- </td>
- <td data-title="'Salary'" sortable="'Salary'">
- {{ item.Salary | number: 2 }}
- </td>
- <td data-title="'Country'" sortable="'Country'">
- {{ item.Country }}
- </td>
- <td data-title="'State'" sortable="'State'">
- {{ item.State }}
- </td>
- <td data-title="'Date of Birth'" sortable="'DateofBirth'">
- {{ item.DateofBirth | date }}
- </td>
- <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>
- </td>
- <td>
- <a href="#/edit/{{item.EmployeeId}}" class="glyphicon glyphicon-edit"></a>
- </td>
- <td>
- <a href="#/delete/{{item.EmployeeId}}" class="glyphicon glyphicon-trash"></a>
- </td>
- </tr>
- </table>
- <script type="text/ng-template" id="custom/pager">
- <ul class="pager ng-cloak">
- <li ng-repeat="page in pages"
- ng-class="{'disabled': !page.active, '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="params.page(page.number)" href="">« Previous</a>
- <a ng-switch-when="next" ng-click="params.page(page.number)" href="">Next »</a>
- </li>
- <li>
- <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>
- </div>
- </li>
- </ul>
- </script>
- </div>
- In controller.js, only replace declaration section of EmpControllers and ListController definition.
- var EmpControllers = angular.module("EmpControllers", ['ngTable']);
-
-
- 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,
- count: 5,
- sorting:
- {
- EmployeeId: 'asc'
- }
- },
- {
- total: data.length,
- getData: function($defer, params)
- {
- var orderedData = params.sorting() ? $filter('orderBy')(data, params.orderBy()) : data;
- $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
- }
- });
- });
- }
- ]);
Thanks for reading this article. I hope this will help you. If you have any suggestion or feedback please provide by using the comment box.