如何在点击复选框时检索现有的jquery数据表(在文档上初始化)

How to retrieve existing jquery datatable(initialized on document on ready) onclicking a checkbox

本文关键字:数据表 文档 jquery 初始化 复选框 检索      更新时间:2023-09-26

我已经成功地在文档上初始化了一个jquery数据表,准备进行服务器端处理。现在,我需要在单击复选框时显示相同的数据表。我该怎么做?

这是我的代码:

$(function(){
    var oTable = $('#example1').dataTable({
            //"bAutoWidth":true,
            "bDestroy":true,
            "bRetrieve": true,
            "bProcessing": true,
            "bServerSide": true,
            "bStateSave": true,
            "sAjaxSource": "/alldata/",
         });

现在,当我点击一个复选框时,我需要显示相同的表。

$("#allid").click(function()
{ 
    if ($('#allid').is(':checked')){
        oTable.fnClearTable( 0 );
        oTable.fnDraw();
   }
});

我已经尝试了上面的方法,但它不起作用,请建议我如何做到这一点?

试试这个:

$(function () {                     
    var trigger $("#allid");
    trigger.on("click", function() {
        if ($('#allid').is(':checked')) {
            oTable.fnClearTable( 0 );
            oTable.fnDraw();
            console.log("should display the table now");
        } else {
            console.log("some error");
        }   
    }); 
});

如果这不起作用,请使用try-catch块进行尝试,如下所示:

$(function () {
    try {
        var trigger $("#allid");
        trigger.on("click", function() {
            if ($('#allid').is(':checked')) {
                oTable.fnClearTable( 0 );
                oTable.fnDraw();
                console.log("should display the table now");
            }
       });
    } catch(e) {
        console.log(e);
    }   
});

打开开发人员控制台,在刷新页面时等待任何消息。粘贴错误。