refresh table every 1 second

refresh table every 1 second

itsbucketitsbucket Posts: 2Questions: 1Answers: 0
edited April 2018 in Free community support

I'm trying to refresh a data table I have every 2 seconds.

my code :

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

what i tried :

setInterval(refreshTable , 1000 );});

and I tried ajax.reload...

I get this error :

Uncaught SyntaxError: Unexpected identifier

I have an ajax code to add values to the DB and showing them in the data table .. I want to refresh the table every 2 seconds so when the user adds something it appears on the table immediately.

EDIT: Updated post to use Markdown formatting

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Uncaught SyntaxError: Unexpected identifier

    This is simply a syntax error in your JS code. Not sure about the setInterval code but you do have an error in this line:

    var user = '<?php echo $_SESSION['username']?>';
    

    Guessing username is a variable the line should look like this:

    var user = '<?php echo $_SESSION[' + username + ']?>';
    

    If you are still having a problem maybe you can post a test case with your code.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • itsbucketitsbucket Posts: 2Questions: 1Answers: 0

    @kthorngren Please check
    Live data

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769
    edited April 2018

    I added a $ to this line:
    var datatables = $('.js-dataTable-full-pagination-std').DataTable({

    Now I get this error:

    DataTables warning: table id=example - Ajax error. For more information about this error, please see http://datatables.net/tn/7

    In the test case you didn't init the Datatable with ajax which I believe is the cause of the above error when the api ajax.reload() is executed.

    The updated example is here:
    http://live.datatables.net/gowogeke/2/edit

    Kevin

This discussion has been closed.