简体   繁体   English

jQuery数据表在重新加载后保留行突出显示

[英]jQuery datatable retain row highlight after reload

I am reloading my datatable on 10 second intervals. 我正在每隔10秒重新加载我的数据表。 When a user clicks on a row, that row will highlight. 当用户单击一行时,该行将突出显示。 But when the datatable reloads, the highlight is gone. 但是,当重新加载数据表时,高光消失了。

Here is the shortened code for my datable: 这是我的数据的简化代码:

$(document).ready(function() 
{
  // set datatable
  $('#example1').DataTable({        
    "ajax": {
      "url": "api/process.php",
      "type": "POST",
      "dataSrc": ''
    },
    "columns": [
      { "data": "" },
      { "data": "column1" },
      { "data": "column2" },
      { "data": "column3" }
    ],
    "iDisplayLength": 25,
    "order": [[ 6, "desc" ]],
    "scrollY": 600,
    "scrollX": true,
    "bDestroy": true,
    "stateSave": true
  });

  // reload datatable every 30 seconds
  setInterval(function()
  {
    var table = $('#example1').DataTable();
    table.ajax.reload();
  }, 30000);

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

All of the above works exactly how it's supposed to work. 以上所有功能都可以正常工作。 I just need to retain the row highlight after the datatable reloads. 重新加载数据表后,我只需要保留行突出显示。

Also, I attempted the answer from this post , but I scrapped it as the row no longer highlights. 另外,我尝试了这篇文章的答案,但是由于该行不再突出显示,因此我将其废弃了。

Kindly update js file with below changes. 请通过以下更改更新js文件。 Below code will save row clicked in global parameter and then focus the clicked row after ajax call. 下面的代码将单击的行保存在全局参数中,然后在ajax调用后集中单击的行。

var gblIndex = 0; //this will save row clicked index

function setFocus(){ 
$($('#example1 tbody > tr')[gblIndex]).addClass('selected');  

}


$(document).ready(function() 
{
  // set datatable
  $('#example1').DataTable({        
    "ajax": {
      "url": "api/process.php",
      "type": "POST",
      "dataSrc": ''
    },
    "columns": [
      { "data": "" },
      { "data": "column1" },
      { "data": "column2" },
      { "data": "column3" }
    ],
    "iDisplayLength": 25,
    "order": [[ 6, "desc" ]],
    "scrollY": 600,
    "scrollX": true,
    "bDestroy": true,
    "stateSave": true
  });

  // reload datatable every 30 seconds
  setInterval(function()
  {
    var table = $('#example1').DataTable();
    table.ajax.reload();
    setFocus(); // this will set focus/highlight row
  }, 30000);

  // highlight row
  $('#example1 tbody').on('click', 'tr', function()
  {
    $('#example1 tbody > tr').removeClass('selected');
    $(this).addClass('selected');
    gblIndex = $(this).index(); // this will save the index clicked
  });
});

You should review their actual selection documentation to manipulate this. 您应该查看其实际选择文档以进行操作。 This feature is already built in and setup so on ajax.reload() it will keep your selections. 该功能已内置并已设置,因此在ajax.reload()上将保留您的选择。

You can apply classes/styling with their methods as well. 您还可以将类/样式及其方法一起应用。

https://datatables.net/reference/option/#select https://datatables.net/reference/option/#select

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

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