Grid With Paging Using KnockoutJS

Grid with Paging Using KnockoutJS. In this article, we will learn how to display data into Grid using JQWidget library. In this demo, we are going to create a database and a table called customer which contains data. We will use Web API 2 and Entity Framework ORM in order to create Service. Finally, for client side, we are using knockout.js library. I hope you will like this.

Introduction
 
In this article, we will learn how to display data into Grid using JQWidget library. In this demo, we are going to create a database and a table called customer which contains data. We will use Web API 2 and Entity Framework ORM in order to create Service. Finally, for client side, we are using knockout.js library. I hope you will like this.
 
Let’s start.
 
Note
 
You can download zip file from JQWidget
 
Prerequisites
 
As I said before, we are going to use Web API 2 and Entity Framework. For this, you must have Visual Studio 2015 (.Net Framework 4.5.2) and SQL Server.
 
In this post, we are going to -
  • Create Database and table.
  • Create Web API application.
  • Configuring Entity Framework ORM.
  • Implementing the needed http service.
  • Using jqxWidget and knockout.js libraries.
SQL Database part
 
Here, you find the scripts to create database and table.
 
Create Database 
  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object:  Database [DBCustomer]    Script Date: 3/12/2017 4:38:33 AM ******/  
  5. CREATE DATABASE [DBCustomer]  
  6.  CONTAINMENT = NONE  
  7.  ON  PRIMARY   
  8. NAME = N'DBCustomer', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBCustomer.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9.  LOG ON   
  10. NAME = N'DBCustomer_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBCustomer_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [DBCustomer] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [DBCustomer].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [DBCustomer] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [DBCustomer] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [DBCustomer] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [DBCustomer] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [DBCustomer] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [DBCustomer] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [DBCustomer] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [DBCustomer] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [DBCustomer] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [DBCustomer] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [DBCustomer] SET CURSOR_DEFAULT  GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [DBCustomer] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [DBCustomer] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [DBCustomer] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [DBCustomer] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [DBCustomer] SET  DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [DBCustomer] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [DBCustomer] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [DBCustomer] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [DBCustomer] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [DBCustomer] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [DBCustomer] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [DBCustomer] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [DBCustomer] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [DBCustomer] SET  MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [DBCustomer] SET PAGE_VERIFY CHECKSUM    
  98. GO  
  99.   
  100. ALTER DATABASE [DBCustomer] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [DBCustomer] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [DBCustomer] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [DBCustomer] SET  READ_WRITE   
  110. GO   
