Integrate Data table with Angular 8 Application With JSON back end


DataTables is a plug-in for the JQuery JavaScript library. It is a highly flexible tool, build upon the foundations of progressive enhancement, that adds all of these advanced features to any HTML table.

Create Project:

Create a new project using angular CLI generator. using the following command to create new project.

ng new angularDatatable

Install Dependencies:

Install the following dependencies using npm.

npm install jquery --save
npm install --save
npm install --save
npm install @types/jquery --save-dev
npm install @types/ --save-dev

project Configuration:

you need to import the jquery and datatable scripts files into angular.json file. Into the styles section you need to import CSS files. Into the script section you need to import JS files. Example code is given below.


"styles": [
"scripts": [

create a new component using the following command

ng generate component Jsondata

Your component template should be like the following

<table #dataTable class="display" style="width:100%">

Create a modal using the following command

ng g m resDataModal

Create a new service using the following command

ng g s Datasource

Your Modal file be like this


export interface ResDataModal {
page: number;
per_page: number;
total: number;
total_pages: number;
data: [];

Your service file should like this


import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {ResDataModal} from '../modals/resDataModal';

providedIn: 'root'
export class DatasourceService {
apiUrl = '';

constructor(private http: HttpClient) { }

getData(): Observable<ResDataModal> {
return this.http.get<ResDataModal>(this.apiUrl);

Your component file be like this

import {Component, OnInit, ViewChild} from '@angular/core';
import {DatasourceService} from '../services/datasource.service';
declare var $;
selector: 'app-json-data',
templateUrl: './json-data.component.html',
styleUrls: ['./json-data.component.scss']
export class JsonDataComponent implements OnInit {

dataTable: any;
dtOptions: any;
tableData = [];
@ViewChild('dataTable', {static: true}) table;

constructor(private dataService: DatasourceService) { }

ngOnInit() {

getDataFromSource() {
this.dataService.getData().subscribe(data => {
this.tableData =;
this.dtOptions = {
data: this.tableData,
columns: [
{title: 'ID', data: 'id'},
{title: 'Email', data: 'email'},
{title: 'First Name', data: 'first_name'},
{title: 'Last Name', data: 'last_name'},
{title: 'Avatar', data: 'avatar'},
}, err => {}, () => {
this.dataTable = $(this.table.nativeElement);