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


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