Create Table 
  1. USE [DBCustomer]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[Customer]    Script Date: 3/12/2017 4:38:53 AM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Customer](  
  15.     [CustID] [int] IDENTITY(1,1) NOT NULL,  
  16.     [FirstName] [varchar](50) NULL,  
  17.     [LastName] [varchar](50) NULL,  
  18.     [Email] [varchar](50) NULL,  
  19.     [Country] [varchar](50) NULL,  
  20.  CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED   
  21. (  
  22.     [CustID] ASC  
  23. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  24. ON [PRIMARY]  
  25.   
  26. GO  
  27.   
  28. SET ANSI_PADDING OFF  
  29. GO   
After creating the table, you can add some records as shown below.
 
create SQL Server table 
 
Create your MVC application
 
Open Visual Studio and select File >> New Project.
 
The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.
 
Create MVC application 
 
Next, new dialog will pop up for selecting the template. We are going choose Web API and click Ok.
 
create Web API 
 
After creating our project, we are going to add ADO.NET Entity Data Model.
 
Adding ADO.NET Entity Data Model
 
In order to ADO.NET Entity Data Model, right click on the project name, click Add > Add New Item. Dialog box will pop up, inside Visual C# select Data then ADO.NET Entity Data Model, and enter name for your Dbcontext model as Model, finally click Add.
 
ADO.NET Entity Data Model 
 
In this level, we are going to choose EF Designer from database as show below.
 
Entity Data Model Wizard 
 
Next step, we need to choose a data connection which should be used to connect to the database. If the connection doesn’t exist, I will invite you to click on new connection button for creating the new one.
 
Entity Data Model Wizard 
 
After clicking on next button, the dialog Entity Data Model Wizard will pop up for choosing the object which we want to use. In this demo we are going to choose customer table and click Finish button. Finally we see that EDMX model generates customer entity.
 
Entity Data Model Wizard 
 
Create a Controller
 
Now, we are going to create a Controller. Right click on the Controllers folder > Add > Controller> selecting Web API 2 Controller – Empty > click Add.
 
Create MVC 5 Controller 
 
Enter Controller name (‘CustomerController’).
 
Create MVC 5 Controller 
 
CustomerController.cs
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Net;    
  5. using System.Net.Http;    
  6. using System.Web.Http;    
  7.     
  8. namespace GridPaging.Controllers    
  9. {    
  10.     public class CustomerController : ApiController    
  11.     {    
  12.     
  13.         //Db Context    
  14.         private DBCustomerEntities db = new DBCustomerEntities();    
  15.     
  16.         public IQueryable<Customer> GetCustomers()    
  17.         {    
  18.             return db.Customers;    
  19.         }    
  20.     
  21.     }    
  22. }   
After creating our Controller, it’s time to implement GetCustomers() function which select all data from customer table.
 
Create HTML page
 
For doing this, right click on project name > Add > HTML Page.
 
 
 
Grid.html 
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>.: Grid - KnockOut JS</title>  
  5.     <meta charset="utf-8" />  
  6.   
  7.     <!-- CSS -->  
  8.     <link href="Content/jqx.base.css" rel="stylesheet" />  
  9.   
  10. </head>  
  11. <body>  
  12.   
  13.     <h3>Customer List</h3>  
  14.     <div>  
  15.         <div id="jqxgrid" data-bind="jqxGrid: { source: CustomerList,  columns: [  
  16.                  { text: 'Customer ID', dataField: 'CustID', width: 60 },  
  17.                  { text: 'First Name', dataField: 'FirstName', width: 100},  
  18.                  { text: 'Last Name', dataField: 'LastName', width: 100 },  
  19.                  { text: 'Email', dataField: 'Email', width: 250 },  
  20.                  { text: 'Country', dataField: 'Country', width: 100 }  
  21.              ], pageable: true, pagesize: 5, autoheight: true}">  
  22.         </div>
  23.     </div>  
  24.   
  25.     <!-- JS  -->  
  26.     <script src="Scripts/jquery-1.10.2.min.js"></script>  
  27.     <script src="Scripts/knockout-3.4.0.js"></script>  
  28.   
  29.     <!-- JQXWidget -->  
  30.     <script src="Scripts/jqxcore.js"></script>  
  31.     <script src="Scripts/jqxdata.js"></script>  
  32.     <script src="Scripts/jqxbuttons.js"></script>  
  33.     <script src="Scripts/jqxscrollbar.js"></script>  
  34.     <script src="Scripts/jqxmenu.js"></script>  
  35.     <script src="Scripts/jqxlistbox.js"></script>  
  36.     <script src="Scripts/jqxdropdownlist.js"></script>  
  37.     <script src="Scripts/jqxgrid.js"></script>  
  38.     <script src="Scripts/jqxgrid.selection.js"></script>  
  39.     <script src="Scripts/jqxgrid.edit.js"></script>  
  40.     <script src="Scripts/jqxgrid.pager.js"></script>  
  41.     <script src="Scripts/jqxknockout.js"></script>  
  42.     <script src="Scripts/demos.js"></script> 
  43.     <script type="text/javascript">
  44.         $(document).ready(function () {
  45.             var viewModel = function () {  
  46.                 var self = this;
  47.                 self.CustID = ko.observable();  
  48.                 self.FirstName = ko.observable();  
  49.                 self.LastName = ko.observable();  
  50.                 self.Email = ko.observable();  
  51.                 self.Country = ko.observable();  
  52.                 self.CustomerList = ko.observableArray([]);  
  53.                 function getCustomers() {  
  54.                     $.ajax({
  55.                         type: 'GET',  
  56.                         url: '/api/Customer',  
  57.                         contentType: 'application/json',  
  58.                         success: function (data) {  
  59.                             self.CustomerList(data);  
  60.                         },  
  61.                         error: function () {  
  62.                             alert('Something Wrong !');  
  63.                         }  
  64.                     });  
  65.                 }
  66.                 getCustomers();  
  67.             }
  68.             ko.applyBindings(new viewModel());
  69.         });
  70.     </script>  
  71. </body>  
  72. </html>  
Output
 
Now, build your application and you can see the following output.
 
Grid using JQWidget library