简体   繁体   English

无法重新初始化数据表

[英]Cannot reinitialize datatables

Hi I'm having the below code where I load the datatable on a click of the button您好我有下面的代码,我点击按钮加载数据表

$("#search").click(function () {
         // alert("Video inside");
        
            //$("#Video_Details").dataTable().fnDestroy();
          console.log(lat);
          $("#Video_Details").DataTable({

            // // Ajax for api call
            ajax: {
              //   // api endpoint uri
              //urlString: "http://localhost:8080/xx/xx/xx/xx/xx",
              url: url,
              type: "GET",
              data: function (json) {
                console.log(json);
                return json;
              },
            },

When I click the second time I'm getting cannot reinitialize error.当我第二次单击时,出现无法重新初始化错误。 Can someone please clarify?有人可以澄清一下吗?

The simplest solution is to use the DataTables retrieve option:最简单的解决方案是使用 DataTables retrieve选项:

"retrieve": true

From the documentation:从文档中:

if the table has already been initialised, this parameter will cause DataTables to simply return the object that has already been set up如果表已经被初始化,这个参数将导致 DataTables 简单地返回已经设置好的 object

The reason you get this error is because the DataTable( {...} ) function actually creates and initializes the DataTable - and you can only do this once for each table.您收到此错误的原因是DataTable( {...} ) function 实际上创建并初始化了 DataTable - 您只能为每个表执行一次。


One alternative is to use the destroy option - but that is a relatively expensive operation if you are not actually changing any of the initialization options each time you click your button.一种替代方法是使用destroy选项 - 但如果您每次单击按钮时实际上都没有更改任何初始化选项,那么这是一个相对昂贵的操作。


Another alternative is to create and initialize the table outside of the button click, and assign it to a variable:另一种方法是在单击按钮之外创建和初始化表,并将其分配给一个变量:

$(document).ready(function() {

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

} );

Now you can use the table variable.现在您可以使用table变量。 This approach means that the table will be initialized and displayed before the first click of the button - which may not be what you want.这种方法意味着表格将在第一次单击按钮之前初始化并显示 - 这可能不是您想要的。

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

相关问题 DataTables无法重新初始化或需要插件 - DataTables cannot reinitialize or plugin is required angularjs数据表无法重新初始化表错误 - angularjs Datatables Cannot Reinitialize Table Error 错误:DataTables 警告:table id = example1 - 无法重新初始化 DataTable - Error: DataTables warning: table id = example1 - Cannot reinitialize DataTable http://datatables.net/tn/3:无法重新初始化数据表 - http://datatables.net/tn/3 : Cannot reinitialize datatable 用不同的脚本文件重新初始化数据表 - Reinitialize datatables with different script file 数据表警告(table id ='inventory')即使只有一次初始化也无法重新初始化数据表 - Datatables warning(table id='inventory') cannot reinitialize data table even if there's only one initialization 无法重新初始化DataTable弹出窗口 - cannot reinitialize DataTable popup 数据表-显示更多/详细信息-重新初始化问题 - Datatables - show extra / detailed information - reinitialize issue 如何使用 MVC 中的 ajax 使用从服务器新获取的数据重新初始化数据表 - How to reinitialize dataTables with newly fetched data from server using ajax in MVC Jquery DataTable 上的 setInterval Jquery DataTable 给出错误无法重新初始化 - setInterval on Jquery DataTable Jquery DataTable giving error Cannot Reinitialize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM