【问题标题】:JQuery Uncaught TypeError: Cannot read property 'reload' of undefinedJQuery Uncaught TypeError:无法读取未定义的属性“重新加载”
【发布时间】:2019-09-07 06:04:40
【问题描述】:

我想在数据库中插入新数据后重新加载数据表。我已经编写了这段 HTML 代码。

<table class="table table-striped table-bordered bootstrap-datatable datatable" id="Slider_table">
<thead>
    <tr>
        <th>Title</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
    <?php foreach($items as $row){?>
    <tr>
        <td><?=$row->item_title;?></td>
        <td class="center"><?=$row->item_price;?></td>
   </tr>
</tbody>

但它在 console.log 中给了我这个错误。

未捕获的类型错误:无法读取未定义的属性“重新加载” 在那条线上$("#Slider_table").DataTable().ajax.reload();

$('#add_items').on('click', function(e){
    e.preventDefault();
    var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form

    $.ajax({
        url: "<?= base_url(); ?>Items/add_items",
        type: "post",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(output) {
            $("#Slider_table").DataTable().ajax.reload();
        }
    });
});

【问题讨论】:

  • 1.10.0版本提供reload,你用的是什么版本?
  • 'reload' of undefined 表示无法重新加载。至少.ajax 有问题。
  • @Deepak 我正在使用最新版本

标签: jquery datatable


【解决方案1】:

您必须将数据表引用存储在一个变量中。根据您使用的代码,您不能使用数据表 ajax 重新加载方法。

所以你必须申请以下解决方案:

定义表变量: 变量表;

并像这样在文档就绪功能上加载数据表。

table = $("#Slider_table").DataTable();

$('#add_items').on('click', function(e){
    e.preventDefault();
    var formData = new FormData($("#form1")[0]); //It automatically collects all fields from form

    $.ajax({
        url: "<?= base_url(); ?>Items/add_items",
        type: "post",
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(output) {
            table.row.add($(output)).draw();
            // Here in output you have to return table rows html from php

        }
    });
});

【讨论】:

    【解决方案2】:

    你可以使用

      dtable.draw();
    

    成功后。它会刷新你的数据表

    【讨论】:

    • $("#Slider_table").location.reload();或简单的 location.reload();成功后?
    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 2015-05-07
    • 1970-01-01
    • 2021-04-28
    • 2017-07-20
    • 1970-01-01
    • 2020-03-28
    • 2023-04-11
    相关资源
    最近更新 更多