5

I have just started working on Angular 4 and I am trying to render some data which I receive from angular service in json format, into angular-datatable, but whichever option i try its not working for me. The table is coming, the columns are coming, however the data inside the columns are not displaying.

Any help would be great,

Thanks in advance..!!!!

Please find my code below:

component.html

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

component.ts

import { Component, OnInit } from '@angular/core';
import { FleetDataService } from '../../services/fleet-data.service';
import { Subject } from 'rxjs/Subject';

@Component({
   selector: 'app-dashboard',
   templateUrl: './dashboard.component.html',
   styleUrls: ['./dashboard.component.scss']
})

export class DashboardComponent implements OnInit {

  private fleetData: any;
  dtOptions: DataTables.Settings = {};
  dtTrigger: Subject<any> = new Subject();
  constructor(private getFleetData:FleetDataService) { }

  ngOnInit() {
     this.getFleetData.getFleetData().subscribe(
     fleetData => {
       this.fleetData = fleetData;
       console.log(this.fleetData);
       this.dtTrigger.next();
    },
    err => {
     console.log(err);
    }
 );
 this.dtOptions = {
   pagingType: 'full_numbers',
   columns: [{
     title: 'First Name',
     data: this.fleetData
   }, {
     title: 'Last Name',
     data: this.fleetData
   }, {
     title: 'Score',
     data: this.fleetData
   }]
  };
 }
}

component.service

import { Injectable } from '@angular/core';
import { HttpModule, Http, Response, Headers, RequestOptions } from 
'@angular/http';
import { Observable } from 'rxjs/Rx';


@Injectable()
export class FleetDataService {

 constructor(private http: Http) { }

 getFleetData() {
   return this.http.get("../../assets/data/test.json")
     .map((res:Response) => res.json())
     .catch((error:any) => Observable.throw(error.json().error || 'Server 
   Error'));
 }

}

test.json

  [{
    "FirstName": "Jill",
    "LastName": "Smith",
    "Score": "disqualified"
  }, {
    "FirstName": "Eve",
    "LastName": "Jackson",
    "Score": "94"
  }, {
    "FirstName": "John",
    "LastName": "Doe",
    "Score": "80"
  }, {
    "FirstName": "Adam",
    "LastName": "Johnson",
    "Score": "67"
 }]
pranay anand
  • 713
  • 4
  • 13
  • 26

1 Answers1

1

You set your dtOptions outside the subscribe.

If you do this the fleetData stays empty so dtOptions is never set correctly, because an Observable is asynchronous. I propose this code:

export class DashboardComponent implements OnInit {

  dtOptions: DataTables.Settings = {};
  dtTrigger: Subject<any> = new Subject();

  constructor(private getFleetData:FleetDataService) { }

  ngOnInit() {
     this.getFleetData.getFleetData().subscribe(
     fleetData => {
       console.log(fleetData);
       this.buildDtOptions(fleetData)
       this.dtTrigger.next();
    },
    err => {
     console.log(err);
    });
  }

  private buildDtOptions(fleetData: any): void {
    this.dtOptions = {
         pagingType: 'full_numbers',
         columns: [
           {title: 'First Name', data: fleetData}, 
           {title: 'Last Name', data: fleetData}, 
           {title: 'Score', data: fleetData}
         ]
    };
  }
 }

For this error: ERROR TypeError: Cannot read property 'aDataSort' of undefined. You can do a spinner (ngIf / else) in the view and when data are loaded you display the datatable

mickaelw
  • 1,453
  • 2
  • 11
  • 28
  • You mean like a spinner or something,till the data is being displayed? – pranay anand Nov 28 '17 at 09:40
  • I think you are right with the approach, but miss as OP the basics in DataTables. You should have `data: fleetData` in `dtOptions` and in the `columns` section refer to the attributes, i.e `data: 'Firstname'` and so on. – davidkonrad Nov 28 '17 at 09:40
  • @pranayanand yes – mickaelw Nov 28 '17 at 09:41
  • @davidkonrad yes I agree with you. The idea is to have a spinner during the loading (ngIf ; else). So the datatable isn't build yet and it will build when all data are fetching – mickaelw Nov 28 '17 at 09:44
  • I added following code in my ngOnInit() function: ngOnInit() { this.loaderService.display(true); this.getFleetData.getFleetData().subscribe( fleetData => { console.log(fleetData); this.buildDtOptions(fleetData); this.dtTrigger.next(); }, err => { console.log(err); }); this.loaderService.display(false); } But still i am getting ERROR TypeError: Cannot read property 'aDataSort' of undefined – pranay anand Nov 28 '17 at 10:21
  • @pranayanand this.loaderService.display(false); to be inside the subscribe too – mickaelw Nov 28 '17 at 10:21
  • ngOnInit() { this.loaderService.display(true); this.getFleetData.getFleetData().subscribe( fleetData => { console.log(fleetData); this.buildDtOptions(fleetData); this.dtTrigger.next(); this.loaderService.display(false); }, err => { console.log(err); this.loaderService.display(false); }); } gives this errors 1.ERROR ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'. 2.TypeError:Cannot read property 'aDataSort' of undefined – pranay anand Nov 28 '17 at 10:24
  • @pranayanand your first error is about the loaderService? – mickaelw Nov 28 '17 at 10:28
  • @pranayanand what's your datatables npm package for I check it in my side? – mickaelw Nov 28 '17 at 10:30
  • I guess so, If i call the loader outside subscribe i don't get that error, Apologies if i am not getting it correctly – pranay anand Nov 28 '17 at 10:31
  • "*TypeError: Cannot read property 'aDataSort' of undefined*" ...because `data` not is attached with an attribute. – davidkonrad Nov 28 '17 at 10:33
  • i got it from here: https://l-lin.github.io/angular-datatables/#/basic/with-ajax. No it doesnt work, but i dont get the "ExpressionChangedAfterItHasBeenCheckedError" error thats all – pranay anand Nov 28 '17 at 10:33
  • @davidkonrad : any tip on how to attach it with an attribute? – pranay anand Nov 28 '17 at 10:34
  • @pranayanand, look at my first comment above. – davidkonrad Nov 28 '17 at 10:34
  • @pranayanand sorry I cann't test to know why you have your error. I don't have my computer and on stackblitz I cann't have acces to node_modules to configure the angular-cli – mickaelw Nov 28 '17 at 11:11
  • @mickaelw : Its ok, I really appreciate all the help, whenever you get time, please look into it, if possible. – pranay anand Nov 29 '17 at 05:32