Introduction
In this article, we will see how we can group the data in datatable plugin, using MVC 5, EF, and AngularJS.
Today, my requirement is to develop a Grid that allows us to display customers’ data by each country. As I said before, we will use MVC application in Visual Studio 2015 to achieve our goal.
Prerequisites
We are going to use datatable plugin for grouping the customers’ data in our MVC application. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.
SQL Database part
Here, you can find the scripts to create database and table.
Create Database
Create Table
- USE [CustomerDB]
- GO
-
- /****** Object: Table [dbo].[Customers] Script Date: 9/11/2016 4:53:43 AM ******/
- SET ANSI_NULLS ON
- GO
-
- SET QUOTED_IDENTIFIER ON
- GO
-
- SET ANSI_PADDING ON
- GO
-
- CREATE TABLE [dbo].[Customers](
- [CustomerID] [int] NOT NULL,
- [CustomerName] [varchar](50) NULL,
- [CustomerEmail] [varchar](50) NULL,
- [CustomerZipCode] [int] NULL,
- [CustomerCountry] [varchar](50) NULL,
- [CustomerCity] [varchar](50) NULL,
- CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
- (
- [CustomerID] 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 for Demo 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.
The next step is to select the template. In this example, we need to choose MVC template and click OK.
Now, we are going to add ADO.NET Entity Data Model. Let’s do it.
Adding ADO.NET Entity Data Model
For adding ADO.NET Entity Framework. Right click on the project name, click Add >> Add New Item. Opens up a pop-up window.
Inside Visual C#, select Data >> ADO.NET Entity Data Model, and enter the name for your Dbcontext model as DbContextCustomer. Finally, click Add.
Now, we are going to choose EF Designer from database, as show below.
After clicking Next button, a new dialog box will pop up with the name Connection Properties. You need to enter your server name here. In order to connect to a database panel, select your database (Customer DB) via dropdown list. Then, click OK.
Now, Entity Data Model Wizard will pop up for choosing the object which we want to use. In our case, we choose Customers table and click Finish button. Finally, we see that EDMX model generates Customer class.
Create a Controller
Right click on the Controllers folder > Add > Controller> select "MVC 5 Controller – Empty" > click Add.
Enter Controller name (‘CustomerController’).
CustomerController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace Data_Grouping_in_AngularJS.Controllers
- {
- public class CustomerController : Controller
- {
-
- private CustomerDBEntities context = new CustomerDBEntities();
-
-
-
- public ActionResult Index()
- {
- return View();
- }
-
- public JsonResult GetCustomerList()
- {
- var CustomerList = from c in context.Customers
- select new
- {
- c.CustomerCountry,
- c.CustomerName,
- c.CustomerEmail,
- c.CustomerZipCode,
- c.CustomerCity
- };
-
- return Json(CustomerList, JsonRequestBehavior.AllowGet);
- }
- }
- }
As you can see, I am creating GetCustomerList() action to retrieve the data from Customers table in JSON format.
Adding View
Right click on Index() action, select Add View. In the popped up dialog box, write a name for your View. Finally, click Add.
Note: - Don’t forget to download the following libraries from jqxwidgets.
- <!-- CSS -->
- <link href="~/Content/jqx.base.css" rel="stylesheet" />
- <!-- JS -->
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jqxcore.js"></script>
- <script src="~/Scripts/jqxdata.js"></script>
- <script src="~/Scripts/jqxangular.js"></script>
- <script src="~/Scripts/jqxbuttons.js"></script>
- <script src="~/Scripts/jqxscrollbar.js"></script>
- <script src="~/Scripts/jqxdatatable.js"></script>
- <script src="~/Scripts/demos.js"></script>
Index.cshtml
- @{
- ViewBag.Title = "Index";
- }
-
- @section scripts{
-
- <!-- CSS -->
- <link href="~/Content/jqx.base.css" rel="stylesheet" />
- <!-- JS -->
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jqxcore.js"></script>
- <script src="~/Scripts/jqxdata.js"></script>
- <script src="~/Scripts/jqxangular.js"></script>
- <script src="~/Scripts/jqxbuttons.js"></script>
- <script src="~/Scripts/jqxscrollbar.js"></script>
- <script src="~/Scripts/jqxdatatable.js"></script>
- <script src="~/Scripts/demos.js"></script>
-
- <script type="text/javascript">
-
- var myApp = angular.module('myApp', ['jqwidgets']);
- myApp.controller('GroupingCtrl',['$scope', function ($scope) {
-
-
- $scope.gridSettings = {
-
- source: new $.jqx.dataAdapter({
-
- dataType: "json",
- dataFields: [
-
- { name: 'CustomerCountry', type: 'string' },
- { name: 'CustomerName', type: 'string' },
- { name: 'CustomerEmail', type: 'string' },
- { name: 'CustomerZipCode', type: 'int' },
- { name: 'CustomerCity', type: 'string' }
-
- ],
- url: 'GetCustomerList'
-
-
- }),
- pageable: true,
- altRows: true,
- sortable: true,
- groups: ['CustomerCountry'],
- width: 850,
- groupsRenderer: function (value, rowData, level) {
- return "Country : " + value;
- },
- columns: [
-
- { text: 'Customer Country', cellsAlign: 'left', align: 'left', dataField: 'CustomerCountry', width: 130 },
- { text: 'Customer Name', cellsAlign: 'left', align: 'left', dataField: 'CustomerName', width: 180 },
- { text: 'Customer Email', dataField: 'CustomerEmail', cellsAlign: 'right', align: 'right', width: 250 },
- { text: 'Customer ZipCode', dataField: 'CustomerZipCode', align: 'right', cellsAlign: 'right', width: 110 },
- { text: 'Customer City', cellsAlign: 'center', align: 'center', dataField: 'CustomerCity' }
- ]
-
- };
-
- }]);
-
-
- </script>
-
- }
- <h2>Data Grouping in AngularJS DataTable</h2>
- <div ng-app="myApp" ng-controller="GroupingCtrl">
- <jqx-data-table jqx-settings="gridSettings"></jqx-data-table>
- </div>
Output
That's it. Please send your feedback and queries in comments box.