Refresh columns dynamically #1575
-
Hi, i'm using angular datatable with the angular way <table datatable id="datatable" class="row-border hover"
[ngClass]="{'d-none' : loading, datatable: true}"
[dtOptions]="dtOptions"
[dtTrigger]="dtTrigger"> i load data from database with service call and my columns change based on the data. So i update dtOptions dynamically with this: // first adding empty column for the select all
const columns: any = [{
title: '<input type="checkbox" class="css" />',
orderable: false,
className: 'select-checkbox'
}];
for (const key in this.fields) {
columns.push({
title: this.fields[key].name,
orderable: true
});
}
// We adding empty column for the action column
columns.push({
title: '',
orderable: false
});
this.dtOptions.columns = columns; and then, i rerender my datatable with this: rerender(): void {
this.dtElement.dtInstance.then((dtInstance: any) => {
// Destroy the table first
dtInstance.clear().destroy();
// Call the dtTrigger to rerender again
this.dtTrigger.next();
});
} the column title change but the number of column stay at the same number before i change it. https://stackblitz.com/edit/update-columns-dynamic Can you help me? Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi, Your code looks fine. The columns displayed on table are part of Try two-way binding It would help if you could provide a StackBlitz link or GitHub repo. |
Beta Was this translation helpful? Give feedback.
-
Hi, I wrote a stackblitz here for my problem: https://stackblitz.com/edit/update-columns-dynamic |
Beta Was this translation helpful? Give feedback.
Hi,
I wrote a stackblitz here for my problem: https://stackblitz.com/edit/update-columns-dynamic
you can that the columns dont change on button click