0

问题

类型“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 附加到数据表中的列中。

  • 任何帮助都是最受欢迎的。

4

2 回答 2

1

this.dtOptions.columns是 参考https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e01d6e7abb2343c6b845d4945a368ed55cbf5bd2/types/datatables.net/index.d.ts#L1309type _ColumnSettings[]

但是您传递的Person[]打字稿编译器会引发错误。

您必须先修改数据,然后再分配给this.dtOptions.columns.

于 2019-05-22T09:57:56.843 回答
0

有效的解决方案

this.abc();

this.p_col = 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;

        callback({
          recordsTotal: resp.recordsTotal,
          recordsFiltered: resp.recordsFiltered,
          data: [],

        });
      });
  },

  columns: this.p_col,


};
于 2019-05-23T05:28:07.037 回答