Тип «Person []» не может быть назначен типу «ColumnSettings [] в Angular 7 Datatable

Проблема

Тип «Person []» не может быть назначен типу «ColumnSettings []».

Тип "Person" не имеет общих свойств с типом "ColumnSettings".ts(2322)

Ожидаемый тип исходит из свойства «столбцы», которое объявлено здесь для типа «Настройки».

я следил за http://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way

проблема возникла после добавления json datatable.

Класс человека

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 в столбцы в datatable.

  • любая помощь приветствуется.


person afeef    schedule 22.05.2019    source источник
comment
можете ли вы воспроизвести эту проблему на stackblitz.com   -  person Ravikumar    schedule 22.05.2019
comment
проблема решена   -  person afeef    schedule 22.05.2019
comment
@afeef, если проблема решена, нажмите галочку рядом с ответом, который решил вашу проблему, вместо добавления комментария. Спасибо   -  person Liam    schedule 22.05.2019
comment
я сам решил проблему и решение добавлено   -  person afeef    schedule 22.05.2019
comment
Также не добавляйте ответы в вопросы. Если вы хотите ответить на него самостоятельно, добавьте ответ   -  person Liam    schedule 22.05.2019


Ответы (2)


this.dtOptions.columns является type из ColumnSettings[] См. https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e01d6e7abb2343c6b845d4945a368ed55cbf5bd2/types/datatables.net/index.d.ts#L1309

но вы передаете Person[], поэтому компилятор машинописного текста выдает ошибку.

Вы должны изменить данные перед назначением this.dtOptions.columns.

person Ravikumar    schedule 22.05.2019

Решение, которое сработало

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,


};
person afeef    schedule 23.05.2019