question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server side the Angular way - unable to load datatable with custom GET Web-API

See original GitHub issue

Hi, I am using Angular 5 with data-table using “https://l-lin.github.io/angular-datatables/#/getting-started”. Till now I was loading the table using the method in “Angular Way” section:

this.http.get('data/data.json')
      .map(this.extractData)
      .subscribe(persons => {
        this.persons = persons;
        // Calling the DT trigger to manually render the table
        this.dtTrigger.next();
      });

But since now the data which I get from my web service is in thousands, I am facing an issue, where the normal html table loads and then the data-table format loads. It takes about 2-3 seconds, which looks very ugly.

I understood that using the method mentioned in “Server side the Angular way”, can resolve this issue:

ngOnInit(): void {
        const that = this;

        this.dtOptions = {
            pagingType: 'full_numbers',
            pageLength: 2,
            serverSide: true,
            processing: true,
            ajax: (dataTablesParameters: any, callback) => {
              that.http
                  .post<DataTablesResponse>(
                    'https://angular-datatables-demo-server.herokuapp.com/',
                    dataTablesParameters, {})
                  .subscribe(resp => {
                      that.persons = resp.data;


                     callback({
                          recordsTotal: resp.recordsTotal,
                          recordsFiltered: resp.recordsFiltered,
                          data: [],
                      });
                  });
            },
            columns: [
                { data: 'id' }, { data: 'firstName' }, { data: 'lastName' },
            ],
        };
    }

But on using this method with my web service having get request and sends some token data in header, the other features of data-table don’t work. For example, all the data loads in one page, and there is no pagination. Search box is visible, but it also doesn’t work. Following is my code:

HTML(accesspoints.component.html):

<table datatable [dtOptions]="dtOptions" class="table table-bordered" id="apTable" width="100%">
	<thead>
		<tr>
			<th class="sorting_disabled text-center">
				<input type="checkbox" value="" id="selectAllCheck" [(ngModel)]="isChecked" (change)="checkAllRows(isChecked)" >
			</th>
			<th>AP Serial</th>
			<th>AP Name</th>
			<th>Mac Address</th>
			<th>Cluster</th>
			<th>Zone</th>
			<th>Last Contacted</th>
		</tr>
	</thead>
	<tbody>
		<tr *ngFor="let ap of this.apsList">
			<td class="text-center">
				<input type="checkbox" value="" [(ngModel)]="ap.selectcheckbox">
			</td>
			<td>{{ap.apserial}}</td>
			<td>{{ap.apname}}</td>
			<td>{{ap.mac}}</td>
			<td>{{ap.clustername}}</td>
			<td>{{ap.zonename}}</td>
			<td class="text-center" *ngIf="ap.last_contacted">{{ap.last_contacted | date: 'MM/dd/yyyy hh:mm:ss'}}</td>
			<td class="text-center" *ngIf="!ap.last_contacted">N/A</td>
		</tr>
	</tbody>
</table>

TYPESCRIPT(accesspoint.component.ts):

ngOnInit(): void {
		$('body').css('background-color', '#D9E0E7');
		$('#overlay').show();
const newHttpOptions = {
			headers: new HttpHeaders({ 'Content-Type': 'application/json',
				'x-access-token' : this._cookieService.get('TOKEN') })
		};


	view.dtOptions = {
			pagingType: 'full_numbers',
            pageLength: 25,
            serverSide: true,
            processing: true,
			ajax : (dataTablesParameters: any, callback) =>{
				view.http.get<DataTablesResponse>('/api/aps', newHttpOptions).subscribe(resp=>{
					console.log(resp);					
					view.response = resp;
					view.apFound = true;
					view.apsList = resp.list;
					view.apsCount = resp.totalCount;
					for (var i = 0; i < view.apsList.length; ++i) {
						view.apsList[i].selectcheckbox = false;
					}
					$('#overlay').hide();
				});
			},
			columns: [
			{ data: 'apserial' }, { data: 'apname' }, { data: 'lastmacName' }, { data : 'clustername'}, { data : 'zonename'}, { data : 'last_contacted'}
			]
		};
	}

What is the way of fetching data from get method of a web service, and show it in data-table using server side processing? Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
androidparth90commented, Jan 30, 2018

Hi, I have resolved this issue. Please connect me on androidparth@gmail.com

0reactions
lukeangularcommented, Mar 11, 2022

I am stuck with the same issue please reply me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 6 datatable serverside ajax request not working
I have written web api in c# to get the data in desired format. With following dtoptions, server side is working fine. dtOptions...
Read more >
Server Side Rendering Of DataTables JS In ASP.NET Core
In this article, we are implementing server-side data configuration in datatables js in asp.net core.
Read more >
JQuery Datatable in ASP.NET Core - Server-Side Processing
Method. This is how to initialize the datatable. Run the application and check. You can see that the Datatable is now up and...
Read more >
jQuery Datatable Server Side Processing in Asp.Net MVC
Content: 1. populated jquery Datatable with MVC post action method and ajax request. 2. implemented server - side processing like sorting, ...
Read more >
How to Use JQuery DataTables with ASP.NET Core Web API
Select datatables@1.10.21 version and keep Include all library files option selected to download all files related to DataTables plugin.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found