如何使用setInterval刷新dataTable?

时间:2017-06-03 14:11:41

标签: javascript php jquery html ajax

我的项目需要每1分钟刷新一次dataTable,我已经编写了php和setInterval。该表很好地显示但使用setInterval看起来不刷新。我不知道这个问题是什么。

这是我的tableSpa.php

<?php
class x{
    function koneksi(){
        $serverName = "DESKTOP-49HH41E\SA";
        $conn = new PDO("sqlsrv:server=$serverName; Database=tebu", "sa", "sa12345");

        return $conn;
    }

    function get_hari(){
        date_default_timezone_set('Asia/Jakarta');
        return date("j F Y G:i:s");
    }

    function KueriSql(){
        $query = "SELECT     TOP (100) PERCENT a.KD_KT, a.DESA, a.NM_KUD, a.AFDL, a.kd_wil, a.kd_rayon, a.Rayon, a.KETUA,   ISNULL(MAX(b.libur), 0) AS Libur, ISNULL(MAX(b.aktif), 0) AS Aktif,
                          (SELECT     ISNULL(SUM(tambahan), 0) AS Tambahan
                            FROM          dbo.tblAlokasiSPTAA AS c
                            WHERE      (kd_kt = a.KD_KT) AND (tambahan <> 0) AND (tgl = '2017-05-28')) AS Tambahan, COUNT(d.spa) AS TotalCetak,
                          (SELECT     ISNULL(COUNT(spa), 0) AS Pemasukan
                            FROM          dbo.vSPATrimaGawang AS e
                            WHERE      (LEFT(Kdptn, 5) = a.KD_KT) AND (tglberlaku = '2017-05-28 23:59:59')) AS Pemasukan,
                          (SELECT     ISNULL(COUNT(spa), 0) AS SPTAMati
                            FROM          dbo.vSPTAMati AS f
                            WHERE      (LEFT(Kdptn, 5) = a.KD_KT) AND (tglberlaku = '2017-05-28 23:59:59')) AS SPTAMati
                        FROM         dbo.vM_KEL AS a LEFT OUTER JOIN
                                              dbo.TblJatahSPTA AS b ON a.KD_KT = b.kd_kt LEFT OUTER JOIN
                                              dbo.tblSPA AS d ON a.KD_KT = LEFT(d.Kdptn, 5) AND d.tglberlaku = '2017-05-28 23:59:59'
                    GROUP BY a.KD_KT, a.DESA, a.NM_KUD, a.AFDL, a.kd_wil, a.kd_rayon, a.Rayon, a.KETUA
                    ORDER BY a.KD_KT";

        $stmt = $this->koneksi()->prepare($query);
        $stmt->execute();

        $hasil = $stmt->fetchAll();
        return $hasil;
    }

    public $arr = array();
    public $arrX=0;

    function tampil_kueri(){


        foreach ($this->KueriSql() as $row) {

            $this->arr[$this->arrX][0] = $this->arrX;
            $this->arr[$this->arrX][1] = $row['KD_KT'];
            $this->arr[$this->arrX][2] = $row['KETUA'];
            $this->arr[$this->arrX][3] = $row['AFDL'];
            $this->arr[$this->arrX][4] = $row['Tambahan'];
            $this->arr[$this->arrX][5] = $row['TotalCetak'];
            $this->arr[$this->arrX][6] = $row['Pemasukan'];
            $this->arr[$this->arrX][7] = $row['SPTAMati'];


            $this->arrX++;
        }
    }


    function tampil_data(){
        for ($baris=0; $baris < $this->arrX; $baris++) { 
                            echo "<tr>";

                        for ($kolom=0; $kolom < 8; $kolom++) { 
                            echo "<td>".$this->arr[$baris][$kolom]."</td>";
                        }
                        echo "</tr>";
                    }   
    }
}

$obj = new x();

if($obj->koneksi()){
    echo "Koneksi Datbase SQL SERVER Berhasil";
    $obj->tampil_kueri();

}

?>

<!DOCTYPE html>
<html>
<head>



<script type="text/javascript" src="assets/DataTables/media/js/jquery.js"></script>
    <script type="text/javascript" src="assets/DataTables/media/js/jquery.dataTables.js"></script>
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="assets/DataTables/media/css/jquery.dataTables.css">
    <link rel="stylesheet" type="text/css" href="assets/DataTables/media/css/dataTables.bootstrap.css">
</head>
<body>
    <center>


<h1>Tabel SPTA<br><?php echo $obj->get_hari();?></h1>
</center>
<br/>
<br/>
<div class="container-fluid">
    <div class="table-responsive">
    <table border = '0' class="table table-striped table-bordered data" id="tabelSpta">
        <thead>
            <tr>
                <th>No.</th>            
                <th>Kode_KT</th>
                <th>Nama_KT</th>
                <th>Afdeling</th>
                <th>Tambahan</th>
                <th>Total Cetak</th>
                <th>Pemasukan</th>
                <th>SPTA Mati</th>

            </tr>
        </thead>
        <tbody>

            <?php echo $obj->tampil_data();         ?>


        </tbody>
    </table>
</div>
</div>
</body>
<script type="text/javascript">


$(document).ready(function(){
        var tabel = $('.data').DataTable();
    });

    setInterval(function () {
      tabel.ajax.reload();
  }, 60000);
</script>
</html>

1 个答案:

答案 0 :(得分:1)

使用fnReloadAjax()代替ajax.reload()。 您需要将脚本//cdn.datatables.net/plug-ins/1.10.15/api/fnReloadAjax.js之后的jquery插件jquery.dataTables.js添加到<head>标记中。

更改

setInterval(function () {
      tabel.ajax.reload();
}, 60000);

setInterval(function () {
      tabel.fnReloadAjax();
}, 60000);