1

As easy as it sounds but I am stuck in retrieving the row number of the current clicked/selected row.

I have tried the following so far. I am using bootstrap table just in case you want to know.

$('#myTable').on('click-row.bs.table', function (e, row, $element) {

     //var row_num = parseInt($(this).parent().index()) + 1;

     //var row_num = $(this).closest('tr').index();

     //var row_num = $(this).closest('td').parent()[0].sectionRowIndex;

}

None seem to be working for me.

Nitin P
  • 107
  • 1
  • 2
  • 7
  • 3
    possible duplicate of [jQuery: Which row number is clicked in table](http://stackoverflow.com/questions/4524661/jquery-which-row-number-is-clicked-in-table) – Ivan Sivak May 18 '15 at 19:54
  • If you add your html and describe what you want to achieve (for example if a row is clicked it should get a different background) we can help you better. Could you explain why the answers did not work for you?. – surfmuggle May 18 '15 at 19:56
  • @IvanSivak, tried both the answers there...didnt work for me. – Nitin P May 18 '15 at 19:57
  • @threeFourOneSixOneThree....the answers are always returning 1 if i do a +1 or in other words 0 – Nitin P May 18 '15 at 20:10

2 Answers2

4

Th problem you have is simply about the way that click-row.bs.table works, as far as click-row.bs.table is a table event, this keyword points to the table itself not to the row, so you just need to use the $element instead:

$('#myTable').on('click-row.bs.table', function (e, row, $element) {
  var row_num = $element.index() + 1;
});
Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
-2

Its better to use the data attribute with the index

$element.data('index')

The method .index() will change if there are rows added for example by using expandRow

// Open or close on click
$table.on('click-row.bs.table', function (e, row, $element, index) {
  if(!row.open){
    $table.bootstrapTable('expandRow', $element.data('index'));
  }
  else{
    $table.bootstrapTable('collapseRow', $element.data('index'));
  }
});
Remi
  • 474
  • 1
  • 4
  • 10