Show a "switch" button in the table that represent a bool result
Show a "switch" button in the table that represent a bool result
Hello everyone.
I need to show a boolean value in the table (wether a product is discounted or not).
In html with bootstrap 5 I used to render it as a switch button with that code:
<input class="form-check-input form-check form-switch d-flex justify-content-center" disabled type="checkbox" id="flexSwitchCheckDefault" value="true">
So the question is, can I achieve the same kind of result with Datatables?
Thanks for your help. I look on the forum but could not find what I was looking for.
This question has accepted answers - jump to:
Answers
Yep, that would be possible. You would use
columns.render
to display that boolean any way you choose. This example should help, especially the progress column, as that's doing something very similar,Colin
Thank you Colin. I'm successfully showing the switch button. But I cannot show the proper state. I know I must cast my bool result in the value field but what I did dosen't sems to work .
{
data: 'isDiscounted',
"render": function (data, type, row, meta) {
return type === 'display'
? '<input class="form-check-input form-check form-switch d-flex justify-content-center" role="switch" disabled type="checkbox" id="flexSwitchCheckDefault" value="' + data + '"/>'
: data;
},
"width": "5%"
},
Ok nevermind mycode was wrong.
That looks much better but still it doesn't show the proper state.
If you want to show state with an input element, you'll need to set its state. Since the
render
function returns a string, what you need to do is userowCallback
. This example shows exactly that being done.Allan
Hey Allan, thanks for your help. I try to use "rowCallback" but I can't make it work.
Here's my code.
Good I make it work! Thanks for your help! I just had to delete the if condition