Search code examples
javascriptbootstrap-table

Auto Refresh Table JSON


I'm use table JSon. But I have encode it from php to Json. So I just call file pin.php from tables. example my HTML tables:

HTML:

<table
    data-url="../../tables/pin.php"  
    data-row-style="rowStyle" 
    data-toggle="table" 
    data-show-refresh="true" 
    data-show-toggle="true" 
    data-show-columns="true" 
    data-search="true" 
    data-select-item-name="toolbar1" 
    data-pagination="true" 
    data-sort-name="name" 
    data-sort-order="desc">
    <thead>
        <tr>
            <th data-field="id_pin" data-sortable="true" data-align="center">ID PIN</th>
            <th data-field="no_meja" data-sortable="true" data-align="center">Nomor Meja</th>
            <th data-field="status" data-sortable="true" data-align="center">Status</th>
            <th data-field="action" data-sortable="true" data-align="center">Input Pemesanan</th>
        </tr>
    </thead>
        <tr>
        </tr>
</table>

Script table :

<script>
    $(function () {
        $('#hover, #striped, #condensed').click(function () {
            var classes = 'table';

            if ($('#hover').prop('checked')) {
            classes += ' table-hover';
            }
            if ($('#condensed').prop('checked')) {
            classes += ' table-condensed';
            }
        $('#table-style').bootstrapTable('destroy')
            .bootstrapTable({
            classes: classes,
            striped: $('#striped').prop('checked')
            });
        });
});

function rowStyle(row, index) {
    var classes = ['info', 'info', 'info', 'info', 'info'];

    if (index % 2 === 0 && index / 2 < classes.length) {
        return {
            classes: classes[index / 2]
        };
    }
return {};
}
</script>

Question: I want this table is auto refresh. please help me this one.


Solution

  • It looks like you're using Bootstrap Tables. According to the documentation, you can refresh the table, so just set a timer to do it periodically:

    (function(){
        function refreshTable() {$('#table-style').bootstrapTable('refresh', {silent: true});}
        setInterval(refreshTable, 5000);
    })()