表刷新中的jtable单列/字段

时间:2014-05-07 07:34:34

标签: javascript php jquery jquery-jtable

我正在使用Jtable预订活动。结合PHP,MySQL。我的问题是,有没有办法只重新加载每10秒单列。我确实有类似的东西:

Checkbox  ID  Event Reservations
    +     4   ev1   22
    -     5   ev2   19

我希望每隔10秒重新加载一次保留列,因此登录的用户可以看到更改。现在我让它重新加载整个表,但这不是我真正需要的,因为每个用户只能预订9个事件,我需要在左侧有复选框。重新加载整个表后,我的复选框没有按预期工作。那么有没有办法只重新加载一列?我现在的代码是:

window.setInterval(function(){      
    $('#Events').jtable('reload');                                          
}, 10000);

任何帮助或建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法:

首先在JS中创建一个新字段,如下所示:

test: {
        title: 'test',
        display: function (data) {
        var $div = $('<div id="test"">'+data.record.id+'</div>');
        return $div;
        }
},

创建一个每10秒运行一次的函数并发出一个AJAX请求:

function UpdateRes(){

            $.ajax({
                url: 'Actions.php?action=update',
                type: 'post',
                data: '&kiu='+$kiu,
            }).success(function(data) {
                var jsondata = JSON.parse(data);
                $.each(jsondata.Records, function(i, item) {
                    $('.jtable tr.jtable-data-row').each(function(){
                        if($(this).attr('data-record-key')==item.id){
                            $(this).find('div').html( item.reservations );
                        }
                    })

                });

            }); 
        }

window.setInterval(function(){      
    UpdateRes();
}, 10000);

让您的JSON响应如下所示:

{"Result":"OK",
"Records":
[
{"0":"111","id":"111","1":"20","reservations":"20"},
{"0":"127","id":"127","1":"20","reservations":"20"},
{"0":"133","id":"133","1":"20","reservations":"20"},
{"0":"134","id":"134","1":"20","reservations":"20"},
{"0":"135","id":"135","1":"20","reservations":"20"},
{"0":"326","id":"326","1":"20","reservations":"20"}
]}

最后在Actions.php中使用try catch:

进行查询
else if($_GET["action"] == "update")
{

    //Get records from database
    $result8 = mysqli_query($con,
   "SELECT l.id,(l.max-l.reserviert) as reservations 
   FROM td_res l WHERE 
   l.kiu='" . mysqli_real_escape_string($con,$_POST["kiu"]) . "';");

    //Add all records to an array
    $rows8 = array();
    while($row8 = mysqli_fetch_array($result8))
    {
        $rows8[] = $row8;
    }

    //Return result to jTable
    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    $jTableResult['Records'] = $rows8;
    print json_encode($jTableResult);
}