jquery - 无法使用 Angular Datatable 重新初始化 DataTable
我有一个带有简单 CRUD 功能的 Angular 应用程序。我已经使用静态 HTML 表格测试过我的数据,这个是可行的。现在我正在使用一个名为 Angular 数据表的数据表框架。
链接: https://l-lin.github.io/angular-datatables/#/welcome
我可以创建、读取和删除一条记录,但在此操作之后我会收到如下错误:
DataTables 警告:表格 id=DataTables_Table_0 - 无法重新初始化 DataTable。有关此错误的更多信息,请参见 http://datatables.net/tn/3
我尝试过几个解决方案,比如它提供的链接以及其他类似这样的 Stack Overflow 帖子:
在 Angular 5 数据表中出现错误:无法重新初始化 DataTable
https://l-lin.github.io/angular-datatables/#/basic/angular-way
https://l-lin.github.io/angular-datatables/#/advanced/rerender
这是我在 car-component.js 中的代码,我使用 car-services.js 处理所有的 HTTP 调用。
import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { CarService } from '../services/carservices/car.service';
import { CarVM } from '../viewmodels/car/car-vm';
import { Subject } from 'rxjs';
import { DataTableDirective } from 'angular-datatables';
@Component({
selector: 'app-car',
templateUrl: './car.component.html',
styleUrls: ['./car.component.css']
})
export class CarComponent implements OnInit {
FormCar: any;
countrecords: any;
Carid: number = 0;
dtTrigger: Subject<any> = new Subject();
dtElement: DataTableDirective;
allCars: CarVM[];
dtOptions: DataTables.Settings = {};
constructor(private formbuilder: FormBuilder, private carservice: CarService) { }
ngOnInit() {
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 5,
};
this.GetCar();
this.GetCarCount();
this.FormCar = this.formbuilder.group({
CarId: ['', Validators.required],
Brand: ['', Validators.required],
Model: ['', Validators.required],
Color: ['', Validators.required],
TopSpeed: ['', Validators.required],
});
}
AddCar(CarVM: CarVM) {
CarVM.CarId = this.Carid;
this.carservice.CreateCar(CarVM).subscribe(() => {
this.GetCar();
this.GetCarCount();
this.Reset();
this.Carid = 0;
})
}
GetCar() {
this.carservice.getAllCars().subscribe(res => {
this.allCars = res;
this.dtTrigger.next();
})
}
GetCarCount() {
this.countrecords = this.carservice.getCount();
}
EditCar(car: CarVM) {
this.carservice.updateCar(car).subscribe(Response => {
this.Carid = Response.CarId;
this.FormCar.controls['Brand'].setValue(Response.Brand);
this.FormCar.controls['Model'].setValue(Response.Model);
this.FormCar.controls['Color'].setValue(Response.Color);
this.FormCar.controls['TopSpeed'].setValue(Response.TopSpeed);
})
}
DeleteCar(CarId: string) {
if (confirm("Are you sure?")) {
this.carservice.deleteCar(CarId).subscribe(() => {
this.GetCar();
this.GetCarCount();
})
}
}
Reset() {
this.FormCar.reset();
}
}
这是我关于汽车的 HTML 页面:
<table datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger" class="row-border hover">
<thead>
<tr>
<th>Id</th>
<th>Brand</th>
<th>Model</th>
<th>Color</th>
<th>Speed</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let car of allCars">
<td>{{car.carId}}</td>
<td>{{car.brand}}</td>
<td>{{car.model}}</td>
<td>{{car.color}}</td>
<td>{{car.topSpeed }}</td>
<td>
<button type="button" class="btn btn-danger mr-1" (click)="DeleteCar(car.carId)">Delete</button>
</td>
</tr>
</tbody>
</table>
注意:
我使用的是Angular版本,而不是JQuery版本!
有人可以给我指明正确的方向吗?
答案#1
在再次触发之前,您需要销毁datatable实例。
你可以这样创建一个函数:
import { DataTableDirective } from 'angular-datatables';dtElement: DataTableDirective;isDtInitialized:boolean = false GetCar() {
this.carservice.getAllCars().subscribe(res => {
this.allCars = res;
if (this.isDtInitialized) {
this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
dtInstance.destroy();
this.dtTrigger.next();
});
} else {
this.isDtInitialized = true
this.dtTrigger.next();
}
})
}
使用这个,第一次它将进入 else 块并直接触发数据表。之后,当你刷新时,它会先销毁数据表,然后再触发。
答案#2
上面的回答 @Adrita Sharma 对我不起作用,它抛出了这个错误。
core.js:9110 错误 TypeError:无法读取未定义的属性 'dtInstance'
所以我加上了
@ViewChild(DataTableDirective, {static: false})
而且它起作用了
import { Component, ViewChild, OnInit } from '@angular/core';
import { DataTableDirective } from 'angular-datatables';
export class AttendancePage implements OnInit {
@ViewChild(DataTableDirective, {static: false})
dtElement: DataTableDirective;
isDtInitialized:boolean = false;
以及在我初始化我的数据表的函数内部
if (this.isDtInitialized) {
this.dtElement.dtInstance.then((dtInstance: DataTables.Api) => {
dtInstance.destroy();
this.dtTrigger.next();
});
} else {
this.isDtInitialized = true;
this.dtTrigger.next();
}
