AngularJS DataTable With Web API

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

  • Create a database.
  • Create Web API Application.
  • Configuring Entity Framework ORM.
  • Create controller.
  • AngularJS part.

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.   
  4. /****** Object:  Database [CustomerDB]    Script Date: 29/03/2017 16:15:43 ******/  
  5. CREATE DATABASE [CustomerDB] ON  PRIMARY   
  6. NAME = N'CustomerDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\CustomerDB.mdf' , SIZE = 11264KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  7.  LOG ON   
  8. 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%)  
  9. GO  
  10.   
  11. ALTER DATABASE [CustomerDB] SET COMPATIBILITY_LEVEL = 100  
  12. GO  
  13.   
  14. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  15. begin  
  16. EXEC [CustomerDB].[dbo].[sp_fulltext_database] @action = 'enable'  
  17. end  
  18. GO  
  19.   
  20. ALTER DATABASE [CustomerDB] SET ANSI_NULL_DEFAULT OFF   
  21. GO  
  22.   
  23. ALTER DATABASE [CustomerDB] SET ANSI_NULLS OFF   
  24. GO  
  25.   
  26. ALTER DATABASE [CustomerDB] SET ANSI_PADDING OFF   
  27. GO  
  28.   
  29. ALTER DATABASE [CustomerDB] SET ANSI_WARNINGS OFF   
  30. GO  
  31.   
  32. ALTER DATABASE [CustomerDB] SET ARITHABORT OFF   
  33. GO  
  34.   
  35. ALTER DATABASE [CustomerDB] SET AUTO_CLOSE OFF   
  36. GO  
  37.   
  38. ALTER DATABASE [CustomerDB] SET AUTO_SHRINK OFF   
  39. GO  
  40.   
  41. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS ON   
  42. GO  
  43.   
  44. ALTER DATABASE [CustomerDB] SET CURSOR_CLOSE_ON_COMMIT OFF   
  45. GO  
  46.   
  47. ALTER DATABASE [CustomerDB] SET CURSOR_DEFAULT  GLOBAL   
  48. GO  
  49.   
  50. ALTER DATABASE [CustomerDB] SET CONCAT_NULL_YIELDS_NULL OFF   
  51. GO  
  52.   
  53. ALTER DATABASE [CustomerDB] SET NUMERIC_ROUNDABORT OFF   
  54. GO  
  55.   
  56. ALTER DATABASE [CustomerDB] SET QUOTED_IDENTIFIER OFF   
  57. GO  
  58.   
  59. ALTER DATABASE [CustomerDB] SET RECURSIVE_TRIGGERS OFF   
  60. GO  
  61.   
  62. ALTER DATABASE [CustomerDB] SET  DISABLE_BROKER   
  63. GO  
  64.   
  65. ALTER DATABASE [CustomerDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  66. GO  
  67.   
  68. ALTER DATABASE [CustomerDB] SET DATE_CORRELATION_OPTIMIZATION OFF   
  69. GO  
  70.   
  71. ALTER DATABASE [CustomerDB] SET TRUSTWORTHY OFF   
  72. GO  
  73.   
  74. ALTER DATABASE [CustomerDB] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  75. GO  
  76.   
  77. ALTER DATABASE [CustomerDB] SET PARAMETERIZATION SIMPLE   
  78. GO  
  79.   
  80. ALTER DATABASE [CustomerDB] SET READ_COMMITTED_SNAPSHOT OFF   
  81. GO  
  82.   
  83. ALTER DATABASE [CustomerDB] SET HONOR_BROKER_PRIORITY OFF   
  84. GO  
  85.   
  86. ALTER DATABASE [CustomerDB] SET RECOVERY FULL   
  87. GO  
  88.   
  89. ALTER DATABASE [CustomerDB] SET  MULTI_USER   
  90. GO  
  91.   
  92. ALTER DATABASE [CustomerDB] SET PAGE_VERIFY CHECKSUM    
  93. GO  
  94.   
  95. ALTER DATABASE [CustomerDB] SET DB_CHAINING OFF   
  96. GO  
  97.   
  98. ALTER DATABASE [CustomerDB] SET  READ_WRITE   
  99. GO   

Create a table

  1. USE [CustomerDB]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[Customers]    Script Date: 29/03/2017 16:16:32 ******/  
  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.     [Id] [int] IDENTITY(1,1) NOT NULL,  
  16.     [FirstName] [varchar](50) NULL,  
  17.     [LastName] [varchar](50) NULL,  
  18.     [City] [varchar](50) NULL,  
  19.     [Country] [varchar](50) NULL,  
  20.  CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED   
  21. (  
  22.     [Id] ASC  
  23. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  24. ON [PRIMARY]  
  25.   
  26. GO  
  27.   
  28. SET ANSI_PADDING OFF  
  29. 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.   
  9. namespace AngularDataTable.Controllers  
  10. {  
  11.     [RoutePrefix("api/Customer")]  
  12.     public class CustomerController : ApiController  
  13.     {  
  14.   
  15.         [Route("Display")]  
  16.         [HttpGet]  
  17.         public IHttpActionResult Display()  
  18.         {  
  19.             if (!ModelState.IsValid)  
  20.             {  
  21.                 return BadRequest(ModelState);  
  22.             }  
  23.   
  24.             using (CustomerDBEntities dc = new CustomerDBEntities())  
  25.             {  
  26.                 var data = dc.Customers.ToList();  
  27.                 return Ok(data);  
  28.             }  
  29.   
  30.         }  
  31.     }  
  32. }   

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.   
  3. function WithPromiseCtrl(DTOptionsBuilder, DTColumnBuilder, $http, $q) {  
  4.     var vm = this;  
  5.     vm.dtOptions = DTOptionsBuilder.fromFnPromise(function () {  
  6.         var defer = $q.defer();  
  7.         $http.get('api/Customer/Display').then(function (result) {  
  8.             defer.resolve(result.data);  
  9.         });  
  10.         return defer.promise;  
  11.     }).withPaginationType('full_numbers');  
  12.   
  13.     vm.dtColumns = [  
  14.         DTColumnBuilder.newColumn('Id').withTitle('ID'),  
  15.         DTColumnBuilder.newColumn('FirstName').withTitle('First Name'),  
  16.         DTColumnBuilder.newColumn('LastName').withTitle('Last Name'),  
  17.         DTColumnBuilder.newColumn('City').withTitle('City'),  
  18.         DTColumnBuilder.newColumn('Country').withTitle('Country')  
  19.     ];  
  20. }   

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.   
  8.     <!-- CSS -->  
  9.       
  10.     <link href="Content/angular-datatables.css" rel="stylesheet" />  
  11.     <link href="Content/DataTables/css/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  12.     <link href="Content/DataTables/css/dataTables.jqueryui.min.css" rel="stylesheet" />  
  13.   
  14. </head>  
  15. <body ng-app="showcase.withPromise">  
  16.   
  17.     <h2>Customer Table</h2>  
  18.     <div ng-controller="WithPromiseCtrl as showCase">  
  19.         <table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="row-border hover"></table>  
  20.     </div>  
  21.   
  22.     <!-- JS -->  
  23.     <script src="Scripts/jquery-1.10.2.min.js"></script>  
  24.     <script src="Scripts/DataTables/jquery.dataTables.min.js"></script>  
  25.     <script src="Scripts/angular.min.js"></script>  
  26.     <script src="Scripts/angular-datatables.min.js"></script>  
  27.   
  28.     <script src="Scripts/AppDataTable.js"></script>  
  29. </body>  
  30. </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.   
  6. <!-- JS -->  
  7. <script src="Scripts/jquery-1.10.2.min.js"></script>  
  8. <script src="Scripts/DataTables/jquery.dataTables.min.js"></script>  
  9. <script src="Scripts/angular.min.js"></script>  
  10. <script src="Scripts/angular-datatables.min.js"></script>  
  11.   
  12. <script src="Scripts/AppDataTable.js"></script>   

Output

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

AngularJS


Similar Articles