How to use server side option in Angular DataTables with the Angular way example?
Asked Answered
E

1

5

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.

Edsel answered 30/12, 2017 at 2:28 Comment(0)
E
17

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.

Edsel answered 30/12, 2017 at 2:28 Comment(13)
Could you check this link #47568513Wellbeing
how 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..?Calcariferous
what can be done if i want to get the column names as well using API call?Ivo
@SunilChaudhary I've edited the answer and added a link to a server side example in PHP used with this implementation, hope it helps!Edsel
@AnmolG you need to open a new question for that, answering this here would make this question too broad, I'm sorry mate.Edsel
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..Calcariferous
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 aroundAfterbody
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 tableEggleston
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 tableByrdie
@MarcosDimitrio If anybody knows it possible way plz tell me.Shirring
i want this datatable with get requestGadolinite
I am getting this "Http failure response for localhost:3000/api/data: 404 Not Found" when i try to hit on localhost api. Please can anyone help me.Dangelo
Why this person.ts and datatables-response.ts are used?Anosmia

© 2022 - 2024 — McMap. All rights reserved.