【问题标题】:Clear table before reloding the data in a table在重新加载表中的数据之前清除表
【发布时间】:2018-05-22 10:34:56
【问题描述】:

我正在尝试绑定数据库中的数据。发生的事情是它每 5 秒绑定一次。现在它的绑定正确,但它不会清除早期的数据。它一直在起球。所以如果有 3 行 .. 它只需要显示 3 行。现在发生的事情是它每 5 秒添加 3 行,并且每 5 秒保持 6-9-12 起球。 我需要清除数据,然后每 5 秒加载一次。

<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>

【问题讨论】:

    标签: javascript asp.net ajax datatables


    【解决方案1】:

    在附加结果之前,尝试清除 tbody 中的所有 &lt;tr&gt;nodes:

    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();
    }
    

    【讨论】:

      【解决方案2】:
      $('#myTable').empty() 
      

      尝试在 ajax 调用之前删除表格的内容 然后填写数据

      【讨论】:

      • '$('#testTable').empty()'
      • 您可以编辑您的帖子 Akuria。将文本格式化为代码,用反引号或缩进 4 个空格
      【解决方案3】:
      <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) {
      
                    // Check if response data is not empty 
                  if (response) {
                    // Empty previous data
      
                       $("#testTable").empty();
                    // Iterate data for each row
      
                       $.each(response, function (index, itemData) {
                       var tr = "<tr>" +
                                    "<td>" + itemData.msg + "</td>" +
                                    "<td>" + itemData.dt + "</td>" +
                                    "</tr>";
      
                      $("#testTable").find("tbody").append(tr);
                  });
      
                  BindTable();
              }
             }
          });
      }
      

      【讨论】:

      • 对你的答案给出简短的解释,否则其他人会用负分标记这个答案。
      猜你喜欢
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2011-08-20
      • 1970-01-01
      • 2023-03-03
      • 2013-04-13
      • 2018-02-10
      相关资源
      最近更新 更多