Introduction

In this article, we will learn how to display the data from the database in DataTable plugin with AngularJS.

In this article, we are going to

SQL database part

Here, you can find the scripts to create a database and a table.

Create a database

  1. USE [master]
  2. GO
  3. /****** Object: Database [CustomerDB] Script Date: 29/03/2017 16:15:43 ******/
  4. CREATE DATABASE [CustomerDB] ON PRIMARY
  5. ( NAME = N'CustomerDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\CustomerDB.mdf' , SIZE = 11264KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  6. LOG ON
  7. ( NAME = N'CustomerDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\CustomerDB_log.ldf' , SIZE = 26816KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
  8. GO
  9. ALTER DATABASE [CustomerDB] SET COMPATIBILITY_LEVEL = 100
  10. GO
  11. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  12. begin
  13. EXEC [CustomerDB].[dbo].[sp_fulltext_database] @action = 'enable'
  14. end
  15. GO
  16. ALTER DATABASE [CustomerDB] SET ANSI_NULL_DEFAULT OFF
  17. GO
  18. ALTER DATABASE [CustomerDB] SET ANSI_NULLS OFF
  19. GO
  20. ALTER DATABASE [CustomerDB] SET ANSI_PADDING OFF
  21. GO
  22. ALTER DATABASE [CustomerDB] SET ANSI_WARNINGS OFF
  23. GO
  24. ALTER DATABASE [CustomerDB] SET ARITHABORT OFF
  25. GO
  26. ALTER DATABASE [CustomerDB] SET AUTO_CLOSE OFF
  27. GO
  28. ALTER DATABASE [CustomerDB] SET AUTO_SHRINK OFF
  29. GO
  30. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE [CustomerDB] SET CURSOR_CLOSE_ON_COMMIT OFF
  33. GO
  34. ALTER DATABASE [CustomerDB] SET CURSOR_DEFAULT GLOBAL
  35. GO
  36. ALTER DATABASE [CustomerDB] SET CONCAT_NULL_YIELDS_NULL OFF
  37. GO
  38. ALTER DATABASE [CustomerDB] SET NUMERIC_ROUNDABORT OFF
  39. GO
  40. ALTER DATABASE [CustomerDB] SET QUOTED_IDENTIFIER OFF
  41. GO
  42. ALTER DATABASE [CustomerDB] SET RECURSIVE_TRIGGERS OFF
  43. GO
  44. ALTER DATABASE [CustomerDB] SET DISABLE_BROKER
  45. GO
  46. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  47. GO
  48. ALTER DATABASE [CustomerDB] SET DATE_CORRELATION_OPTIMIZATION OFF
  49. GO
  50. ALTER DATABASE [CustomerDB] SET TRUSTWORTHY OFF
  51. GO
  52. ALTER DATABASE [CustomerDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
  53. GO
  54. ALTER DATABASE [CustomerDB] SET PARAMETERIZATION SIMPLE
  55. GO
  56. ALTER DATABASE [CustomerDB] SET READ_COMMITTED_SNAPSHOT OFF
  57. GO
  58. ALTER DATABASE [CustomerDB] SET HONOR_BROKER_PRIORITY OFF
  59. GO
  60. ALTER DATABASE [CustomerDB] SET RECOVERY FULL
  61. GO
  62. ALTER DATABASE [CustomerDB] SET MULTI_USER
  63. GO
  64. ALTER DATABASE [CustomerDB] SET PAGE_VERIFY CHECKSUM
  65. GO
  66. ALTER DATABASE [CustomerDB] SET DB_CHAINING OFF
  67. GO
  68. ALTER DATABASE [CustomerDB] SET READ_WRITE
  69. GO

Create a table

  1. USE [CustomerDB]
  2. GO
  3. /****** Object: Table [dbo].[Customers] Script Date: 29/03/2017 16:16:32 ******/
  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. [Id] [int] IDENTITY(1,1) NOT NULL,
  12. [FirstName] [varchar](50) NULL,
  13. [LastName] [varchar](50) NULL,
  14. [City] [varchar](50) NULL,
  15. [Country] [varchar](50) NULL,
  16. CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
  17. (
  18. [Id] ASC
  19. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  20. ) ON [PRIMARY]
  21. GO
  22. SET ANSI_PADDING OFF
  23. GO

After creating the table, you can add some records, as shown below.

AngularJS

Create your Web API 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.

AngularJS

Now, anew dialog will pop up for selecting the template. We are going to choose Web API template and click OK button.

AngularJS

After creating our project, we are going to add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

To add ADO.NET Entity Framework, right click on Mapping EDMX file >> Add >> Add New Item. Dialog box will pop up. Inside Visual C#, select Data >> ADO.NET Entity Data Model and enter a name for your Dbcontext model as CustomerModel.

AngularJS

Next, we need to choose EF Designer from the database, which model contains.

AngularJS

As you can see below, we need to select Server name, then via dropdown list, connect to a database panel. You should choose your database name. Finally, click OK.

AngularJS

Now, the dialog Entity Data Model Wizard will pop up for choosing the object, which we need to use. In our case, we are going to choose Customers table and click Finish button.

Finally, we will see that EDMX model generates a Customers class.

AngularJS

Create a controller

Now, we are going to create a controller. Right click on Controllers folder >> Add >> Controller>> select Web API 2 Controller – Empty >> click Add.

AngularJS

Enter Controller name (‘CustomerController’).

AngularJS

CustomerController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. using System.Web.Http.Description;
  8. namespace AngularDataTable.Controllers
  9. {
  10. [RoutePrefix("api/Customer")]
  11. public class CustomerController : ApiController
  12. {
  13. [Route("Display")]
  14. [HttpGet]
  15. public IHttpActionResult Display()
  16. {
  17. if (!ModelState.IsValid)
  18. {
  19. return BadRequest(ModelState);
  20. }
  21. using (CustomerDBEntities dc = new CustomerDBEntities())
  22. {
  23. var data = dc.Customers.ToList();
  24. return Ok(data);
  25. }
  26. }
  27. }
  28. }

Here, I am creating Display() action to retrieve all the data from Customer table in JSON format.

AngularJS part

To create new JS file, right click on Scripts folder > Add > JavaScript file.

AngularJS

AppDataTable.js

  1. angular.module('showcase.withPromise', ['datatables']).controller('WithPromiseCtrl', WithPromiseCtrl);
  2. function WithPromiseCtrl(DTOptionsBuilder, DTColumnBuilder, $http, $q) {
  3. var vm = this;
  4. vm.dtOptions = DTOptionsBuilder.fromFnPromise(function () {
  5. var defer = $q.defer();
  6. $http.get('api/Customer/Display').then(function (result) {
  7. defer.resolve(result.data);
  8. });
  9. return defer.promise;
  10. }).withPaginationType('full_numbers');
  11. vm.dtColumns = [
  12. DTColumnBuilder.newColumn('Id').withTitle('ID'),
  13. DTColumnBuilder.newColumn('FirstName').withTitle('First Name'),
  14. DTColumnBuilder.newColumn('LastName').withTitle('Last Name'),
  15. DTColumnBuilder.newColumn('City').withTitle('City'),
  16. DTColumnBuilder.newColumn('Country').withTitle('Country')
  17. ];
  18. }

Create HTML Page

To add HTML page, right click on the project name > Add > HTML page.

AngularJS

Index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title>.: Display Data - Angular DataTable :.</title>
  6. <meta charset="utf-8" />
  7. <!-- CSS -->
  8. <link href="Content/angular-datatables.css" rel="stylesheet" />
  9. <link href="Content/DataTables/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
  10. <link href="Content/DataTables/css/dataTables.jqueryui.min.css" rel="stylesheet" />
  11. </head>
  12. <body ng-app="showcase.withPromise">
  13. <h2>Customer Table</h2>
  14. <div ng-controller="WithPromiseCtrl as showCase">
  15. <table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="row-border hover"></table>
  16. </div>
  17. <!-- JS -->
  18. <script src="Scripts/jquery-1.10.2.min.js"></script>
  19. <script src="Scripts/DataTables/jquery.dataTables.min.js"></script>
  20. <script src="Scripts/angular.min.js"></script>
  21. <script src="Scripts/angular-datatables.min.js"></script>
  22. <script src="Scripts/AppDataTable.js"></script>
  23. </body>
  24. </html>

Note

You can download all the required libraries from AngularJS DataTable.

Don’t forget to add the libraries given below in index.html.

  1. <!-- CSS -->
  2. <link href="Content/angular-datatables.css" rel="stylesheet" />
  3. <link href="Content/DataTables/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
  4. <link href="Content/DataTables/css/dataTables.jqueryui.min.css" rel="stylesheet" />
  5. <!-- JS -->
  6. <script src="Scripts/jquery-1.10.2.min.js"></script>
  7. <script src="Scripts/DataTables/jquery.dataTables.min.js"></script>
  8. <script src="Scripts/angular.min.js"></script>
  9. <script src="Scripts/angular-datatables.min.js"></script>
  10. <script src="Scripts/AppDataTable.js"></script>

Output

Now, you can run your Application. Let’s see the output.

AngularJS