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
Create Table
- USE [DBCustomer]
- GO
-
- /****** Object: Table [dbo].[Customer] Script Date: 3/12/2017 4:38:53 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[Customer](
- [CustID] [int] IDENTITY(1,1) NOT NULL,
- [FirstName] [varchar](50) NULL,
- [LastName] [varchar](50) NULL,
- [Email] [varchar](50) NULL,
- [Country] [varchar](50) NULL,
- CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
- (
- [CustID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
-
- GO
-
- SET ANSI_PADDING OFF
- GO
After creating the table, you can add some records as shown below.
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.
Next, new dialog will pop up for selecting the template. We are going choose Web API and click Ok.
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.
In this level, we are going to choose EF Designer from database as show below.
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.
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.
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.
Enter Controller name (‘CustomerController’).
CustomerController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
-
- namespace GridPaging.Controllers
- {
- public class CustomerController : ApiController
- {
-
-
- private DBCustomerEntities db = new DBCustomerEntities();
-
- public IQueryable<Customer> GetCustomers()
- {
- return db.Customers;
- }
-
- }
- }
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
- <!DOCTYPE html>
- <html>
- <head>
- <title>.: Grid - KnockOut JS</title>
- <meta charset="utf-8" />
-
- <!-- CSS -->
- <link href="Content/jqx.base.css" rel="stylesheet" />
-
- </head>
- <body>
-
- <h3>Customer List</h3>
- <div>
- <div id="jqxgrid" data-bind="jqxGrid: { source: CustomerList, columns: [
- { text: 'Customer ID', dataField: 'CustID', width: 60 },
- { text: 'First Name', dataField: 'FirstName', width: 100},
- { text: 'Last Name', dataField: 'LastName', width: 100 },
- { text: 'Email', dataField: 'Email', width: 250 },
- { text: 'Country', dataField: 'Country', width: 100 }
- ], pageable: true, pagesize: 5, autoheight: true}">
- </div>
-
-
- </div>
-
-
-
- <!-- JS -->
- <script src="Scripts/jquery-1.10.2.min.js"></script>
- <script src="Scripts/knockout-3.4.0.js"></script>
-
- <!-- JQXWidget -->
- <script src="Scripts/jqxcore.js"></script>
- <script src="Scripts/jqxdata.js"></script>
- <script src="Scripts/jqxbuttons.js"></script>
- <script src="Scripts/jqxscrollbar.js"></script>
- <script src="Scripts/jqxmenu.js"></script>
- <script src="Scripts/jqxlistbox.js"></script>
- <script src="Scripts/jqxdropdownlist.js"></script>
- <script src="Scripts/jqxgrid.js"></script>
- <script src="Scripts/jqxgrid.selection.js"></script>
- <script src="Scripts/jqxgrid.edit.js"></script>
- <script src="Scripts/jqxgrid.pager.js"></script>
- <script src="Scripts/jqxknockout.js"></script>
- <script src="Scripts/demos.js"></script>
-
-
- <script type="text/javascript">
-
- $(document).ready(function () {
-
- var viewModel = function () {
-
- var self = this;
-
- self.CustID = ko.observable();
- self.FirstName = ko.observable();
- self.LastName = ko.observable();
- self.Email = ko.observable();
- self.Country = ko.observable();
-
- self.CustomerList = ko.observableArray([]);
-
- function getCustomers() {
-
- $.ajax({
-
- type: 'GET',
- url: '/api/Customer',
- contentType: 'application/json',
- success: function (data) {
- self.CustomerList(data);
- },
- error: function () {
- alert('Something Wrong !');
- }
- });
- }
-
- getCustomers();
- }
-
- ko.applyBindings(new viewModel());
-
- });
-
-
- </script>
- </body>
- </html>
Output
Now, build your application and you can see the following output.