Javascript 刷新jquery数据表而不刷新整个页面

Javascript 刷新jquery数据表而不刷新整个页面,javascript,php,jquery,Javascript,Php,Jquery,我想每3秒钟刷新一次jquery数据表。我已经这样做了,但我有分页问题。当我执行代码时,分页就消失了,所以所有的数据都显示出来,没有分页 这是我的密码: <div id="timexx"> <table id="example1" class="table table-bordered table-striped" style="margin-right:-10px"> <thead> <tr> <th

我想每3秒钟刷新一次jquery数据表。我已经这样做了,但我有分页问题。当我执行代码时,分页就消失了,所以所有的数据都显示出来,没有分页

这是我的密码:

<div id="timexx">
  <table id="example1" class="table table-bordered table-striped" style="margin-right:-10px">
    <thead>
      <tr>
        <th>id #</th>
        <th>Name</th>
      </tr>
    </thead>
    <?php
      include('../dist/includes/dbcon.php');

      $query=mysqli_query($con,"SELECT * FROM accounts_tic WHERE statuss = 'New' OR statuss = 'On-Process' order by id DESC")or die(mysqli_error());
      while($row=mysqli_fetch_array($query)){
        $id=$row['id'];
        $name=$row['name'];

        ?>
      <tr>
        <td>
          <?php echo $id;?>
        </td>
        <td>
          <?php echo $name;?>
        </td>
      </tr>

      <?php }?>
  </table>
</div>

我建议使用ajax数据源以及。事实上,我在那里链接的docs页面有一个例子,说明了你在追求什么

对于标记,您只需创建表和标题:

<table id="example1" class="table table-bordered table-striped" style="margin-right:-10px">
    <thead>
        <tr>
            <th>id #</th>
            <th>Name</th>
        </tr>
    </thead>
</table>
现在,您需要在为ajax数据源声明的端点处创建一个网页
http://example.com/table_data.php
将表数据作为json输出。有几种方法可以将其结构化,但我更喜欢使用
data
属性作为

此处显示的数据参数格式可用于简化的DataTables初始化,因为数据是DataTables在源数据对象中查找的默认属性

基于此目标,您的php脚本可能如下所示:

include('../dist/includes/dbcon.php');
$query = mysqli_query($con, "SELECT * FROM accounts_tic WHERE statuss = 'New' OR statuss = 'On-Process' order by id DESC") or die(mysqli_error($con));
$results = [];
while ($row=mysqli_fetch_array($query)) {
    $results['data'][] = [
        'id'   => $row['id'],
        'name' => $row['name']
    ];
}
echo json_encode($results);

旁注,需要一个单独的参数,它应该是您的连接资源。

您可能需要研究如何使用
ajax
这里是jQuery在dataTable()中使用ajax调用,在setintervel-@phantom-hi中使用ajax.reload(),我的答案对您有帮助吗?@billynoah它将显示数据,但不在数据表中。请帮忙me@phantom-我不知道你的意思。我的回答明确地将数据重新加载到datatable中。
$(document).ready( function () {
    var table = $('#example1').DataTable({
        "ajax" : 'http://example.com/table_data.php',
        "columns" : [
            { "data" : "id" },
            { "data" : "name" )
        ]
    });
    setInterval(function () {
        // the second argument here will prevent the pagination
        // from reseting when the table is reloaded
        table.ajax.reload( null, false );
    }, 3000);
});
include('../dist/includes/dbcon.php');
$query = mysqli_query($con, "SELECT * FROM accounts_tic WHERE statuss = 'New' OR statuss = 'On-Process' order by id DESC") or die(mysqli_error($con));
$results = [];
while ($row=mysqli_fetch_array($query)) {
    $results['data'][] = [
        'id'   => $row['id'],
        'name' => $row['name']
    ];
}
echo json_encode($results);