2015-09-07 47 views
0

我有一个使用ajax加载值的数据表。jquery datatable突出显示下降后重新加载

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'); 
    } 
}); 

使用setInterval,我要求该表每5秒重新加载一次。

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

当Datatable重新加载时,它将从高亮行中删除。任何人都可以请告诉我如何在重新加载后保留行上的高光?

感谢

+0

当我点击该行,我可以看到,排在深蓝色高亮显示,但它确实落于重载 – Saj

+0

您可以使用jQuery将突出显示的行的ID保存在隐藏字段中(表外部),然后在刷新后检索id值,并使用它来选择并再次突出显示该行。 – markpsmith

回答

0

你可以做这样的事情:

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); 

}); 
+0

我想上面的代码不会工作,因为没有哪里突出显示功能将焦点设置到任何行? –

相关问题