Updating table data every 5 seconds

Now I am at the design stage and wondered how I update the table every 5 seconds.

My table will display read-only data returned from my model.

Normally my view would have only <table></table> HTML and then a foreach loop to output the data.

However, since I want to update this entire table every 5 seconds, I am not sure how to implement it.

I know there is a javascript setinterval function, but I'm also not sure what to do at this point. Would there be something like this?

eg /

 function getdata() { $.getJSON("/mycontroller/mymethod"), function(data) { $.each(data, function(i, item) { var row = { item.ID, item.Date, item.Title }; $(#table).tableInsertRows(row); }); }); } setInterval( "getdata", 5000 ); 
+2
source share
1 answer

It might be easiest for your mymethod action mymethod display the view instead of returning JSON. Then you can simply set innerHTML for the div in the ajax response.

Otherwise, your approach will work, but you should obviously delete existing table rows first:

 $('#table').tableRemoveRows({from:0, length:???}); 

Edit

re-reading your question, it looks like you are asking more about setInterval than about creating a table. You need to re-register the callback, so something like this:

 function getdata() { $.getJSON("/mycontroller/mymethod"), function(data) { $.each(data, function(i, item) { var row = { item.ID, item.Date, item.Title }; $(#table).tableInsertRows(row); }); setInterval( getdata, 5000 ); }); } setInterval( getdata, 5000 ); 
+1
source

Source: https://habr.com/ru/post/893789/


All Articles