我在用
Angular Datatables
用户.ts
ngOnInit() {
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 7,
deferRender: true,
retrieve: true
};
if (localStorage.getItem('data') !== null) {
const data = JSON.parse(localStorage.getItem('data'));
this.item = data.body.response;
if (Array.isArray(this.item.domains) || this.item.domains.length) {
for (let i = 0; i < this.item.domains.length; i++) {
this.users.getUsers(this.item.domains[i].id).subscribe((res: any) => {
this.domain_users = res.response.items;
// the api returns arrays and so I had to iterate over them, and not all of them
// the first array is empty
for (let j = 0; j < this.domain_users.length; j++) {
if (this.domain_users[j].user !== undefined) {
this.user.push(this.domain_users[j].user);
}
}
}, error => {
console.log('getUsers error, probably session expired on the server ' + error);
});
}
}
}
}
ngAfterViewInit(): void {
this.dtTrigger.next();
}
ngOnDestroy(): void {
// Do not forget to unsubscribe the event
this.dtTrigger.unsubscribe();
}
rerender(): void {
this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
// Destroy the table first
dtInstance.destroy();
// Call the dtTrigger to rerender again
this.dtTrigger.next();
});
}
html格式
<table id="detailed-resource-optria" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="compact row-border hover cell-border"
data-page-length='10'>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of user">
<td>{{item.profile?.fname}}</td>
<td>{{item.profile?.lname}}</td>
</tr>
</tbody>
</table>
this.user
没有经过
dtOptions
,但我不知道如何修复它,我尝试从数组中提供数据,作为源,这是行不通的。