如何使用数据表更新行,保持排序和搜索条件

时间:2015-12-27 20:00:04

标签: javascript jquery datatables

我有一个页面每秒加载来自JSON文件的数据并将数据放入表中。我正在使用datatables插件。

代码概述:

<!DOCTYPE html>
<html lang="en">
<head>
<script>
    function createTableBody(tdata) {
        var nRows = tdata.length;
        var tableHTML ="";
        for (i=0 ; i < nRows ; i++) {
        //create the HTML here
      };
        $('#QueuesTableBody').html(tableHTML);
    }

    function loadTableData() {
        $.ajax(
            {
          // parameters here
                success:function(data) {
                    createTableBody(data); //puts the data into the DOM
                } 
    })}


    $(document).ready(function() {
        $.ajax(
            {
          // parameters here
                success:function(data) {
                    createTableBody(data); //puts the data into the DOM
                    var wtable = $('#QueuesTable').DataTable(
                        {
                        "order": [[ 0, 'asc' ]],
                        "paging": false
                        }
                    ) 
                } 
            }); 
        setInterval(function(){
            loadTableData();
        wtable.draw(false); // datatables method for redrawing the table
        }
        ,1000);
    })
    </script>

    </head>
    <body>
    <div id="QueuesTablePage">
    <table id="QueuesTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
      <thead>
      header row goes here
      </thead>
      <tbody id="QueuesTableBody">
      </tbody>
    </table>
    </div>
    </body>
    </html>

代码工作,从某种意义上讲,每秒都会加载数据。只是它失去了排序和搜索标准。

但是,我想要实现的是保持排序和搜索条件以及窗口中的位置,这意味着,我不希望它每秒都应该向上滚动。我们说的是100行。

(注意分页已禁用)。

0 个答案:

没有答案