afeef
afeef

Reputation: 4706

Type 'Person[]' is not assignable to type 'ColumnSettings[] in Angular 7 Datatable

Problem

Type 'Person[]' is not assignable to type 'ColumnSettings[]'.

Type 'Person' has no properties in common with type 'ColumnSettings'.ts(2322)

The expected type comes from property 'columns' which is declared here on type 'Settings'

i have followed http://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way

problem occured after appending json datatatble.

Person class

export class Person {
id: number;
first_name: string;
last_name: string;
}

Angular Code

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 data

     TABLE_FIELD: [
        {
        data: "id"
        },
        {
        data: "first_name"
        },
        {
        data: "last_name"
        }

        ]

Upvotes: 0

Views: 1511

Answers (2)

afeef
afeef

Reputation: 4706

Solution that worked

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,


};

Upvotes: 0

Ravikumar Rajendran
Ravikumar Rajendran

Reputation: 504

this.dtOptions.columns is a type of ColumnSettings[] Refer https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e01d6e7abb2343c6b845d4945a368ed55cbf5bd2/types/datatables.net/index.d.ts#L1309

but you are passing a Person[] so typescript compiler is throwing an error.

You have to modifiy the data before assinging to this.dtOptions.columns.

Upvotes: 1

Related Questions