I have web in Angular that have a multilanguage option. This web has a datatable. The code is similar to
Switch language function
switchLanguage(language: string) {
let currentUrl = this.router.url;
this.translate.use(language);
this.router
.navigateByUrl("", { skipLocationChange: true })
.then(() => this.router.navigate([currentUrl]));
}
Datatable view
ngOnInit() {
this.datatable()
}
datatable() {
const self = this;
this.table = $('#userList').DataTable({
order: [5, "desc"],
pagingType: "full_numbers",
pageLength: 25,
responsive: true,
processing: true,
searching: true,
deferRender: true,
stateSave: true,
columnDefs: [
{
targets: [0, 11],
orderable: false
},
{ targets: 0, "width": "5%" },
{ targets: 11, "width": "10%" },
{ targets: 7, "width": "15%" },
{ targets: 6, "width": "10%" },
{ targets: 5, "width": "10%" },
{ targets: "_all", defaultContent: '-' },
],
ajax: { ENDPOINT },
columns: [
{
title: this.translate.instant('user'),
data: 'username',
},
]
}
The problem: When we call
this.router.navigateByUrl("", { skipLocationChange: true }).then(() => this.router.navigate([currentUrl]));
Code try to recreate datatable but returns this error. This error makes sense, but how can avoid this? Also, i try to add destroy:true to datatable options to try destroy and recreate datatable when change language, but destroy also on first load so doesn´t work as expected.
So, simplified question: I try to change datatable columns (JQuery library) name when user change language dynamically. ¿What´s the right way?