I'm trying to use Angular DataTables with the server side processing option, but when I try to enable it in their "Angular way example", only the first request gets rendered, the subsequent requests (paging, ordering, searching) are sent but they never update the table.
1 Answers
After a little digging, I found an unrelated user contributed note that suggests that you override the ajax
option with your own function to handle the server side call.
The trick here is to return an empty array to the DataTables callback, so it won't use its renderer to render the table. That will be done by Angular. It's also a good idea to specify the columns names to the server.
ngOnInit(): void {
var that = this;
this.dtOptions = {
pagingType: 'full_numbers',
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
that.http
.post<DataTablesResponse>('/api/Persons', dataTablesParameters, {})
.subscribe(resp => {
that.persons = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: [],
});
});
},
columns: [
{ data: "id" },
{ data: "firstName" },
{ data: "lastName" },
],
};
}
Since DataTables will think there are no rows to display, it will show the "No data available" message. The simplest way to handle it is to hide it with CSS. You can add it to your global styles.css:
.dataTables_empty {
display: none;
}
then show it yourself in the template:
<tr *ngIf="persons?.length == 0">
<td colspan="3" class="no-data-available">No data!</td>
</tr>
So here's the complete code. Tested with Angular 5.0.0, datatables.net 1.10.16 and angular-datatables 5.0.0:
angular-way-server-side.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { DataTablesResponse } from '../datatables/datatables-response';
import { Person } from './person';
@Component({
selector: 'app-angular-way-server-side',
templateUrl: 'angular-way-server-side.component.html',
styleUrls: ['angular-way-server-side.component.css'],
})
export class AngularWayServerSideComponent implements OnInit {
dtOptions: DataTables.Settings = {};
persons: Person[];
constructor(private http: HttpClient) { }
ngOnInit(): void {
var that = this;
this.dtOptions = {
pagingType: 'full_numbers',
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
that.http
.post<DataTablesResponse>('/api/Persons', dataTablesParameters, {})
.subscribe(resp => {
that.persons = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: [],
});
});
},
columns: [
{ data: "id" },
{ data: "firstName" },
{ data: "lastName" },
],
};
}
}
angular-way-server-side.component.html
<table datatable [dtOptions]="dtOptions" class="row-border hover">
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let person of persons">
<td>{{ person.id }}</td>
<td>{{ person.firstName }}</td>
<td>{{ person.lastName }}</td>
</tr>
<tr *ngIf="persons?.length == 0">
<td colspan="3" class="no-data-available">No data!</td>
</tr>
</tbody>
</table>
angular-way-server-side.component.css
.no-data-available {
text-align: center;
}
person.ts
export class Person {
id: number;
firstName: string;
lastName: string;
}
datatables-response.ts
export class DataTablesResponse {
data: any[];
draw: number;
recordsFiltered: number;
recordsTotal: number;
}
src/styles.css
.dataTables_empty {
display: none;
}
The server side is implemented pretty much the same way as if you were using DataTables with JavaScript/jQuery. You can see a very simple sample implementation in PHP.

- 6,651
- 5
- 38
- 62
-
Could you check this link https://stackoverflow.com/questions/47568013/get-more-than-1000-row-in-the-data-table-in-angular-js-from-mongodb – Varun Sharma Jan 19 '18 at 12:37
-
2how it will check for server side paging, sorting etc, what is the structure of ur ajax post and what is the supposed output from the same..? – Sunil Chaudhary Jun 19 '18 at 05:51
-
what can be done if i want to get the column names as well using API call? – Anmol G Jul 14 '18 at 10:05
-
@SunilChaudhary I've edited the answer and added a link to a server side example in PHP used with this implementation, hope it helps! – Marcos Dimitrio Jul 14 '18 at 16:46
-
@AnmolG you need to open a new question for that, answering this here would make this question too broad, I'm sorry mate. – Marcos Dimitrio Jul 14 '18 at 16:54
-
thanks @MarcosDimitrio..up-vote you but still confusion where are the parameters like :- current page number, paging size etc(for api parameters) and where are you setting these.. – Sunil Chaudhary Jul 24 '18 at 13:36
-
Please note: Using this method will have other consequences down the road. For example, the select plugin will not work nor will the "expanding of rows". Without the data going through the "data: [] param". I have yet to figure a work around – JCircio Oct 15 '18 at 15:35
-
I am using the same for server side ajax, but I also using the datatable buttons for print and export data in excel, Only headers are getting print not rows in a table – deepak bhardwaj Oct 29 '18 at 05:12
-
I am using the same for server side ajax, but I also using the datatable buttons for print and export data in excel, Only headers are getting print not rows in a table – Pavan Hukerikar Nov 16 '18 at 05:24
-
@MarcosDimitrio [If anybody knows it possible way plz tell me](https://github.com/l-lin/angular-datatables/issues/1391). – Jai Kumaresh Jun 24 '19 at 14:07
-
i want this datatable with get request – Pavan Nagadiya Jul 17 '19 at 04:56
-
I am getting this "Http failure response for http://localhost:3000/api/data: 404 Not Found" when i try to hit on localhost api. Please can anyone help me. – Mohammad Quanit Aug 18 '19 at 13:33
-
Why this person.ts and datatables-response.ts are used? – Navneet Kumar Jan 03 '20 at 12:10