在删除表中的数据之前先清除表 [英] Clear table before reloding the data in a table

查看:73
本文介绍了在删除表中的数据之前先清除表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绑定数据库中的数据。发生的是它每5秒钟绑定一次。现在,它已正确绑定,但不会清除以前的数据。它不断堆积。因此,如果有3行..它仅需要显示3行。现在发生的事情是每5秒添加3行,并每5秒保持6-9-12个起球。
我需要清除数据,然后每5秒加载一次。

I am trying to bind data from the database. what is happening is that it binds every 5 seconds. Now its binding properly but it does not clear the earlier data. it keeps pilling up. So if there is 3 rows .. it needs to show only 3 rows. now what is happening is it is adding 3 rows every 5 seconds and keeps pilling 6-9-12 every 5 seconds. I need to clear the data and then load every 5 seconds.

<script type="text/javascript">
        $(document).ready(function () {
            Get();
            setInterval(function () {
                Get() // this will run after every 5 seconds
            }, 5000);
        });

    function Get() {
        $.ajax({
            type: "POST",
            url: "../adminpage/noti.ashx",
            data: {},
            dataType: "json",
            success: function (response) {
                $.each(response, function (index, itemData) {
                    var tr = "<tr>" +
                                  "<td>" + itemData.msg + "</td>" +
                                  "<td>" + itemData.dt + "</td>" +
                                  "</tr>";

                    $("#testTable").find("tbody").append(tr);
                });

                BindTable();
            }
        });
    }
    function BindTable() {

        try {
            $('#testTable thead th').each(function (i) {
                var title = $('#testTable thead th').eq($(this).index()).text();

                if (title != "") {
                    $(this).html('<div style="width:40%;text-align:center;white-space:nowrap">' + title + '</div>');
                }
            });

        }
        catch (e) { }
    }
</script>


<table id="testTable" class="display" cellspacing="0" width="100%">
                                        <thead>
                                            <tr>
                                                <th class="m-list-timeline__text" style="width:70%">msg</th>
                                                <th class="m-list-timeline__time" style="width:30%">dt</th>
                                            </tr>
                                        </thead>
                                          <tbody></tbody>
                                    </table>


推荐答案

尝试清除所有< tr> 结点之前的结点上的结点:

Try clearing all <tr>nodes from your tbody before you append your results:

success: function (response) {
    $("#testTable").find("tbody").empty(); //clear all the content from tbody here.
    $.each(response, function (index, itemData) {
         /*do stuff*/
         $("#testTable").find("tbody").append(tr);
    });
    BindTable();
}

这篇关于在删除表中的数据之前先清除表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