Steps to be followed.
Step 1
Right click on project name (here "Entities") > Add > New folder.
Step 2
Right Click on that folder > add > new class > Enter Name > Add.
Code Ref
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Entities.ViewModels
- {
- public class EmployeeDataWithPaging
- {
- public List<Employee> Employees { get; set; }
- public int TotalPage { get; set; }
- }
- }
Code Description
In this class, I have metioned two properties.
- Employees for fetching data from database in the table.
- The display of data with paging.
Step 3
Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > select Templete "empty MVC Controller"> Add.
Add a new Action (Get) for fetching data from the database.
Code Ref
- using Entities;
- using Entities.ViewModels;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- namespace SatyaWebApi.Controllers
- {
- public class ExamplePagingController : ApiController
- {
- public HttpResponseMessage Get(int pageNo = 1)
- {
- HttpResponseMessage response = null;
- int totalPage, totalRecord, pageSize;
- pageSize = 5;
- using (CrystalGranite2016Entities dc = new CrystalGranite2016Entities())
- {
- totalRecord = dc.Employees.Count();
- totalPage = (totalRecord / pageSize) + ((totalRecord % pageSize) > 0 ? 1 : 0);
- var record = (from a in dc.Employees
- orderby a.FirstName, a.LastName
- select a).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
- EmployeeDataWithPaging empData = new EmployeeDataWithPaging
- {
- Employees = record,
- TotalPage = totalPage
- };
- response = Request.CreateResponse(HttpStatusCode.OK, empData);
- }
- return response;
- }
- }
- }
Code Description
Here, I have added a new action method to fetch data from the database as well as implement paging.
Here, I declared some variable and mentioned no. of records per page that is 5.
- HttpResponseMessage response = null;
- int totalPage, totalRecord, pageSize;
- pageSize = 5;
Then, fetch records from database using Entity Data Model and calculate total no. of pages based on records per page as mentioned before.
- using (CrystalGranite2016Entities dc = new CrystalGranite2016Entities())
- {
- totalRecord = dc.Employees.Count();
- totalPage = (totalRecord / pageSize) + ((totalRecord % pageSize) > 0 ? 1 : 0);
- var record = (from a in dc.Employees
- orderby a.FirstName, a.LastName
- select a).Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
Then, mention the variable data into properties of class
EmployeeDataWithPaging for No. of employee records with paging.
- EmployeeDataWithPaging empData = new EmployeeDataWithPaging
- {
- Employees = record,
- TotalPage = totalPage
- };
Step 4
Add a new Action into the home controller for getting View for show data.
Code Ref
- @{
- ViewBag.Title = "Satyaprakash-Fetch data from WEB Api and Display data with paging using jquery";
- }
-
- <h2>Satyaprakash Fetch data from WEB Api and Display data with paging using jquery</h2>
- <div id="updatePanel">
-
- </div>
-
- @section Scripts{
- <script>
- $(document).ready(function () {
- var currentPage = 1;
- fetchData(1);
- $('#updatePanel').on('click', '.footerContent a', function (e) {
- e.preventDefault();
- var pageNo = parseInt($(this).html());
- currentPage = pageNo;
- fetchData(currentPage);
- });
- function fetchData(pageNo) {
- var $loading = "<div class='loading'>Please wait...</div>";
- $('#updatePanel').prepend($loading);
- $.ajax({
- url: 'http://localhost:47250/api/ExamplePaging',
- type: 'GET',
- data: { pageNo: pageNo },
- dataType: 'json',
- success: function (data) {
- var $table = $('<table/>').addClass('table table-responsive table-striped table-bordered');
- var $header = $('<thead/>').html('<tr><th style="background-color: Yellow;color: blue">Name</th><th style="background-color: Yellow;color: blue">Email</th><th style="background-color: Yellow;color: blue">City</th><th style="background-color: Yellow;color: blue">Country</th></tr>');
- $table.append($header);
- $.each(data.Employees, function (i, emp) {
- var $row = $('<tr/>');
- $row.append($('<td/>').html(emp.FirstName + ' ' + emp.LastName));
- $row.append($('<td/>').html(emp.EmailID));
- $row.append($('<td/>').html(emp.City));
- $row.append($('<td/>').html(emp.Country));
- $table.append($row);
- });
- var totalPage = parseInt(data.TotalPage);
- var $footer = $('<tr/>');
- var $footerTD = $('<td/>').attr('colspan', 4).addClass('footerContent');
-
- if (totalPage > 0) {
- for (var i = 1; i <= totalPage; i++) {
- var $page = $('<span/>').addClass((i == currentPage) ? "current" : "");
- $page.html((i == currentPage) ? i : "<a href='#'>" + i + "</a>");
- $footerTD.append($page);
- }
- $footer.append($footerTD);
- }
- $table.append($footer);
-
- $('#updatePanel').html($table);
- },
- error: function () {
- alert('Error! Please try again.');
- }
- }).done(function () {
- $loading.remove();
- });
- }
- });
- </script>
- }
-
- <style>
- #updatePanel {
- width: 95%;
- margin: 0 auto;
- position: relative;
- }
-
- .loading {
- float: left;
- position: absolute;
- margin-left: 40%;
- width: 200px;
- top: 100px;
- padding: 3px;
- border: 1px solid rgb(253, 0, 0);
- background-color: rgb(245, 245, 78);
- text-align: center;
- }
-
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
-
- tr:nth-child(even) {
- background-color: #dddddd;
- }
-
- span.current {
- cursor: auto !important;
- background-color: #6E6E6E !important;
- color: #ffffff;
- font-weight: bold;
- padding: 5px 10px;
- border: 1px solid #000000;
- margin-right: 4px;
- }
-
- td.footerContent span a {
- display: inline-block;
- padding: 3px 10px;
- background-color:chartreuse;
- margin-right: 4px;
- border: 1px solid #998787;
- cursor: pointer;
- }
-
- td.footerContent span a {
- text-decoration: none;
- }
-
- td.footerContent {
- text-align: right;
- }
- </style>
Code Description
- var currentPage = 1;
- fetchData(1);
Then, implement
- $('#updatePanel').on('click', '.footerContent a', function (e) {
- e.preventDefault();
- var pageNo = parseInt($(this).html());
- currentPage = pageNo;
- fetchData(currentPage);
- });
Then, create a function for f
- function fetchData(pageNo) {
- }
Then, c
- var $loading = "<div class='loading'>Please wait...</div>";
- $('#updatePanel').prepend($loading);
- $.ajax({
- url: 'http://localhost:47250/api/ExamplePaging',
- type: 'GET',
- data: { pageNo: pageNo },
- dataType: 'json',
- success: function (data) {
- success: function (data) {
- var $table = $('<table/>').addClass('table table-responsive table-striped table-bordered');
- var $header = $('<thead/>').html('<tr><th style="background-color: Yellow;color: blue">Name</th><th style="background-color: Yellow;color: blue">Email</th><th style="background-color: Yellow;color: blue">City</th><th style="background-color: Yellow;color: blue">Country</th></tr>');
- $table.append($header);
- $.each(data.Employees, function (i, emp) {
- var $row = $('<tr/>');
- $row.append($('<td/>').html(emp.FirstName + ' ' + emp.LastName));
- $row.append($('<td/>').html(emp.EmailID));
- $row.append($('<td/>').html(emp.City));
- $row.append($('<td/>').html(emp.Country));
- $table.append($row);
- });
- var totalPage = parseInt(data.TotalPage);
- var $footer = $('<tr/>');
- var $footerTD = $('<td/>').attr('colspan', 4).addClass('footerContent');
-
- if (totalPage > 0) {
- for (var i = 1; i <= totalPage; i++) {
- var $page = $('<span/>').addClass((i == currentPage) ? "current" : "");
- $page.html((i == currentPage) ? i : "<a href='#'>" + i + "</a>");
- $footerTD.append($page);
- }
- $footer.append($footerTD);
- }
- $table.append($footer);
-
- $('#updatePanel').html($table);
- },
- .done(function () {
- $loading.remove();
- });
I have added
- <style>
- #updatePanel {
- width: 95%;
- margin: 0 auto;
- position: relative;
- }
-
- .loading {
- float: left;
- position: absolute;
- margin-left: 40%;
- width: 200px;
- top: 100px;
- padding: 3px;
- border: 1px solid rgb(253, 0, 0);
- background-color: rgb(245, 245, 78);
- text-align: center;
- }
-
- table {
- font-family: arial, sans-serif;
- border-collapse: collapse;
- width: 100%;
- }
-
- td, th {
- border: 1px solid #dddddd;
- text-align: left;
- padding: 8px;
- }
-
- tr:nth-child(even) {
- background-color: #dddddd;
- }
-
- span.current {
- cursor: auto !important;
- background-color: #6E6E6E !important;
- color: #ffffff;
- font-weight: bold;
- padding: 5px 10px;
- border: 1px solid #000000;
- margin-right: 4px;
- }
-
- td.footerContent span a {
- display: inline-block;
- padding: 3px 10px;
- background-color:chartreuse;
- margin-right: 4px;
- border: 1px solid #998787;
- cursor: pointer;
- }
-
- td.footerContent span a {
- text-decoration: none;
- }
-
- td.footerContent {
- text-align: right;
- }
- </style>
OUTPUT
The number of records per page. That is 5 and paging feature.
For loading panel.
Added style sheet for a good look in table and paging.