5

I am using datatables as my grid on a webapp. The problem is, the user has to refresh the page always to get the current data from the database,is there a way this can be done automatically? because several apps write to the same table and the webapp is just for monitoring,but it beats the purpose if the user has to refresh the page to get the current data. here is my initialization code:

  $('.{{datatable['class']}}').dataTable( {
                "sDom": 'T<"clear">lfrtip',
                "oTableTools": {
                    "sSwfPath": "includes/swf/copy_csv_xls_pdf.swf",                
                    "aButtons": [
                         { 
                             "sExtends":"copy",
                             "mColumns":[{{datatable['flds']}}]
                         },
                         {   
                             "sExtends":"csv",
                             "mColumns":[{{datatable['flds']}}]
                         },

                         {
                             "sExtends":"xls",
                             "mColumns":[{{datatable['flds']}}]
                         },                         
                         {
                            "sExtends": "pdf",
                            "mColumns":[{{datatable['flds']}}],
                            "sPdfOrientation": "landscape",
                            "sPdfMessage": "{{datatable['title']}}"
                         }
                    ]
                    },        
                "bProcessing": true,
                "bServerSide": true,
                "sAjaxSource": "{{datatable['source']}}",
                "aoColumns": [          
                        {% for column in 0..datatable['columns']-2 %}
                        null,
                        {% endfor %}
                        null
                ]

        });

is there a way the list can be updating itself each time anything(UPDATE/INSERT/DELETE) happens to the datasource? i have implemented a loop as Danny suggested,

var int=self.setInterval(function(){oTable.fnDraw();},1000);

but the problem is that the list is always a funny state,see the attatched imageenter image description here

indago
  • 2,041
  • 3
  • 29
  • 48

1 Answers1

2

You could have an ajax request that happens on a loop by an interval timer that checks to see if there is new data since the page last received anything. You would need to figure out what you could use to determine that like number of records or a dedicated record that holds a datetime of the last update/add/delete and if the datetime is newer then when the data was last grabbed update it again etc. On the page you would record/update the datetime of the last data update in a javascript var to use on all future checks.

Danny
  • 1,185
  • 2
  • 12
  • 33
  • can you give me an example of this loop? second i cannot use record count because most of the changes are updates and updates dont change record counts. – indago Apr 10 '13 at 08:03
  • @indago Ya maybe create a new table or file just to store when the last insert/update/delete was done to use as the check. – Danny Apr 10 '13 at 08:06
  • http://www.w3schools.com/jsref/met_win_setinterval.asp Just remember that if you set a really low interval it might update faster but if you have a lot of users on it will create a lot of http requests, maybe too many so you need to balance out the interval to what your server can handle based on user traffic and server stats ect. – Danny Apr 10 '13 at 08:07
  • Is there a way of accomplishing this without the loop?because there might be more than 100 users on the same list at same time as u said the http requests might be crazy! – indago Apr 10 '13 at 08:26
  • I don't think so, the web is still "stateless" so you need to communicate back and forth constantly to break the stateless environment which means lots of requests. Try setting the loop interval at the longest reasonable amount of time you would expect someone to wait before manually refreshing the page. You can always play with the interval numbers and monitor your server load to get a good balance. – Danny Apr 10 '13 at 08:29
  • i have implemented your idea, its working fine but it has created a new problem, the list is always in a funny state and its not well readable. Look at my edit on the question. – indago Apr 10 '13 at 09:49