Rajasekhar
Rajasekhar

Reputation: 2455

How to add static data columns in dataTable existing columns

Have requirement like API gives few columns of data and I need to add some static data columns. Is there any way to add few columns dynamically passing below like { data: this.test1 },{ data: this.test2 }. I tried to add two columns but unable to fetch the data, getting blank cells.

  constructor(private http: HttpClient) {}
   public test1 =123;
   public test2 =234;

Initialization:

this.dtOptions = {
  pagingType: "full_numbers",
  pageLength: 10,
  scrollCollapse: true,
  processing: true,
  destroy: true,
  scrollY:'50vh',
  columns: [
    { title: '<input type="checkbox" />' },
    { data: "index" },
    { data: "firstname" },
    { data: "lastname" },
    { data: this.test1 },
    { data: this.test2 }
  ],

Stackblitz

Upvotes: 2

Views: 889

Answers (1)

andrewJames
andrewJames

Reputation: 22042

You can set data to null and then use the defaultContent option.

Hard-coded in the column definition:

{ title: "Hard-Coded Data", data: null, defaultContent: "foo" }

Similar, but using a JavaScript variable - for example, var bar = 'baz';:

{ title: "Hard-Coded Data", data: null, defaultContent: bar }

Upvotes: 2

Related Questions