jQuery 数据表警告(表 ID = 'IDTableName'):无法重新初始化数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18489663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:45:43  来源:igfitidea点击:

DataTables warning (table id = 'IDTableName'): Cannot reinitialise DataTable

jquerydatatables

提问by Khristian Liahut

I'm using Jquery Datatable in ASP.NET, and UpdatePanel (scriptManager) . I have the next below error:

我在 ASP.NET 和 UpdatePanel (scriptManager) 中使用 Jquery Datatable。我有以下错误:

DataTables warning (table id = 'tbVerificationApplicant'): Cannot reinitialise DataTable.

To retrieve the Datatables object for this table, pass no argument or see the docs for bRetrieve and bDestroy

数据表警告(表 ID = 'tbVerificationApplicant'):无法重新初始化数据表。

要检索此表的 Datatables 对象,请不传递任何参数或查看 bRetrieve 和 bDestroy 的文档

This is the Jquery file to create the table:

这是用于创建表的 Jquery 文件:

function DatatablesExec() { 
        $('#tbVerificationApplicant').dataTable({
            'bProcessing': true,
            'bServerSide': true,
            "sPaginationType": "full_numbers",
            'sAjaxSource': 'listVerificationData.ashx?ddlStatusValue=' + $("#ddlStatusClient option:selected").text(),
            "fnDrawCallback": function () {
                $('#tbVerificationApplicant tbody tr').click(function () {
                    var hRef = $("td:eq(0)", this).text();
                    document.location.href = 'frm_VerifyIdentity.aspx?ID=' + hRef;
                });
            }
    });
}

$(document).ready(function () {
    /* Initialise the DataTable */

        DatatablesExec()

});

But, to avoid that the table desapear after I changed the dropdownlist, I added the next below code in the code behind in the web form.

但是,为了避免更改下拉列表后表格消失,我在 Web 表单后面的代码中添加了下面的代码。

protected void Page_Prerender(object sender, EventArgs e)
{
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "_function_dummyname", "<script type='text/javascript'>DatatablesExec();</script>", false);
    }
}

It's working well, but at the begining appear a pop up with this error.

它运行良好,但在开始时会出现一个带有此错误的弹出窗口。

Thi is part of the web form:

这是网络表单的一部分:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlStatusClient" EventName="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <table id="tbVerificationApplicant" class="display">
                <thead>

回答by SophiaAP

This drove me nuts, too, so I thought I'd add the solution that worked for me.

这也让我发疯,所以我想我会添加对我有用的解决方案。

If you're sending any Ajax requests that respond in some element of your table, in your table configuration you'll need to set

如果您发送的任何 Ajax 请求在表的某些元素中做出响应,则需要在表配置中设置

bRetrieve: true 

SUVASH'S BLOG explains:

SUVASH 的博客解释说:

There is an another scenario ,say you send more than one ajax request which response will access same table in same template then we will get error also.In this case fnDestroy method doesn't work properly because you don't know which response comes first or later.Then you have to set bRetrieve TRUE in data table configuration.That's it.

还有另一种情况,假设您发送多个 ajax 请求,哪个响应将访问同一个模板中的同一个表,那么我们也会得到错误。在这种情况下,fnDestroy 方法无法正常工作,因为您不知道哪个响应首先出现或稍后。然后你必须在数据表配置中设置 bRetrieve TRUE。就是这样。

回答by Peter-Pan

When your page loads ,your function DatatablesExec()is executed twice: once in code-behind Page_Prerenderand once in $(document).readyfunction.

当您的页面加载时,您的函数DatatablesExec()会执行两次:一次在代码隐藏中Page_Prerender,一次在$(document).ready函数中。

The error means you are trying to make a table a Datatable, when it infact is already a Datatable.

该错误意味着您正在尝试将表变成数据表,而实际上它已经是数据表。

You will have to rethink your design here.

您将不得不在这里重新考虑您的设计。

Why is it neccessary to RegisterStartupScript in PreRender?

为什么在 PreRender 中需要 RegisterStartupScript?

Try removing either one of the two DatatablesExec()and your problem should dissapear!

尝试删除两者之一,DatatablesExec()您的问题应该会消失!

回答by Waqas colt

I have had this problem too i read different threads about solving this but no luck. But at the end i have initialized datatable in jquery window load method

我也有这个问题,我阅读了关于解决这个问题的不同线程,但没有运气。但最后我在 jquery 窗口加载方法中初始化了数据表

$(window).load(function(){
$("#example").DataTable();
}); 

instead of jquery's document ready method

而不是 jquery 的文档就绪方法

$(document).ready(function(){
$("#example").DataTable();
});

and miracle happens :) .. Hope it helps you.Thanks

奇迹发生了:) .. 希望对你有帮助。谢谢

回答by user3377911

Hello everyone i just want to add something in this case, i also experience it and also search for the solution for this problem but now i found the answer rather than using bDestroy always check your referencing id of the datatable in my case if you already use this

大家好,我只是想在这种情况下添加一些东西,我也经历过它并搜索此问题的解决方案但现在我找到了答案而不是使用 bDestroy 如果您已经使用,请始终检查我的数据表的引用 ID这个

$('#dtAddParticipantList').dataTable 

and your other table also use this

你的另一张桌子也用这个

$('#dtAddParticipantList').dataTable 

change it to something different

把它改成不同的东西

var oTable = $('#dtParticipantList').dataTable({ 
 //some code stuffs
});

回答by Bettaibi Nidhal

Try to add destory: trueto your dataTable configuration:

尝试添加destory: true到您的 dataTable 配置中:

$('#dataTable').dataTable({
  destory: true,
  ...
});