Reputation: 834
My boss asked me to make our datatable's automatically update with new data without having to reload the page.
From my research i need to do some AJAX scripting with our datatables and not sure witch one to go with.
I have a form on the same page that i use Ajax and jquery to post to the Database. That all works perfectly but we need the table to automatically update (add or remove rows) and not just from an event from the user but on a automated timer since there is data entered remotely thru an API.
Currently we have this type of setup: (cut down to the basics)
Controller:
def index
@data = Data.all
respond_to do |format|
format.html
end
end
View:
<table class="table" id="data_table" width="100%">
<thead>
<tr>
<th>
Id
</th>
<th>
Origin
</th>
<th>
Destination
</th>
</tr>
</thead>
<tbody>
<% if @data.nil? %>
<tr></tr>
<% else %>
<% @data.each do |s| %>
<tr>
<td>
<%= s.id %>
</td>
<td align="center">
<%= s.origin_cs unless s.origin_cs.nil? %>
</td>
<td align="center">
<%= s.dest_cs unless s.dest_cs.nil? %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
jQuery:
var post_load = $('#data_table').dataTable( {
paging: false,
scrollY: 200,
"bAutoWidth": true,
"bJQueryUI": true,
"dom":'TC<"clear"><"toolbar1">frtip',
"oTableTools": {
"sRowSelect": "single",
"aButtons": [
]
},
"columnDefs": [
{
"targets": 0,
"visible": false,
"searchable": false
},
{
"targets": [1,2],
"searchable": false,
"width": "250px"
}]
});
Upvotes: 0
Views: 732
Reputation: 111
How about a timer in javascript calling every n seconds to your ajax function for reloading the table? How do i get this javascript to run every second?
Upvotes: 1