I'm using typescript in angular 7, I want to have two data tables in one page, and I want a rerender method for each one so I can use a search bar, I know data tables already have a search bar, but I need to assign the data arrays in each search because the data's have thousand of rows that make the data tables ultra slow, so with a search I filter the array making it faster.
I already know how to rerender if I have one table, using dtElement, dtInstance.destroy(); and dtTrigger.next(); but I can't make it work using two tables. I found this code in a forum but gives me this error, and I don't know what to do because I'm not using JQuery: DataTables warning: table id=tablaClaveProdServ - Cannot reinitialize DataTable. For more information about this error, please see http://datatables.net/tn/3
This is table 1
<input type="text" class="form-control" placeholder="Busque una clave del catalogo SAT" (change)="buscarClaveProdServ()" [(ngModel)]="busquedaClaveProdServ">
<div class="table-responsive table-wrapper-scroll-y">
<table id="tablaClaveProdServ" datatable [dtOptions]="dtOptions['new']" [dtTrigger]="dtTrigger['new']" class="table table-hover">
<thead>
<tr>
<th scope="col">Clave</th>
<th scope="col">Descripción</th>
</tr>
</thead>
<tbody>
<tr class="pointer" *ngFor="let dato of listaClaveProdServ;">
<td scope="col">{{dato.clave}}</td>
<td scope="col">{{dato.descripcion}}</td>
</tr>
</tbody>
</table>
</div>
This is table 2 (almost same table, changes the data array)
<input type="text" class="form-control" placeholder="Busque una clave del catalogo SAT" (change)="buscarClaveUnidades()" [(ngModel)]="busquedaClaveUnidad">
<div class="table-responsive table-wrapper-scroll-y">
<table id="tablaClaveUnidad" datatable [dtOptions]="dtOptions['new']" [dtTrigger]="dtTrigger['new']" class="table table-hover">
<thead>
<tr>
<th scope="col">Clave</th>
<th scope="col">Descripción</th>
</tr>
</thead>
<tbody>
<tr class="pointer" *ngFor="let dato of listaClaveUnidades;">
<td scope="col">{{dato.clave}}</td>
<td scope="col">{{dato.descripcion}}</td>
</tr>
</tbody>
</table>
</div>
This is my typescript file
export class FrmCatRegistroProductoComponent implements AfterViewInit, OnDestroy, OnInit {
//data arrays
listaClaveProdServ: Array<GenericModel>;
listaClaveUnidad: Array<GenericModel>;
//words to search
busquedaClaveProdServ:string;
busquedaClaveUnidad:string;
@ViewChildren(DataTableDirective)
dtElements: QueryList<DataTableDirective>;
dtOptions: DataTables.Settings[] = [];
dtTrigger: Subject<any>[] = [];
constructor(
private listas: GenericList) {
}
ngOnInit(): void {
this.dtTrigger["new"] = new Subject<any>();
this.dtOptions['new'] = {
pagingType: 'full_numbers',
language: this.listas.dtEspanol,
searching: false
};
this.listaClaveProdServ = [];
this.listaClaveUnidad = [];
}
ngAfterViewInit(): void {
this.dtTrigger['new'].next();
}
ngOnDestroy(): void {
this.dtTrigger['new'].unsubscribe();
}
rerender():void{
this.dtElements.forEach((dtElement: DataTableDirective) => {
dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
dtInstance.destroy();
this.dtTrigger['new'].next();
});
});
}
//the search methods
buscarClaveProdServ():void{
this.listaClaveProdServ = this.listas.ClaveProductoServicio.filter(o => o.descripcion.includes(this.busquedaClaveProdServ));
this.rerender();
}
buscarClaveUnidades():void{
this.listaClaveUnidad = this.listas.ClaveUnidad.filter(o => o.descripcion.includes(this.busquedaClaveUnidad));
this.rerender();
}
}
I also tried sourrounded dtTrigger with setTimeout(()=>{...}); unsuccessfully.
Hope someone can help me, very thanks if you read this far.