javascript - get row number on row click in jQuery for bootstrap table - Stack Overflow

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

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.

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.

Share Improve this question edited May 18, 2015 at 20:19 Nitin P asked May 18, 2015 at 19:52 Nitin PNitin P 1071 gold badge2 silver badges8 bronze badges 4
  • 3 possible duplicate of jQuery: Which row number is clicked in table – Ivan Sivak Commented May 18, 2015 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 Commented May 18, 2015 at 19:56
  • @IvanSivak, tried both the answers there...didnt work for me. – Nitin P Commented May 18, 2015 at 19:57
  • @threeFourOneSixOneThree....the answers are always returning 1 if i do a +1 or in other words 0 – Nitin P Commented May 18, 2015 at 20:10
Add a ment  | 

2 Answers 2

Reset to default 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;
});

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'));
  }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745082802a4610229.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信