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
2 Answers
Reset to default 4Th 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'));
}
});