Get row index

Can anyone advise how I cn get the row index of the selected row from this jQuery?

$table.on("click-row.bs.table", function(e, row, $tr) {
}

Open in new window

martmacAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
Try this:

$('td').click(function(){
   var row_index = $(this).parent().index();
   var col_index = $(this).index();
});

Open in new window


If you need the index of table contain td then you can change it to
var row_index = $(this).parent('table').index();

Open in new window


Also you can try this code for your table

var table = $('#example').DataTable();
 
$('#example tbody').on( 'click', 'tr', function () {
    alert( 'Row index: '+table.row( this ).index() );
} );

Open in new window

0
martmacAuthor Commented:
Thanks for your prompt response Misha, much appreciated Sorry for the delayed response, but I have been experimenting with your code. The issue I have (if I can wrap some context around it) is that I need to get the row index as part of another function see below;

                     $table.on("click-row.bs.table", function(e, row, $tr) {  // as part of this click I need the table row to pass to a row-index variable

                                                                                            
                                if ($tr.next().is('tr.detail-view')) {
                                    //  the row value that is obtained above is passed in this to provide the 'index'
                                    $('.spanEdit').attr('id', 'desc'+row_index);  

                                    $table.bootstrapTable('collapseRow', $tr.data('index')); 
                                                                        
                                } 
                                    else {                                       
                                                                            
                                    $('.spanEdit').attr('id', 'desc'+row_index);                                                                              
                                    $table.bootstrapTable('expandRow', $tr.data('index'));

                               
                                }

                                });

Open in new window

0
Vijaya KumarCommented:
hopes this helps

 "initComplete": function () {
	var api = this.api();
		api.$('tr').click( function () {
		var index = $(this)[0]._DT_RowIndex;
		console.log(index);
		 } );
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vijaya KumarCommented:
initcomplete is inbuild datatable function u can pass as argument in

$('#example').dataTable({initcomplete});
0
martmacAuthor Commented:
Excellent, thanks very much for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.