3

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

1
  • can you please provide js fiddle link of your code if it is possible ? Commented Oct 24, 2016 at 5:36

1 Answer 1

1

Adding border style for TR tag is not best idea. How about adding border style to first cell (in my example it's TD but it might be TH) in row with class ?

.selected td:first-child{
  border-left: 4px solid #0e8ae8;
}

Look at example - it only adds border where it should be: https://jsfiddle.net/6x2ubk00/

Should work same regardless if it's build manually or with Datatable

Sign up to request clarification or add additional context in comments.

2 Comments

Ups. Sorry. I've edited my answer and added working jsfiddle url
It seams to solve the problem indeed, thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.