angular-datatable (оболочка для библиотеки datatables.net) не поддерживает динамические источники данных, поэтому при создании таблицы данные не могут быть изменены.
Итак, чтобы решить проблему, мне приходилось пересоздавать таблицу каждый раз, когда данные менялись.
Вот код, файл request-results.component.ts:
import { Component, OnInit, OnDestroy, AfterViewInit, ViewChild } from '@angular/core';
import { RequestService } from '../services/request.service';
import { Subject } from 'rxjs/internal/Subject';
import { DataTableDirective } from 'angular-datatables';
declare var $: any;
declare interface DataTable {
headerRow: string[];
dataRows: string[][];
}
@Component({
selector: 'app-request-results',
templateUrl: './request-results.component.html'
})
export class RequestResultsComponent implements OnDestroy, AfterViewInit, OnInit {
@ViewChild(DataTableDirective)
dtElement: DataTableDirective;
public hideTheResults = false;
private notFirstTime = false; // if the data table is going to be used again with new data, it has to be recreated
requestResponse;
resultsAreFound = true;
dtOptions: DataTables.Settings = {
columnDefs: [ { orderable: false, targets: [4, 5] } ],
retrieve: true,
autoWidth: true,
pagingType: 'full_numbers',
lengthMenu : [
[10, 25, 50, -1],
[10, 25, 50, 'All']
],
responsive: true,
language: {
search: '_INPUT_',
searchPlaceholder: 'Search records',
}};
dtTrigger = new Subject();
public dataTable: DataTable;
constructor(private requestService: RequestService) { }
ngOnInit() {
this.dataTable = {headerRow : null, footerRow: null, dataRows: null };
this.dataTable.headerRow = ['Col1', 'Col2', 'Col3', 'Col4', 'Col5'];
this.requestService.currentSearchResult.subscribe(result => {
this.requestResponse = result;
if (result.length > 0) {
const table = $('#datatable').DataTable();
table.destroy();
this.resultsAreFound = true;
if (this.notFirstTime) {
this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
dtInstance.destroy();
});
}
this.notFirstTime = true;
this.dataTable.dataRows = this.requestResponse.map(
function(i: { [s: string]: {}; } | ArrayLike<{}>) { return Object.values(i);
});
this.dtTrigger.next();
} else {
this.notFirstTime = true;
this.resultsAreFound = false;
}
});
}
ngOnDestroy(): void {
this.dtTrigger.unsubscribe();
}
ngAfterViewInit() {
this.dtTrigger.next();
}
и файл request-results.component.html:
<div class="main-content" style="margin-top: 50px; padding-left: 0px; padding-right: 0px">
<div class="row">
<div class="col-md-12">
<div class="card">
<div [hidden]="resultsAreFound" class="card-header">
<h4 class="card-title">0 Resultats trouvées.</h4>
</div>
<div [hidden]="!resultsAreFound || hideTheResults" class="card-body">
<h4 class="card-title">Profils trouvées:</h4>
<div class="toolbar">
<!-- Here you can write extra buttons/actions for the toolbar -->
</div>
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="table table-striped table-bordered">
<thead>
<tr>
<th>{{ dataTable.headerRow[0] }}<span></span></th>
<th>{{ dataTable.headerRow[1] }}<span></span></th>
<th>{{ dataTable.headerRow[2] }}<span></span></th>
<th>{{ dataTable.headerRow[3] }}<span></span></th>
<th>{{ dataTable.headerRow[4] }}<span></span></th>
<th><span></span></th>
</tr>
</thead>
<tfoot>
<tr>
<th>{{ dataTable.footerRow[0] }}</th>
<th>{{ dataTable.footerRow[1] }}</th>
<th>{{ dataTable.footerRow[2] }}</th>
<th>{{ dataTable.footerRow[3] }}</th>
<th>{{ dataTable.footerRow[4] }}</th>
<th></th>
</tr>
</tfoot>
<tbody>
<tr *ngFor="let row of dataTable.dataRows">
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
<td>{{row[3]}}</td>
<td>
<a class="btn btn-info btn-link btn-icon btn-sm like" href="{{row[4]}}" title="LinkedIn"><i class="fa fa-linkedin"></i></a>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end content-->
</div>
<!-- end card -->
</div>
<!-- end col-md-12 -->
</div>
<!-- end row -->