I have a Datatable from datatables.net on which each rows can be selected with a click event on a checkbox. When the event is triggered, I add or remove a CSS class to change the look.
$(".cbDatatable").on('click', (function (evt) {
$(this).closest('tr').removeClass('selected');
evt.stopImmediatePropagation();
}
else {
$(this).closest('tr').addClass('selected');
evt.stopImmediatePropagation();
}
}));
The CSS class simply change the background color and add a little blue border en the left side of the row.
.selected{background-color: #fafafa; border-left: 4px solid #0e8ae8;}
This is working just fine.
The problem
But if I select the n+1 row and then the n+3 row, the n+2 row also render the border-left from the 'selected' CSS class whereas the class is not applied on it.
<body>
<tr class="odd selected" role="row">
<tr class="even" role="row"> <-- This one
<tr class="odd selected" role="row">
<tr class="even" role="row">
<tr class="odd" role="row">
<tr class="even selected" role="row">
</tbody>
It looks like that on every browsers. If I scroll, the border often bug like that.
Is it a bug ? Am I doing something wrong ?
What I tried to solve it
- I looked in the DOM explorer and no CSS property is applied on the non 'selected' row to render the border-left
- I tried to put the js function in the $datatable.on('draw.dt', function () {}); as well as in the $(document).ready(function () {});
- In the DOM explorer, inactivate and reactivate an other CSS property of the non 'selected' row solve the rendering issue
- I forced the reloading of all the CSS sources in the click function and it solved the rendering problem
Any advice would be very appreciated