简体   繁体   English

jquery 数据表将类添加到行

[英]jquery datatable add class to row

I have a datatable which when I click on a row, it highlights the row.我有一个数据表,当我单击一行时,它会突出显示该行。 I want to save the row index and use it to re-highlight that row when the page is reloaded.我想保存行索引并在页面重新加载时使用它来重新突出显示该行。

var table = $('#example').DataTable( {
                    "ajax": "DataQueries/FetchOpenJobs.asp",
                    stateSave: true,
                    "aLengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
                    "iDisplayLength": 25,
                    "order": [[ 0, "desc" ]],
                    columnDefs: [
                        {"visible": false, "targets": [2,3]},
                        {"type": 'date-uk', "targets": [1,9,10] },
                        {"type": 'title-string', "targets": [11] }
                    ],
                } );

I am able to get the row index using the below:我可以使用以下方法获取行索引:

var selectedRowIndex = table.row(this).index();

However, I am unsure as to how to re-highlight using the row index.但是,我不确定如何使用行索引重新突出显示。

I think this question deserves a more thorough example.我认为这个问题值得一个更彻底的例子。

I would save the selected id ('s) in the localStorage as an stringified array.我会将选定的id ('s) 作为字符串化数组保存在localStorage中。 When the page is (re)loaded then retrieve the array and if it contains any stored indexes, highlight the corresponding rows.当页面被(重新)加载时,然后检索数组,如果它包含任何存储的索引,则突出显示相应的行。 Below a code example in explanatory order :下面是按说明顺序的代码示例:

Sample code of highlightning rows when they are clicked :单击时突出显示行的示例代码:

$('#example').on('click', 'tr', function() {
    $(this).toggleClass('highlight');
    processSelected(table.row(this).index(), $(this).hasClass('highlight'));
}); 

Basic code for maintaining an array of highlighted row indexes in the localStorage :在 localStorage 中维护一组突出显示的行索引的基本代码:

function processSelected(id, selected) {
   if (selected) {
      selectedRows.push(id);
   } else {
      selectedRows.splice(selectedRows.indexOf(id), 1);
   }
   localStorage.setItem('selectedRows', JSON.stringify(selectedRows));
}   

When the page is loaded, retrieve the array from localStorage :加载页面后,从 localStorage 检索数组:

var selectedRows = JSON.parse(localStorage.getItem('selectedRows'));

Finally, re-highlight stored row indexes when the dataTable is initialised :最后,在初始化 dataTable 时重新突出显示存储的行索引:

var table = $('#example').DataTable({
    initComplete : function() {
       if (!Array.isArray(selectedRows)) {
          //selectedRows does not exists in localStorage or is messed up
          selectedRows = [];
       } else {   
          var _table = this.api(); 
          selectedRows.forEach(function(index) {
             //for some reason to$() doesnt work here 
             $(_table.row(index).node()).addClass('highlight');
          })
       }    
   }
});

demo -> http://jsfiddle.net/f3ghvz4n/演示 -> http://jsfiddle.net/f3ghvz4n/

Try the demo, highlight some rows and then reload the page.尝试演示,突出显示一些行,然后重新加载页面。 Notice that this works on a paginated dataTable as well!请注意,这也适用于分页数据表!


Update .更新 When you are using a AJAX datasrc reloaded each 30 secs, I think you could skip the initComplete and just rely on the draw.dt event :当您使用每 30 秒重新加载一次 AJAX datasrc 时,我认为您可以跳过initComplete并只依赖draw.dt事件:

var table = $('#example').DataTable();

$('#example').on('draw.dt', function() {
   if (!Array.isArray(selectedRows)) {
      selectedRows = [];
   } else {   
      selectedRows.forEach(function(index) {
         $(table.row(index).node()).addClass('highlight');
      })
   }   
})

Completely untested, but cannot see why it shouldnt work.完全未经测试,但不明白为什么它不应该工作。

This should do the trick:这应该可以解决问题:

$('#id tbody > tr').eq(rowindex)

or或者

$('#id tbody > tr').eq(rowindex).children().addClass('myClass');

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

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