简体   繁体   中英

Change color clicked row in Datatable

How can I change color of a clicked row of a Datatable?

I have my data table and a JavaScript to do some operations with the data of a clicked row:

var table = $('#myTable').DataTable();
$('#myTable tbody').on('click', 'tr', function () {
   var data = table.row( this ).data();
   // below some operations with the data
   // How can I set the row color as red?
} );

Thanks

I'm not sure the relevance of DataTable in this case but you can do this with jQuery.

Example: https://codepen.io/alexpetergill/pen/ccd10f785e6b6d9e2c2b2503b219eab4

var table = $('#myTable').DataTable();
$('#myTable').on('click', 'tr', function () {
   var data = table.row( this ).data();
   // below some operations with the data
   // How can I set the row color as red?
  $(this).addClass('highlight').siblings().removeClass('highlight');
});

If you want to remove the highlight following pagination then I would suggest using one of DataTables custom events...

$('#myTable').on('draw.dt', function () {
  $(this).find('.highlight').removeClass('highlight');
});

https://datatables.net/reference/event/draw

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM