DataTables logo DataTables

via Ad Packs
Error fnDraw() is not defined
  • DenonthDenonth
    Posts: 76
    Hi all,

    I want to refresh data in the table every 30 sec, I was reading on this forum a lot. And I saw that for the server side processing I can call fnDraw() function every 30 sec and it will populate all new entries that are made to the table?

    Here is my code:

    $(document).ready(function() {
    		$('#jphit').dataTable( {
    			"bProcessing": true,
    			"bServerSide": true,
    			"sScrollY": "350px",
    			"bDeferRender": true,
    			
    		    "oTableTools": {
    			"aButtons": [
    				"copy",
    				"print",
    				{
    					"sExtends":    "collection",
    					"sButtonText": "Save",
    					"aButtons":    [ "csv", "xls", "pdf" ]
    				}
    			]
    		},
    			"sAjaxSource": "increment_table.php"
    		} );
    
    setInterval('fnDraw()',30000);
    
    

    I am getting an error that fnDraw() is not defined.
  • DenonthDenonth
    Posts: 76
    Any idea guys? How to make this table to fetch new data every 10 sec
  • allanallan
    Posts: 15,874
    fnDraw is not a global function - it is an API method of DataTables and you need to call it as such. See the fnDraw documentation for an example.

    Allan
  • DenonthDenonth
    Posts: 76
    Ok I have put it like this and it is not working:

    $(document).ready(function() {
           var oTable = $('#jphit').dataTable( {
                "bProcessing": true,
                "bServerSide": true,
                "sScrollY": "350px",
                "bDeferRender": true,
                 
                "oTableTools": {
                "aButtons": [
                    "copy",
                    "print",
                    {
                        "sExtends":    "collection",
                        "sButtonText": "Save",
                        "aButtons":    [ "csv", "xls", "pdf" ]
                    }
                ]
            },
                "sAjaxSource": "increment_table.php"
            } );
     
    setInterval(oTable.fnDraw(),3000);
    
    
  • allanallan
    Posts: 15,874
    setInterval calls the callback function with global scope, but oTable is locally defined, hence why it is not working.

    You need to add an anonymous function into setInterval which will call oTable.fnDraw()

    Allan
  • DenonthDenonth
    Posts: 76
    Okay I have defined it and it is working but know there is a problem with saving the state what I am looking at. It keeps getting me back to the first row and resets filters. How to save the filters and state where I was?
This discussion has been closed.
← All Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Support

Get useful and friendly help straight from the source.

In this Discussion