Angular 9 Datatables Ajax Example Tutorial

Here, i will show you angular 9 datatable example with pagination. Here you will learn angular 9 datatables dtoptions example. In this article, we will implement a angular 9 datatables example. We will look at example of angular 9 datatable example. you will do the following things for how to integrate datatables in angular 9.

In this tutorial, i will give you simple working example of how to integrate datatables in angular 9 application. we will use angular datatable with ajax example. we will use third party api and get all post data then display in table format using datatable. using datatable you can easily search, sorting and paginate your data.

You can see step by step bellow example of use datatable in angular 8/9 application. you can also see bellow preview:


Step 1: Create New App

You can easily create your angular app using bellow command:

ng new ngDatatable

Step 2: Install Npm Packages

In this step, we will install list of following npm packages for datatables angular. so let's run both command:

npm install jquery --save

npm install datatables.net --save

npm install datatables.net-dt --save

npm install angular-datatables --save

npm install @types/jquery --save-dev

npm install @types/datatables.net --save-dev

After successfully installed all packages we need to add css and js files on angular.json file. so let's add as like bellow:




"projects": {

"your-app-name": {

"architect": {

"build": {

"options": {

"styles": [



"scripts": [






Step 3: Import DataTablesModule

Now, here we will import DataTablesModule from angular-datatables and then we add on declarations part. we also need to import HttpClientModule for getting data. so let's update app.module.ts file as like bellow:


import { BrowserModule } from '@angular/platform-browser';

import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { DataTablesModule } from 'angular-datatables';

import { HttpClientModule } from '@angular/common/http';


declarations: [



imports: [





providers: [],

bootstrap: [AppComponent]


export class AppModule { }

Step 4: Update Component ts File

Here, we will update app.component.ts file here, in this file we will write datatable configuration using dtOptions and call api for data.

You can update as bellow app.component.ts file.


import { Component, OnInit } from '@angular/core';

import { HttpClient } from '@angular/common/http';


selector: 'app-root',

templateUrl: './app.component.html',

styleUrls: ['./app.component.css']


export class AppComponent implements OnInit{

title = 'datatables';

dtOptions: DataTables.Settings = {};


constructor(private http: HttpClient) { }

ngOnInit(): void {

this.dtOptions = {

pagingType: 'full_numbers',

pageLength: 5,

processing: true



.subscribe(posts => {

this.posts = posts;




Step 5: Update HTML File

Here, we will update html file as like bellow, so update it as like bellow:


<h1>Angular Datatables Step by Step Example - HDTuto.com</h1>

<table datatable [dtOptions]="dtOptions" class="row-border hover">









<tr *ngFor="let post of posts">

<td>{{ post.id }}</td>

<td>{{ post.title }}</td>

<td>{{ post.body }}</td>




Now you can run angular 9 app:

Run Angular App:

ng serve

I hope it can help you...

July 21, 2020 | Category : Angular

Blog Search