JQuery DataTables服务器端自动刷新

时间:2014-09-15 19:41:50

标签: jquery ajax jquery-datatables

你好,抱歉我的英语不好,但我希望,每个人都能理解我的意思。

我在MySQL数据库中创建了一个表,每秒都会填充数据(日志条目)。所以我用JQuery和Datatables-Plugin创建一个html端来查看条目,每次看到最新的条目时我都不能用PhpMyAdmin登录。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">

<title>DataTables example - Server-side processing</title>
<link rel="stylesheet" type="text/css" href="./css/jquery.dataTables.css">


 <script type="text/javascript" language="javascript" src="./js/jquery.js"></script>
 <script type="text/javascript" language="javascript" src="./js/jquery.dataTables.js"></script>
 <script type="text/javascript" language="javascript" src="./js/fnReloadAjax.js"></script>

 <script type="text/javascript" language="javascript" class="init">

$(document).ready(function() {
    $('#log').DataTable( {
        "bStateSave": true,
        "processing": true,
        "serverSide": true,
        "cache": false,
        "aaSortingFixed": [[4,'desc']],
        "aoColumns": [
            { "sClass": "left" },
            { "sClass": "center" },
            { "sClass": "right" },
            { "sClass": "center" },
            { "sClass": "center" },
            { "sClass": "center" }
        ],
        "ajax": "server_processing.php"
} );

setInterval(function() {otable.fnReloadAjax(null)}, 10000);


} );


</script>

</head>

<body class="dt-example">

<table id="log" class="display" cellspacing="0" width="75%">
<thead>
    <tr>
        <th>TEST A</th>
        <th>TEST B</th>
        <th>TEST C</th>
        <th>TEST D</th>
        <th>TEST E</th>
        <th>TEST F</th>
    </tr>
</thead>

<tfoot>
    <tr>
        <th>TEST A</th>
        <th>TEST B</th>
        <th>TEST C</th>
        <th>TEST D</th>
        <th>TEST E</th>
        <th>TEST F</th>
    </tr>
</tfoot>
</table>

</body>
</html>

我看到网格中的所有数据都是正确的,但问题是,该表不会自动刷新所有10秒的表,其中包含来自MySQl表的新条目,我必须通过重新加载页面来刷新侧面通过浏览器。

有没有人可以帮助我作为Java的初学者,错误在哪里,哪个是正确的方法?非常坦克。

3 个答案:

答案 0 :(得分:2)

您必须先定义 otable 才能使用它:

为了澄清:这里是你应该使用的整个代码 - 不要两次调用DataTable - 这会给你一个关于重复初始化的错误。

$(document).ready(function() {
    var otable = $('#log').DataTable( {
    "bStateSave": true,
    "processing": true,
    "serverSide": true,
    "cache": false,
    "aaSortingFixed": [[4,'desc']],
    "aoColumns": [
        { "sClass": "left" },
        { "sClass": "center" },
        { "sClass": "right" },
        { "sClass": "center" },
        { "sClass": "center" },
        { "sClass": "center" }
    ],
    "ajax": "server_processing.php"
} );

setInterval( function () {
    otable.ajax.reload();
}, 10000 );
});

答案 1 :(得分:0)

将DataTable对象保存在变量中。

答案 2 :(得分:0)

  

你定义了两次你的桌子吗?你还有其他任何打电话给$(&#34;#log&#34;)。DataTable? - Birgit Martinelle 29分钟前

我不明白,你的意思,所以这是用过的代码:

    <script type="text/javascript" language="javascript" class="init">


    $(document).ready(function() {
    $('#log').DataTable( {
        "bStateSave": true,
        "processing": true,
        "serverSide": true,
        "cache": false,
        "aaSortingFixed": [[4,'desc']],
        "aoColumns": [
                { "sClass": "center" },
                { "sClass": "center" },
                { "sClass": "center" },
                { "sClass": "center" },
                { "sClass": "center" },
                { "sClass": "center" }
        ],
    "ajax": "server_processing.php"
} );


var otable = $('#log').DataTable( {
"bStateSave": true,
"processing": true,
"serverSide": true,
"cache": false,
"aaSortingFixed": [[4,'desc']],
"aoColumns": [
    { "sClass": "left" },
    { "sClass": "center" },
    { "sClass": "right" },
    { "sClass": "center" },
    { "sClass": "center" },
    { "sClass": "center" }
],
"ajax": "server_processing.php"
} );

setInterval( function () {
otable.ajax.reload();
}, 10000 );


} );