26

I am using Twitter Bootstrap table with clickable rows that are highlighted when hovered over (I can get rid of the hover feature if it makes this easier). I want the selected row to remain highlighted until another row is clicked or it is reclicked.

    $( document ).ready(function() {
        $('#myTable').on('click', 'tbody tr', function(event) {
            //  console.log("test ");                   
        });

and the table

<table class="table table-bordered table-hover" id="myTable">
    <tbody>
        <tr>
        <tr class='clickable-row'>

I tried this code in the JS but didn't work

$(this).addClass('highlight').siblings().removeClass('highlight');

4 Answers 4

61

You are quite close. Targeting the .clickable-row class on your $("#myTable").on(...) event and using Bootstrap's .active class should work for you:

HTML:

<table class="table table-bordered" id="myTable">
  <tr class="clickable-row">
    <th>Example</th>
  </tr>
   <tr class="clickable-row">
    <th>Example 2</th>
  </tr>
</table>

Javascript:

$('#myTable').on('click', '.clickable-row', function(event) {
  $(this).addClass('active').siblings().removeClass('active');
});

And a Bootply Example

Note: If you left .table-hover on your table, you'd have to use a different class than .active, such as .bg-info (which would be a blue hightlight)

To remove a highlight from the row (ie click again), check if the row has the class and remove it:

$('#myTable').on('click', '.clickable-row', function(event) {
  if($(this).hasClass('active')){
    $(this).removeClass('active'); 
  } else {
    $(this).addClass('active').siblings().removeClass('active');
  }
});

See @BravoZulu's answer for original information.

2
  • re-clicking on the row does not deselect it.
    – Jabda
    Commented Jul 30, 2015 at 18:29
  • 1
    There is a bug with this solution - you can select multiple rows (one per page, to be precise). Using table.$('tr.active').removeClass('active'); seems to fix the problem.
    – wondra
    Commented Apr 4, 2019 at 13:08
8

Try:

$(".clickable-row").click(function(){
    if($(this).hasClass("highlight"))
        $(this).removeClass('highlight');
    else
        $(this).addClass('highlight').siblings().removeClass('highlight');
})
1
  • 3
    This is where toggleClass is handy. Commented Oct 16, 2016 at 2:32
2

To show the painted row of a color, indicating that it is selected, you can use data-row-style = 'formatterRowUtSelect'

Inside your code, you can add this method:

formatterRowUtSelect = function (row, index) {
    if (row.fieldOfMyObject == $ ('#fieldOfMyObject'). val ())
        return {classes: 'row-selected'};
    return {};
}

Do not forget to create the css for the selected row:

.row-selected {
    background-color: #a9f0ff !important;
    font-weight: bold;
}

I hope it serves you, Greetings.

2
  • Welcome to Stack Overflow! If you haven't already, please feel free to check out how to ask and what's on-topic here.
    – Claudia
    Commented Feb 6, 2018 at 15:09
  • Also, I'll note that it's generally more idiomatic that you use function declarations (i.e. function foo() { ... }) rather than implicit global assignment (like foo = function () { ... }). An added benefit to doing it this way is that it also works in strict mode.
    – Claudia
    Commented Feb 6, 2018 at 15:13
0

i have used this script on bootstrap-table.

$('#tb-pegawai').on('change', 'tr' , function (event) {
            if($('.selected')){ // } })

Not the answer you're looking for? Browse other questions tagged or ask your own question.