Data Grouping In AngularJS Datatable Using ASP.NET MVC 5

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
  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object: Database [CustomerDB] Script Date: 9/11/2016 4:54:28 AM ******/  
  5. CREATE DATABASE [CustomerDB]  
  6. CONTAINMENT = NONE  
  7. ON PRIMARY   
  8. NAME = N'CustomerDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9. LOG ON   
  10. NAME = N'CustomerDB_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustomerDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [CustomerDB] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [CustomerDB].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [CustomerDB] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [CustomerDB] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [CustomerDB] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [CustomerDB] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [CustomerDB] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [CustomerDB] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [CustomerDB] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [CustomerDB] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [CustomerDB] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [CustomerDB] SET CURSOR_DEFAULT GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [CustomerDB] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [CustomerDB] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [CustomerDB] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [CustomerDB] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [CustomerDB] SET DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [CustomerDB] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [CustomerDB] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [CustomerDB] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [CustomerDB] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [CustomerDB] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [CustomerDB] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [CustomerDB] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [CustomerDB] SET MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [CustomerDB] SET PAGE_VERIFY CHECKSUM   
  98. GO  
  99.   
  100. ALTER DATABASE [CustomerDB] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [CustomerDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [CustomerDB] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [CustomerDB] SET READ_WRITE   
  110. GO  
Create Table
  1. USE [CustomerDB]  
  2. GO  
  3.   
  4. /****** Object: Table [dbo].[Customers] Script Date: 9/11/2016 4:53:43 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].[Customers](  
  15. [CustomerID] [intNOT NULL,  
  16. [CustomerName] [varchar](50) NULL,  
  17. [CustomerEmail] [varchar](50) NULL,  
  18. [CustomerZipCode] [intNULL,  
  19. [CustomerCountry] [varchar](50) NULL,  
  20. [CustomerCity] [varchar](50) NULL,  
  21. CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED   
  22. (  
  23. [CustomerID] ASC  
  24. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  25. ON [PRIMARY]  
  26.   
  27. GO  
  28.   
  29. 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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace Data_Grouping_in_AngularJS.Controllers  
  8. {  
  9.     public class CustomerController : Controller  
  10.     {  
  11.         //dbContext  
  12.         private CustomerDBEntities context = new CustomerDBEntities();  
  13.   
  14.   
  15.         // GET: Customer  
  16.         public ActionResult Index()  
  17.         {  
  18.             return View();  
  19.         }  
  20.   
  21.         public JsonResult GetCustomerList()  
  22.         {  
  23.             var CustomerList = from c in context.Customers  
  24.                                select new  
  25.                                {  
  26.                                    c.CustomerCountry,  
  27.                                    c.CustomerName,  
  28.                                    c.CustomerEmail,  
  29.                                    c.CustomerZipCode,  
  30.                                    c.CustomerCity  
  31.                                };  
  32.   
  33.             return Json(CustomerList, JsonRequestBehavior.AllowGet);  
  34.         }  
  35.     }  
  36. }  
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.
  1. <!-- CSS -->  
  2. <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  3. <!-- JS -->  
  4. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  5. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  6. <script src="~/Scripts/jqxcore.js"></script>  
  7. <script src="~/Scripts/jqxdata.js"></script>  
  8. <script src="~/Scripts/jqxangular.js"></script>  
  9. <script src="~/Scripts/jqxbuttons.js"></script>  
  10. <script src="~/Scripts/jqxscrollbar.js"></script>  
  11. <script src="~/Scripts/jqxdatatable.js"></script>  
  12. <script src="~/Scripts/demos.js"></script>  
Index.cshtml
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. @section scripts{  
  6.   
  7.     <!-- CSS -->  
  8.     <link href="~/Content/jqx.base.css" rel="stylesheet" />  
  9.     <!-- JS -->  
  10.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>  
  11.     <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  12.     <script src="~/Scripts/jqxcore.js"></script>  
  13.     <script src="~/Scripts/jqxdata.js"></script>  
  14.     <script src="~/Scripts/jqxangular.js"></script>  
  15.     <script src="~/Scripts/jqxbuttons.js"></script>  
  16.     <script src="~/Scripts/jqxscrollbar.js"></script>  
  17.     <script src="~/Scripts/jqxdatatable.js"></script>  
  18.     <script src="~/Scripts/demos.js"></script>  
  19.       
  20.     <script type="text/javascript">  
  21.   
  22.         var myApp = angular.module('myApp', ['jqwidgets']);  
  23.         myApp.controller('GroupingCtrl',['$scope'function ($scope) {  
  24.   
  25.             //create AngularJS DataTable  
  26.             $scope.gridSettings = {  
  27.   
  28.                 source: new $.jqx.dataAdapter({  
  29.   
  30.                     dataType: "json",  
  31.                     dataFields: [  
  32.   
  33.                         { name: 'CustomerCountry', type: 'string' },  
  34.                         { name: 'CustomerName', type: 'string' },  
  35.                         { name: 'CustomerEmail', type: 'string' },  
  36.                         { name: 'CustomerZipCode', type: 'int' },  
  37.                         { name: 'CustomerCity', type: 'string' }  
  38.   
  39.                     ],  
  40.                     url: 'GetCustomerList'  
  41.                       
  42.   
  43.                 }),  
  44.                 pageable: true,  
  45.                 altRows: true,  
  46.                 sortable: true,  
  47.                 groups: ['CustomerCountry'],  
  48.                 width: 850,  
  49.                 groupsRenderer: function (value, rowData, level) {  
  50.                     return "Country : " + value;  
  51.                 },  
  52.                 columns: [  
  53.   
  54.                   { text: 'Customer Country', cellsAlign: 'left', align: 'left', dataField: 'CustomerCountry', width: 130 },  
  55.                   { text: 'Customer Name', cellsAlign: 'left', align: 'left', dataField: 'CustomerName', width: 180 },  
  56.                   { text: 'Customer Email', dataField: 'CustomerEmail', cellsAlign: 'right', align: 'right', width: 250 },  
  57.                   { text: 'Customer ZipCode', dataField: 'CustomerZipCode', align: 'right', cellsAlign: 'right', width: 110 },  
  58.                   { text: 'Customer City', cellsAlign: 'center', align: 'center', dataField: 'CustomerCity' }  
  59.                 ]  
  60.   
  61.             };  
  62.   
  63.         }]);  
  64.   
  65.   
  66.     </script>  
  67.       
  68. }  
  69. <h2>Data Grouping in AngularJS DataTable</h2>  
  70. <div ng-app="myApp" ng-controller="GroupingCtrl">  
  71.     <jqx-data-table jqx-settings="gridSettings"></jqx-data-table>  
  72. </div>  
Output

 


That's it. Please send your feedback and queries in comments box.