I am using the Angular-Datatables package and using the render function to show a button, except this button needs a click event to redirect to another page, at the moment it's with an onlick but obviously this does a full redirect which defeats the purpose of the Angular being a SPA.
This is my implementation of the datatable
<table datatable [dtOptions]="dtOptions"></table>
this.dtOptions = {
lengthChange:false,
ajax: (dataTablesParameters: any, callback) => {
this.userService.getUsers().subscribe(resp => {
callback({
data: resp
});
});
},
columns: [{
title: 'Username',
data: 'name'
}, {
title: 'Email',
data: 'email'
}, {
title: 'Phone Number',
data: 'phoneNumber'
}, {
title:'',
data:null,
render: function (data, type, row, meta) {
const temp = JSON.stringify(data);
return `<button class="btn btn-primary" onclick="location.href='/userdetails?user=`+JSON.parse(JSON.stringify(data)).id+`'">Profile</button>`;
}
}],
responsive: true
};
This question uses the rowCallback function which assigns an click event to the whole row, but I cannot do this because of the responsive:true flag, as without this, in a small window, my table goes off screen, so the responsive fits the row to the width of the screen with a click event to expand the remaining fields which conflicts with the rowCallback if I used it (well specifically the rowCallback takes priority).
What can I do to get around this? I have tried <button class="btn btn-primary" routerLink='/testroute'">Profile</button> but that doesn't do anything, it can be seen via inspect element but route does not change, nor is there errors in the console
ngAfterViewInit(): void { this.renderer.listen('document', 'click', (event) => { console.log(event) }); }just to see it working