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.
- UI-Grid With AngularJS And WebAPI
- Export Data In Angular-UI-Grid Using WebAPI
- Filtering In UI-Grid With AngularJS And WebAPI
- Custom Scroll In AngularJS-UI-Grid With Web API
- Expandable grid in AngularJS-UI-Grid with Web API
- CRUD Operation with Model dialog using UI-Grid in AngularJS and Web API
- How To Customize UI-Grid Height Dynamically
- Custom Paging In Angular-UI Grid
- Two Ways To Get Data From Entity Framework
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
- /****** Object: StoredProcedure [dbo].[GetEmployeeWithPaging] Script Date: 11/13/2017 12:56:50 PM ******/
- 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
- 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;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="PageNumber"></param>
- /// <param name="PageSize"></param>
- /// <param name="OrderBy"></param>
- /// <param name="SortDirection"></param>
- /// <returns></returns>
- public async Task < List < GetEmployeeWithPaging_Result >> GetEmployeeWithPaging(int PageNumber, int PageSize, string OrderBy, string SortDirection) {
- try {
- // Create the SQL param with the id provided by the caller
- 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);
- // Create an enumerated list of SearchProgramOptions objects
- var result = await db.Database.SqlQuery < GetEmployeeWithPaging_Result > ("GetEmployeeWithPaging @PageNumber,@PageSize,@OrderBy,@SortDirection", pageNumber, pageSize, orderBy, sortDirection).ToListAsync();
- return result;
- } catch (Exception ex) {
- // Something didn't go as expected - this Exception handler should be more verbose
- 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);
- }

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
- //Define an angular module for our app
- 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'
- });
- //$locationProvider.html5Mode(true);
- //$locationProvider.html5Mode(true).hashPrefix('*');
- });
Service
- //get employees with paging params
- 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



Purushottam RathorePosted Nov 27, 2017, 6:37 AM
Excellent stuff for who are learning Angular. This is very helpful for me. Thank you very much Raj Kumar sir.