angular - 类型'Person []'不可分配给Angular 7 Datatable中的类型'ColumnSettings []
问题描述
问题
类型“Person[]”不可分配给类型“ColumnSettings[]”。
“Person”类型与“ColumnSettings”类型没有共同的属性。ts(2322)
预期的类型来自属性 'columns',它在类型 'Settings' 上声明
我已经关注了http://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way
附加 json 数据表后出现问题。
人物类
export class Person {
id: number;
first_name: string;
last_name: string;
}
角码
publicDeals: Person[] = [];
ngOnInit(): void {
const that = this;
this.abc();
console.log(this.publicDeals);
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 2,
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
that.httpClient
.post<DataTablesResponse>(
this.api_url,
dataTablesParameters, {}
).subscribe(resp => {
that.persons = resp.data;
//console.log(resp);
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: [],
});
});
},
columns: that.publicDeals,
};
}
abc() {
return this.service.apifunc()
.subscribe(persons => {
this.testts = TABLE_FIELDS;
TABLE_FIELDS.forEach(element => {
this.publicDeals.push(element);
});
this.dtTrigger.next();
});
}
json数据
TABLE_FIELD: [
{
data: "id"
},
{
data: "first_name"
},
{
data: "last_name"
}
]
无法将 json 附加到数据表中的列中。
任何帮助都是最受欢迎的。
解决方案
this.dtOptions.columns
是
参考https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e01d6e7abb2343c6b845d4945a368ed55cbf5bd2/types/datatables.net/index.d.ts#L1309type
_ColumnSettings[]
但是您传递的Person[]
打字稿编译器会引发错误。
您必须先修改数据,然后再分配给this.dtOptions.columns
.
推荐阅读
- visual-studio - 如何在 VS 2019 中构建一个深色 UI 应用程序?
- javascript - Node.js 如何在浏览器中使用警报消息
- influxdb - 如何在kapacitor中发送任务名称而不是警报ID?
- c - 从 PostgreSQL C 函数写入文件
- reactjs - 当数组未更改时,在每次渲染中调用 useEffect 数组依赖项
- microsoft-graph-api - 我可以使用 MS Graph v1.0 发送具有“个人”敏感性的邮件吗
- ios - 单击按钮隐藏键盘并且在键盘启动时不会触发操作
- ruby-on-rails - 主动存储通过加密上传到 S3。如何删除加密并将文件按原样保存在存储中
- spring-boot - spring websocket 向特定用户发送消息
- javascript - 即使输入文本有效(Java 脚本或 Jquery),如何强制输入文本无效