Проверьте это https://stackblitz.com/edit/how-to-create-drill-down-tables-using-this-library-1240-cqmh8n?file=app%2Fapp.component.html
TS:
import { Component, VERSION, ViewChild } from '@angular/core';
import { DataTableDirective } from 'angular-datatables';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
version = 'Angular: v' + VERSION.full;
dtOptions: DataTables.Settings = {};
mainData = [];
table;
@ViewChild(DataTableDirective)
private datatableElement: DataTableDirective;
constructor() { }
ngOnInit(): void {
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 5,
processing: true
};
this.mainData = [
{ 'plus': '', 'ID': 1, 'FName': 'AAA', 'SName': 'A', 'Place': 'ID' },
{ 'plus': '', 'ID': 2, 'FName': 'BBB', 'SName': 'B', 'Place': 'SG' },
{ 'plus': '', 'ID': 3, 'FName': 'CCC', 'SName': 'C', 'Place': 'HK' },
{ 'plus': '', 'ID': 4, 'FName': 'DDD', 'SName': 'D', 'Place': 'CN' },
{ 'plus': '', 'ID': 2, 'FName': 'BBB', 'SName': 'B', 'Place': 'SG' },
{ 'plus': '', 'ID': 3, 'FName': 'CCC', 'SName': 'C', 'Place': 'HK' },
{ 'plus': '', 'ID': 4, 'FName': 'DDD', 'SName': 'D', 'Place': 'CN' }
];
}
ngAfterViewInit() {
this.datatableElement.dtInstance.then(table => {
console.log(table);
this.table = table
});
}
addChildTable(rowInstance) {
const row = this.table.row(rowInstance);
const data = this.table.row(rowInstance).data()
console.log(data);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
}
else {
const childTable=this.getTable();
row.child(childTable).show();
}
}
getTable() {
return `<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>SSS</td>
</tr>
</tbody>
</table>`
}
}
HTML:
<div class="row" style="margin:0px;">
<div class="col-md-12">
<h6>Nested Table</h6>
</div>
<div class="col-md-12">
<table datatable class="table table-striped">
<thead>
<tr>
<td></td>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Place</td>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let row of mainData;let i = index">
<tr #rowInstance>
<td (click)=addChildTable(rowInstance)><i class="fa fa-plus"></i></td>
<td>{{row.ID}}</td>
<td>{{row.FName}}</td>
<td>{{row.SName}}</td>
<td>{{row.Place}}</td>
</tr>
</ng-container>
</tbody>
</table>
</div>
</div>