简体   繁体   English

如何在jQuery中将数据追加到数据表?

[英]How to append data to datatables in jquery?

I have been trying to append data to a data table in jquery, data is appending to data table but data table is not working properly that means sorting, pagination, the search is not working. 我一直试图将数据追加到jquery中的数据表,数据追加到数据表,但是数据表不能正常工作,这意味着排序,分页,搜索不起作用。 Even data is there in the data table but it is showing 0 records. 数据表中甚至有数据,但显示0条记录。 Am trying to make all functionalities work properly, please help me on this. 我正在尝试使所有功能正常工作,请为此提供帮助。

<table id="newexample1">
    <thead>
        <tr>
            <th>Name</th>
            <th>Employee</th>
            <th>InTime</th>
        </tr>
    </thead>
    <tbody class="tablebody">
    </tbody>
</table>
<script>
    function newfunction() {
        $(".tablebody").append("<tr onclick='historyfunction()'><td>Sasi Kumar</td><td>Suresh Kumar</td><td>21/06/2019 10:59:02</td></tr>");
    }
</script>
<script>
    $(document).ready(function() {
        $('#newexample1').DataTable();
    });
</script>

Am expecting a proper data table. 我期待一个正确的数据表。

function newfunction() {
        $(".tablebody").append("<tr onclick='historyfunction()'><td>Sasi Kumar</td><td>Suresh Kumar</td><td>21/06/2019 10:59:02</td></tr>");
        $(".tablebody").append("<tr onclick='historyfunction()'><td>omar omar</td><td>Suresh Kumar</td><td>21/06/2019 10:59:02</td></tr>");
    }

    $(document).ready(function() {
        newfunction();
        $('#newexample1').DataTable();
    });

Here a working JSfiddle 是一个工作的JSfiddle

In your code newfunction() is never called so the table is empty when Datatable tries to render the table. 在您的代码中,从不调用newfunction()因此当Datatable尝试呈现表时,该表为空。 Add row before render Datatable is instantiated in $(document).ready(function() (otherwise you get 0 rows table). Otherwise, if you would add rows after Datatable is rendered, use rows.add() and then redraw the table on the screen with draw() function. $(document).ready(function()实例化渲染数据表之前添加行(否则您将获得0行表)。否则,如果要在渲染数据表后添加行,请使用rows.add()然后重绘该表在屏幕上使用draw()函数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM