Rerendering Datatable in Angular 2 - dtInstance.th

2020-04-12 00:05发布

问题:

I have a component in my angular 2 application which has a dropdown list and a datatable. Based on the name selected from the dropdown list, I want to display the details in the datatable.

HTML -

<div>
  <select [(ngModel)]="selectedName">
    <option *ngFor="let name of nameList" value= {{name.firstName}} >
      {{name.firstName}}
    </option>
  </select>

  <button id="submitName" (click)="getData()">Go</button>
</div>

<table #myTable [dtTrigger]="dtTrigger" datatable class="row-border hover">
  <thead>
    <tr>
      <th>ID</th>
      <th>First name</th>
      <th>Last Name</th>
      <th>Middle Name</th>
    </tr>
  </thead>
  <tbody *ngIf="retrievedNames">


      <tr *ngFor="let name of retrievedNames">
        <td>{{name.id}}</td>
        <td>{{name.firstName}}</td>
        <td>{{name.lastName}}</td>
        <td>{{name.middleName}}</td>
      </tr>


  </tbody>

</table>

My component.ts

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { NetworkService } from './../services/network.service';
import { Subject } from 'rxjs/Rx';
import { Router } from '@angular/router';
import { DataTableDirective } from 'angular-datatables';

@Component({
  selector: 'app-namedetails',
  templateUrl: './namedetails.component.html',
  styleUrls: ['./namedetails.component.css']
})
export class NameDetails implements OnInit {

@ViewChild("myTable") myTable:DataTableDirective;

  private nameList: any;
  private selectedName:string;

  private retrievedNames: any;
  dtTrigger: Subject<any> = new Subject();

  dtElement: DataTableDirective;

  dtOptions: DataTables.Settings = {};


  constructor(private _http:Http, private networkservice : NetworkService,private router: Router) { 
 }

  ngOnInit() {

      this.fetchFirstNames();

  }

  fetchFirstNames(){

    this.networkservice.getAllFirstNames()
          .subscribe(

            res => {
              console.log(res);
              this.nameList = res;
            });

  }

  fetchAllDetails(){

    this.networkservice.getAllNames(this.selectedName)
          .subscribe(

            res => {
              console.log(res);
              this.retrievedNames = res;
              this.myTable.dtInstance.then((dtInstance: DataTables.Api) => {
                  // Destroy the table first
                  dtInstance.destroy();
                  // Call the dtTrigger to rerender again
                  this.dtTrigger.next();
                });
            });

  }


  getData(){

      this.fetchAllDetails();
  }

}

However I keep getting the following error - "Cannot read property 'then' of undefined". How do I resolve this ?

回答1:

You need to add the following method to your component.ts class:

ngAfterViewInit() {
   this.dtTrigger.next();
}

Otherwise the initialisation of the instance is never triggered, so the dtInstance is never created in the first place; which is why it is null.