Search code examples
angulartablesaw

Tablesaw in Angular2+ not working when I get my data from service


Im using Tablesaw in my Angular 5 project but its not working when Im getting my table data from service but its working with static data.

export class MmrParticipationListComponent implements OnInit {
pid; mid; participationList; approversList = [];

constructor(private activatedRoute: ActivatedRoute, private programService: 
ProgramService) { }

ngOnInit() {
this.activatedRoute.queryParams.subscribe(params => {
  this.pid = +params['pid'];
  this.mid = +params['mid'];

  this.programService.getParticipationList(this.pid, this.mid, 0).subscribe(a => {
    this.participationList = a;

    this.programService
      .getParticipationListPerEachGroup(this.pid, this.mid, this.participationList.FillRepeater[0].MMRPTID)
      .subscribe(b => {     this.approversList = b;}
      );
    });
   });
  }
}

<table *ngIf="approversList" class="tablesaw tablesaw-stack table table-
hover table-custom" data-tablesaw-mode="stack">
<thead>
<tr>      <th>Role</th>   <th>Name</th>    </tr>
</thead>
<tbody>
<tr *ngFor="let item of approversList">    
<td>{{item.Role}}</td>  <td>{{item.Name}}</td>  
</tr>
 </tbody>
</table>

Solution

  • Most likely, Tablesaw is initialising before you are receiving the data from your service.

    Add an *ngIf="results" on the component selector. Angular won't run that component until you have the data from your service.