简体   繁体   English

jQuery DataTables 行高亮显示

[英]jQuery DataTables row highlighting

Using jQuery DataTables, I am using the below code to highlight a selected row:使用 jQuery DataTables,我使用下面的代码来突出显示选定的行:

 var table = $('#example1').DataTable();        
 $('#example1 tbody').on('click', 'tr', function()
 {
   if($(this).hasClass('selected')){
     $(this).removeClass('selected');
   }
   else{
     table.$('tr.selected').removeClass('selected');
     $(this).addClass('selected');
   }
 });

I also have this in-page CSS class at the top of the page:我还在页面顶部有这个页内 CSS 类:

 <style>
   tr.selected {background-color: #a6a6a6;}
 </style>

All of the above works accordingly.以上所有工作都相应地起作用。 I can click anywhere on the row, and the row will be highlighted.我可以单击行上的任意位置,该行将突出显示。

However, I have encountered a problem.但是,我遇到了一个问题。 In each row, there are links that the user can click on to open a modal window.在每一行中,都有用户可以单击以打开模态窗口的链接。 If the user clicks directly on the link, the modal opens and the row is indeed highlighted.如果用户直接单击链接,则模式会打开并且该行确实突出显示。

The problem occurs if the user clicks the row first, then within the same row, clicks the link to open the window...now the row is no longer highlighted.如果用户首先单击该行,然后在同一行中单击链接以打开窗口,则会出现问题……现在该行不再突出显示。

What I need to do is keep the highlight on the row no matter how many times they click the same row - until they click a different row.我需要做的是无论他们单击同一行多少次都保持该行的突出显示 - 直到他们单击不同的行。

How can I adjust the jQuery above to make this happen?我如何调整上面的 jQuery 来实现这一点?

You may want to un-highlight all of the rows, then hightlight the current row: 您可能要取消突出显示所有行,然后突出显示当前行:

$('#example1 tbody').on('click', 'tr', function() {
  $('#example1 tbody > tr').removeClass('selected');
  $(this).addClass('selected');
});

You may want to un-highlight the selected row, and then hightlight the current row:您可能希望取消突出显示所选行,然后突出显示当前行:

   /*DataTables highlight selected row*/
    $('#dataTable tbody').on('click', 'tr', function() {
        $('#dataTable tbody > tr').removeClass('row_selected');
        $(this).addClass('row_selected');
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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