Search code examples
javascriptphpjqueryjquery-jtable

jtable single column/field in a table refresh


I am using Jtable for booking events. In combination with PHP, MySQL. My question is, is there a way to just reload every 10 second single column. Precisely I have something like this:

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

I would like to have the reservations column reloaded every 10 seconds, so the user that is logged in can see the changes. Now I got it working with reloading the whole table, but this is not what I really need because every user can book only 9 events and I need to have checkboxes at the left side. After reloading the whole table my checkboxes are not working as expected. So is there a way to reload just one column? My code right now is:

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

Any help or suggestion would be appreciated.


Solution

  • I found a way around how to solve it:

    First create a new field in JS like this:

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

    Than create a function that will be run every 10 seconds and make an AJAX request:

    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);
    

    Let your JSON response look like this:

    {"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"}
    ]}
    

    And in the end in Actions.php make your query in 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);
    }