Custom Paging
Custom Paging improves the performance of default paging by retrieving only those records from the database that must be displayed on the particular page of data requested by the user. However, custom paging involves a bit more efforts to implement than that for default paging.
Read the below articles first to understand the AngularJS UI-Grid.
Prerequisites
Visual Studio 2017 is the prerequisite to work with this article.
Thus, let's just use the sections with which we can implement the functionality.
- Create ASP.NET MVC 5 Application.
- Adding Model.
- Scaffolding in MVC 5.
- View in MVC 5.
- Log in an Entity Framework.
Create ASP.NET MVC 5 Application
In this section, we'll create an ASP.NET Web Application with the MVC 5 Project Template. Use the procedure given below.
Step 1
Open Visual Studio 2015 and click "New Project".
Step 2
Select "Web" from the left pane and create ASP.NET Web application.
Step 3
Select the MVC Project template in the next ASP.NET wizard.
Visual Studio automatically creates the MVC 5 Application, adds some files and folders to the solution.
Working with Entity Framework
Step 1
Right click on Models folder, click Add New Item, select ADO.NET Entity Data Model from Data template and give a name.
Step 2
Select EF Designer from the database.
Step 3
Make a new connection and select a connection, if you already have a connection.
Step 4
Select tables, view, and stored procedures and click Finish.
Stored Procedure
- USE [NORTHWND]
- GO
-
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER PROCEDURE [dbo].[GetEmployeeWithPaging]
- @PageNumber INT = NULL,
- @PageSize INT = NULL,
- @OrderBy VARCHAR(50) = N'EmployeeID', -- Column to sort programs by
- @SortDirection VARCHAR(4) = N'asc'
- AS
- DECLARE @totalCount int
- SET @totalCount = (SELECT COUNT(EmployeeID) as totalRecords FROM Employees)
- SELECT EmployeeID,
- FirstName,
- LastName,
- City,
- Region,
- PostalCode,
- Country,
- Notes,
- @totalCount as TotalCount
- FROM Employees
- ORDER BY
- CASE WHEN @SortDirection = 'asc' AND @OrderBy = 'FirstName' THEN FirstName END ASC
- ,CASE WHEN @SortDirection = 'desc' AND @OrderBy = 'FirstName' THEN FirstName END DESC
- ,CASE WHEN @SortDirection = 'asc' AND @OrderBy = 'LastName' THEN LastName END ASC
- ,CASE WHEN @SortDirection = 'desc' AND @OrderBy = 'LastName' THEN LastName END DESC
- ,CASE WHEN @SortDirection = 'asc' AND @OrderBy = 'City' THEN City END ASC
- ,CASE WHEN @SortDirection = 'desc' AND @OrderBy = 'City' THEN City END DESC
- ,CASE WHEN @SortDirection = 'asc' AND @OrderBy = 'Region' THEN Region END ASC
- ,CASE WHEN @SortDirection = 'desc' AND @OrderBy = 'Region' THEN Region END DESC
- ,CASE WHEN @SortDirection = 'asc' AND @OrderBy = 'PostalCode' THEN PostalCode END ASC
- ,CASE WHEN @SortDirection = 'desc' AND @OrderBy = 'PostalCode' THEN PostalCode END DESC
- ,CASE WHEN @SortDirection = 'asc' AND @OrderBy = 'Country' THEN Country END ASC
- ,CASE WHEN @SortDirection = 'desc' AND @OrderBy = 'Country' THEN Country END DESC
- OFFSET ((@PageNumber-1) * @PageSize) ROWS
- FETCH NEXT @PageSize ROWS ONLY
Here, you have 2 ways to add Web API class. You can add new Web API controller and select model class and data source that will generate API class with entity framework. Another way is to generate it manually. So in this article, I will create a manual API class.
- public partial class GetEmployeeWithPaging_Result {
- public int EmployeeID {
- get;
- set;
- }
- public string FirstName {
- get;
- set;
- }
- public string LastName {
- get;
- set;
- }
- public string City {
- get;
- set;
- }
- public string Region {
- get;
- set;
- }
- public string PostalCode {
- get;
- set;
- }
- public string Country {
- get;
- set;
- }
- public string Notes {
- get;
- set;
- }
- public Nullable < int > TotalCount {
- get;
- set;
- }
- }
Now, add a builder class.
-
-
-
-
-
-
-
-
- public async Task < List < GetEmployeeWithPaging_Result >> GetEmployeeWithPaging(int PageNumber, int PageSize, string OrderBy, string SortDirection) {
- try {
-
- var pageNumber = new SqlParameter("PageNumber", PageNumber == 0 ? (object) DBNull.Value : PageNumber);
- var pageSize = new SqlParameter("PageSize", PageSize == 0 ? (object) DBNull.Value : PageSize);
- var orderBy = new SqlParameter("OrderBy", OrderBy == null ? (object) DBNull.Value : OrderBy);
- var sortDirection = new SqlParameter("SortDirection", SortDirection == null ? (object) DBNull.Value : SortDirection);
-
- var result = await db.Database.SqlQuery < GetEmployeeWithPaging_Result > ("GetEmployeeWithPaging @PageNumber,@PageSize,@OrderBy,@SortDirection", pageNumber, pageSize, orderBy, sortDirection).ToListAsync();
- return result;
- } catch (Exception ex) {
-
- throw ex;
- }
- }
If you want to use a stored procedure, then read my previous article.
Web API
Right click on controller folder, click Add and select Controller and select Web API 2 Controller – Empty.
- [Route("GetEmployeeWithPaging")]
- public async Task < IEnumerable < GetEmployeeWithPaging_Result >> GetEmployeeWithPaging(int PageNumber = 0, int PageSize = 0, string OrderBy = null, string SortDirection = null)
- {
- return await _employeeVMBuilder.GetEmployeeWithPaging(PageNumber, PageSize, OrderBy, SortDirection);
- }
Thus, we are done with Entity framework and API Controller here. Now, install the files given below, using "Manage NuGet Package".
Add JavaScript files and CSS reference in BundleConfig.cs.
- bundles.Add(new ScriptBundle("~/bundles/angular").Include(
- "~/Scripts/angular.js",
- "~/Scripts/angular-route.js",
- "~/Scripts/ui-grid.js",
- "~/Scripts/angular-ui/ui-bootstrap.js",
- "~/Scripts/angular-ui/ui-bootstrap-tpls.js"));
- bundles.Add(new ScriptBundle("~/bundles/employee").Include(
- "~/Angular/app.js",
- "~/Angular/Services/employeeService.js",
- "~/Angular/Controller/employeeController.js",
- "~/Angular/Controller/editEmployeeController.js",
- "~/Angular/Controller/addEmployeeController.js"));
And, render on _layout.cshtml.
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/bootstrap")
- @Scripts.Render("~/bundles/angular")
- @Scripts.Render("~/bundles/employee")
- @RenderSection("scripts", required: false)
Now, add a new Angular Controller with scope. I am using just one script for Module, Service, and Controller. You can have it separate if working on a big project.
Module
-
- var app = angular.module('app', ['ngRoute', 'ui.grid', 'ui.grid.edit', 'ui.grid.pagination', 'ui.grid.autoResize', 'ui.grid.expandable', 'ui.grid.selection', 'ui.grid.pinning', 'ui.bootstrap']).config(function($routeProvider, $locationProvider) {
- $locationProvider.hashPrefix('');
- $routeProvider.when('/', {
- templateUrl: 'Home',
- controller: 'homeController'
- }).when('/employee', {
- templateUrl: 'Employee',
- controller: 'employeeController'
- });
-
-
- });
Service
-
- this.getEmployeeWithPaging = function (PageNumber,PageSize,OrderBy,SortDirection) {
- var req = $http.get('api/EmployeeAPI/GetEmployeeWithPaging', { params: { PageNumber:PageNumber,
- PageSize:PageSize,
- OrderBy:OrderBy,
- SortDirection:SortDirection } });
- return req;
- };
Controller
- app.controller("employeeController", function($scope, $filter, employeeService, $window, $http, $log, $interval, $uibModal) {
- init();
-
- function init() {
-
- var paginationOptions = {
- pageNumber: 1,
- pageSize: 10,
- sort: {
- columnName: 'EmployeeID',
- isAscending: false,
- }
- };
- $scope.currentPage = 1;
- $scope.pageSize = paginationOptions.pageSize;
-
- $scope.loadData = function() {
- employeeService.getEmployeeWithPaging(paginationOptions.pageNumber, paginationOptions.pageSize, null, paginationOptions.sort).then(function(result) {
- $scope.gridOptions.totalItems = result.data[0].TotalCount;
- $scope.totalPage = Math.ceil($scope.gridOptions.totalItems / $scope.pageSize);
- $scope.gridOptions.data = result.data;
- console.log($scope.Employees);
- }, function(error) {
- $window.alert('Oops! Something went wrong while fetching employee data.');
- });
- };
- $scope.loadData();
- $scope.gridOptions = {
- enableRowSelection: true,
- selectionRowHeaderWidth: 35,
- enableRowHeaderSelection: false,
-
- paginationPageSizes: [$scope.pageSize, $scope.pageSize * 2, $scope.pageSize * 3],
- paginationPageSize: paginationOptions.pageSize,
- useExternalPagination: true,
- useExternalSorting: true,
- useExternalFiltering: true,
- enableSorting: true,
- columnDefs: [{
- name: 'Edit',
- field: 'EmployeeID',
- width: '10%',
- enableColumnMenu: false,
- cellTemplate: '<button title="Edit" class="btn btn-xs btn-primary fa fa-edit" ng-click="grid.appScope.editEmployee(row)">Edit </button>',
- width: 50,
- pinnedLeft: false,
- enableHiding: false,
- exporterSuppressExport: true,
- enableSorting: false,
- enableFiltering: false
- }, {
- name: 'First Name',
- field: 'FirstName',
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true,
- enableCellEdit: true,
- }, {
- name: 'Last Name',
- field: 'LastName',
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true,
- enableCellEdit: true,
- }, {
- name: 'City',
- field: 'City',
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true,
- enableCellEdit: true,
- }, {
- name: 'Region',
- field: 'Region',
- enableCellEdit: false,
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true
- }, {
- name: 'Postal Code',
- field: 'PostalCode',
- enableCellEdit: false,
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true
- }, {
- name: 'Country',
- field: 'Country',
- enableCellEdit: false,
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true
- }, {
- name: 'Notes',
- field: 'Notes',
- width: '20%',
- enableCellEdit: false,
- headerCellClass: 'tablesorter-header-inner',
- enableFiltering: true
- }],
-
- enableGridMenu: true,
- enableSelectAll: true,
- exporterMenuPdf: false,
- enableFiltering: true,
- exporterCsvFilename: 'EmployeeList_' + $filter('date')(new Date(), 'MM/dd/yyyy') + '.csv',
- exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
- onRegisterApi: function(gridApi) {
- $scope.gridApi = gridApi;
- gridApi.selection.on.rowSelectionChanged($scope, function(row) {
- var msg = 'row selected ' + row.isSelected;
- $log.log(msg);
- console.log(msg);
-
- });
- gridApi.selection.on.rowSelectionChangedBatch($scope, function(rows) {
- var msg = 'rows changed ' + rows.length;
- $log.log(msg);
-
- console.log(msg);
- });
-
- gridApi.pagination.on.paginationChanged($scope, function(newPage, pageSize) {
- paginationOptions.pageNumber = newPage;
- paginationOptions.pageSize = pageSize;
- $scope.pageSize = pageSize;
- $scope.currentPage = newPage;
- $scope.totalPage = Math.ceil($scope.gridOptions.totalItems / $scope.pageSize);
- $scope.loadData();
- });
-
- $scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {
- if (sortColumns.length == 0) {
- paginationOptions.sort = null;
- } else {
- paginationOptions.sort = sortColumns[0].sort.direction;
- }
- $scope.loadData();
- });
- },
-
-
-
- };
- }
-
- $scope.deleteSelected = function() {
- angular.forEach($scope.gridApi.selection.getSelectedRows(), function(data, index) {
- $scope.employees.splice($scope.employees.lastIndexOf(data), 1);
- });
- }*/
-
- $scope.addEmployee = function() {
- var modalInstance = $uibModal.open({
-
- templateUrl: 'Template/add.html',
- controller: 'addEmployeeController',
-
-
-
- size: 'md',
- backdrop: 'static',
- keyboard: false,
-
- resolve: {
-
-
-
- row: function() {
- return null;
- }
- }
- }).closed.then(function() {
- $scope.RefreshGridData();
- $scope.showGrid = true;
- }, function() {});
- };
-
- $scope.editEmployee = function(row) {
- var modalInstance = $uibModal.open({
-
- templateUrl: 'Template/edit.html',
- controller: 'editEmployeeController',
-
-
-
- size: 'md',
- backdrop: 'static',
- keyboard: false,
-
- resolve: {
-
-
- row: function() {
- return row.entity;
- }
- }
- }).closed.then(function() {
- $scope.RefreshGridData();
- $scope.showGrid = true;
- }, function() {});
- }
-
- $scope.RefreshGridData = function() {
-
-
-
-
-
-
- $scope.loadData();
- }
-
- });
Index
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Employee</h2>
- <div ng-controller="employeeController">
- <div style="padding-left: 30px;">
- <button type="button" id="addRow" class="btn btn-success" ng-click="addEmployee()">Add New Employee</button>
- </div>
- <br />
- <div ui-grid="gridOptions"
- ui-grid-pagination
- ui-grid-selection
- ui-grid-exporter
- ui-grid-resize-columns
- ui-grid-auto-resize
- class="grid">
- </div>
- </div>
As everything is done, run the application.
In the given screenshot, you can see the page index and page size. Click "Next" to see the next page index. Best way to check page index debug your application in debug mode. Every time when you press next you pass updated page index and page size and sorting order. The biggest benefit of server-side custom paging is when you have millions of records and loading is slow then go with custom paging, after custom paging no matter how big database is, you will get result fast because you are fetching 10 records each time.
Conclusion
In this article, we have seen how to implement server-side custom paging with Angular UI-Grid with Web API and Entity Framework in MVC. If you have any questions or comments, drop me a line in the comments section.
In the given screenshot, you can see the page index and page size. Click "Next" to see the next page index. Best way to check page index debug your application in debug mode. Every time when you press next you pass updated page index and page size and sorting order. The biggest benefit of server-side custom paging is when you have millions of records and loading is slow then go with custom paging, after custom paging no matter how big database is, you will get result fast because you are fetching 10 records each time.
Conclusion
In this article, we have seen how to implement server-side custom paging with Angular UI-Grid with Web API and Entity Framework in MVC. If you have any questions or comments, drop me a line in the comments section.
Become a master of the Rubik's Cube with this tutorial. Learn how to solve the cube with the beginner's method!