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
- USE [master]
- GO
- /****** Object: Database [DBCustomer] Script Date: 3/12/2017 4:38:33 AM ******/
- CREATE DATABASE [DBCustomer]
- CONTAINMENT = NONE
- ON PRIMARY
- ( NAME = N'DBCustomer', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBCustomer.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
- LOG ON
- ( 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%)
- GO
- ALTER DATABASE [DBCustomer] SET COMPATIBILITY_LEVEL = 110
- GO
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC [DBCustomer].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE [DBCustomer] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE [DBCustomer] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE [DBCustomer] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE [DBCustomer] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE [DBCustomer] SET ARITHABORT OFF
- GO
- ALTER DATABASE [DBCustomer] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE [DBCustomer] SET AUTO_CREATE_STATISTICS ON
- GO
- ALTER DATABASE [DBCustomer] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE [DBCustomer] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE [DBCustomer] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE [DBCustomer] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE [DBCustomer] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE [DBCustomer] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE [DBCustomer] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE [DBCustomer] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE [DBCustomer] SET DISABLE_BROKER
- GO
- ALTER DATABASE [DBCustomer] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE [DBCustomer] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE [DBCustomer] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE [DBCustomer] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE [DBCustomer] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE [DBCustomer] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE [DBCustomer] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE [DBCustomer] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE [DBCustomer] SET MULTI_USER
- GO
- ALTER DATABASE [DBCustomer] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE [DBCustomer] SET DB_CHAINING OFF
- GO
- ALTER DATABASE [DBCustomer] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
- GO
- ALTER DATABASE [DBCustomer] SET TARGET_RECOVERY_TIME = 0 SECONDS
- GO
- ALTER DATABASE [DBCustomer] SET READ_WRITE
- GO
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
- {
- //Db Context
- private DBCustomerEntities db = new DBCustomerEntities();
- public IQueryable<Customer> GetCustomers()
- {
- return db.Customers;
- }
- }
- }
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.
Join the conversation! Your thoughts help the community grow.