简体   繁体   English

重新加载后jquery datatable突出显示下降

[英]jquery datatable highlight drops off after reload

I have a datatable which uses ajax to load the values. 我有一个使用ajax加载值的数据表。

var table = $('#example').DataTable({
    "ajax": "xxxxxx.aspx",
    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]
    }],
});

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

using setInterval, I am asking that table to be reloaded every 5 secs. 使用setInterval,我要求该表每5秒重新加载一次。

setInterval(function(){table.ajax.reload(null, false)}, 5000);

When the Datatable is reloaded, it drops off the highlight row. 重新加载数据表后,它将从突出显示行中删除。 Can anyone please tell me how to retain the highlight on the row after the reload? 有人可以告诉我如何在重新加载后在行中保留突出显示吗?

Thanks 谢谢

You could do something like that : 你可以做这样的事情:

function highlight() {
    $('#example tbody').on('click', 'tr', function () {
        if ($(this).hasClass('selected')) {
            $(this).removeClass('selected');
        } else {
            table.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    });
}

$(document).ready(function () {

    // Call the function at document ready
    highlight();

    // Call the ajax and the function every 5 seconds
    setInterval( function () {
        table.ajax.reload(null, false);
        highlight();
    }, 5000 );

});

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

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