每隔x秒刷新数据表 - jquery

时间:2018-04-13 20:33:55

标签: php jquery ajax datatable

我正在尝试每隔2秒刷新一次数据表。

我的代码:

<script type="text/javascript">
var user = '<?php echo $_SESSION['username']?>';
jQuery('.js-dataTable-full-pagination-help').DataTable({


pagingType: "full_numbers",
columnDefs: [ { orderable: false, targets: [ 4 ] } ],
pageLength: 10,
lengthMenu: [[5, 10, 15, 20], [5, 10, 15, 20]],
dom: 'lfBrtip',
buttons: [
        'copy',
        {
            extend: 'excel',
            messageTop: 'report'
        },
        {
            extend: 'pdf',
            title: ' report.'
        },
        {
            extend: 'print',
            messageTop: 'printed by - '+user,
            title : ' report',
            exportOptions: {
                      columns: [1, 2,3,4, 5 ]
                  }

        }
    ]


});

</script>
我尝试了什么:

setInterval(refreshTable , 1000 );});

我尝试了ajax.reload ...

我收到此错误:

  

未捕获的SyntaxError:意外的标识符

我有一个ajax代码,用于向数据库添加值并在数据表中显示它们。我想每2秒刷新一次表,这样当用户添加它会立即显示在表上时。

1 个答案:

答案 0 :(得分:0)

语法错误,setInterval将参数作为参数,以及以毫秒为单位的时间,如下所示:

var datatables = ('.js-dataTable-full-pagination-help').datatables ({Your-configurations}); // keep a reference to datatables api.
setInterval(refreshTable ,1000);

或者您可以将匿名函数作为第一个参数传递:

setInterval (function () {
    // Do some more stuff
    refreshTable ();
}, 1000);

甚至更简单:

setInterval (datatables.ajax.reload, 1000); // then you can delete the refreshTable function entirely.

假设你在该范围内有函数refreshTable:

function  refreshTable() {
     // this function will be called every second
     // run here the ajax reload according to Datatables api
     datatables.ajax.reload ();
}