【问题标题】:How to refresh/reload datatable after ajax success functionajax成功功能后如何刷新/重新加载数据表
【发布时间】:2018-10-31 18:57:43
【问题描述】:

添加一些数据后刷新数据表时出现问题。 另外,我在页面加载时使用函数来获取数据。 我已经尝试过数据表函数.draw(),但它第一次打破了分页限制。所以,我需要解决方案。

这是正文上的代码:

<table id="table-employee" class="table table-hover">
        <thead>             
            <tr>
                <th>No</th>
                <th>Employee ID Number</th>
                <th>Employee Name</th>
                <th>Company Name</th>
                <th>Created Date</th>
                <th>Create By</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody id="list-data-employee">
            <tr></tr>
        </tbody>
    </table>

这是脚本上的代码: https://pastebin.com/MU8iFRiq(我用了这个,因为论坛说我这个帖子里的代码太多了)

提前谢谢你。 *已编辑:在正文部分添加一些细节以及我在脚本部分所做的事情。

【问题讨论】:

  • 分享你的代码你要附加的元素datatable
  • 已编辑,抱歉解释

标签: javascript jquery datatable


【解决方案1】:
 var options = {data:[]};
 var table = $('#example').DataTable(options);
  function refresh(newData){          
     // Clear all existing data and insert fresh data and redraw datatable.
     table.clear().rows.add([newData]).draw();
   }   

$("#modal-input").on("submit","#form-add-employee", function(){
        $.ajax({
            url:"employee/add/save.json",
            type:"get",
            dataType:"json",
            data:$(this).serialize(),
            success: function(result){
                $("#modal-input").modal("hide");
                alert("Data Saved! New Employee has been add with employee ID Number "+result.employeeModel.employee_number);
             refresh(result);
            }
        });
        return false;
    });

我创建了一个带有工作示例的 jsfiddle - http://jsfiddle.net/p74sytjc/

【讨论】:

  • 出现错误。未捕获的 TypeError:options.data 不是函数
  • 请分享ajax响应result的结构。您在哪里定义了 option 变量。它是全局变量还是任何局部变量。
  • @jadugdestian 答案已更新并随 jsfiddle 演示提供 - jsfiddle.net/p74sytjc
猜你喜欢
  • 2016-12-17
  • 1970-01-01
  • 2018-05-09
  • 2021-05-16
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
相关资源
最近更新 更多