数据表在按钮单击后初始化表(ajax,jquery)

Mac*_*wel 5 javascript ajax jquery datatables

我在加载数据表对象时遇到问题。当我在页面加载时初始化并填充表时,它可以正常工作。

下面的代码在页面重新加载时完美运行。

<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" charset="utf-8">

$(document).ready(function(){
  var table = $('#dt_110x_complex').DataTable({
    paging : true,
    scrollY: 300,
    ajax: "{{ url_for('complex_data') }}"
  });

});
</script>
Run Code Online (Sandbox Code Playgroud)

但下面的代码在单击按钮时不起作用。我究竟做错了什么?

$(function() {
    $('#proces_input').on('click', function() {
        alert('Im in')
        var table = $('#dt_110x_complex').DataTable({
        paging : true,
        scrollY: 300,
        ajax: "{{ url_for('complex_data') }}"
        });

        });
    });
Run Code Online (Sandbox Code Playgroud)

按钮 id =“process_input”。单击按钮后会显示消息警报(“我正在”)。

下面是我的数据表的 html 表代码(两个示例相同):

<div class="row">
<div class="col-lg-12">
<table id="dt_110x_complex" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>id</th>
<th>code</th>
<th>date</th>
<th>blocade</th>
<th>konti</th>
<th>free</th>
<th>occ</th>
<th>origin</th>
<th>type</th>
<th>created</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

Sha*_*upe 3

根据你的评论

这不可能是服务器问题:(两个ajax请求是相同的。

如果您将数据显示到同一个表,则可能存在数据表初始化问题

如果是这样,您需要destroy数据表并重新初始化它单击按钮:

使用 destroy : true,

$(function() {
    $('#proces_input').on('click', function() {
        alert('Im in')
        var table = $('#dt_110x_complex').DataTable({
            paging : true,
            destroy : true,    <-------Added this 
            scrollY: 300,
            ajax: "{{ url_for('complex_data') }}"
        });
    });
});
Run Code Online (Sandbox Code Playgroud)