Reload/refresh data using table.ajax.reload()
Reload/refresh data using table.ajax.reload()
I am struggling on getting the table.ajax.reload to work. I am sure I am missing some parameters. I tried adding the ajax portion to the DataTable section but can't seem to get it to work. Please assist. Below is the code I have.
<script type="text/javascript" class="init">
// The number of milliseconds (ms) when we should update our currency
// charts = one minute
let UPDATE = 60 * 1000;
$(document).ready(function() {
$.ajax({
url: "https://api.coinmarketcap.com/v1/ticker/?limit=0",
type: "Get",
data: {},
success: function (data) {
table = $('#coinTable').DataTable( {
colReorder: {
realtime: true
},
data: data,
"aoColumns" : [
{data : 'price_usd'},
{data : 'percent_change_1h'},
{data : 'percent_change_24h'},
{data : 'percent_change_7d'},
{data : 'market_cap_usd'}
]
} );
}
} );
} );
setInterval( function () {
table.ajax.reload(null, false);
}, UPDATE );
</script>
This question has an accepted answers - jump to answer
Answers
You need to use the Datatables
ajax
for the ajax request. DataTables has no ajax configuration information since you are using jQuery ajax directly.Kevin
Could you try this?
This should reload the data every time you select a record.
If this works you could try to add a function which you call from
the body element.
I did the following as well and that didn’t work.
$(document).ready(function() {
$.ajax({
url: "https://api.coinmarketcap.com/v1/ticker/?limit=0",
type: "Get",
data: {},
success: function (data) {
table = $('#coinTable').DataTable( {
ajax: {
url: same as above,
type: “Get” },
colReorder: {
realtime: true
},
data: data,
"aoColumns" : [
{data : 'price_usd'},
{data : 'percent_change_1h'},
{data : 'percent_change_24h'},
{data : 'percent_change_7d'},
{data : 'market_cap_usd'}
]
} );
}
} );
} );
Thanks for all the help
You need to remove the jQuery ajax() function and just use Datatables. I copied and fixed your code here:
http://live.datatables.net/sidesiwo/1/edit
Looks like there are a couple rows with empty data (the first two displayed) and you may want to add more columns for the name, etc.
Kevin
Kindly help me the issue that i am facing relating to the above query. I am new to DataTables. I am loading table data from my jsp. I need to reload updated data in to my data table when I click my button. I have been struggling. $('#manTable').DataTable().ajax.reload(); is not working. Is there any way for me to reload data. In my jsp I am actually iterating the data which is available in nested objects. After clicking any button I am actually updating the Object that is required to load DataTable. But it is not reloading the table with new data. Any help is highly appreciated.Here is my code :
$(document).ready(function() {
// }
}
else
{
// Nothing to do here
}
} );
// }
}
else
{
// Nothing to do here
}
} );
});
function listManagerUsers(id){
console.log("inside listManagerUsers function "+id);
var jsonracfData = {"managerId":id};
$.ajax({
type: "POST",
url: context+"listManagerUsers",
contentType: "application/json",
dataType: "json",
timeout : 100000,
data: JSON.stringify(jsonracfData),
success: function(responseText){
if(responseText.code==0){
console.log("inside success function list : "+responseText.message);
}else{
console.log("failed from server "+responseText.message);
alert(responseText.message);
}
},
error: function(e){
console.log("inside ERROR function")
alert("ERROR: ", e);
}
});
}
As explained above
ajax.reload()
requires that you use theajax
within Datatables. However in your case usingajax
is probably not what you want to do. Replace theajax.reload()
withclear()
followed byrows.add()
anddraw()
to clear the table then add all the rows and draw the updated table.Kevin
Dear Kevin
Thanks for the reply.
Could you please post a sample code snippet?
Karthik
Here is a running example.
http://live.datatables.net/hopepicu/1/edit
It starts with a Datatable of one row of data. Click the button to request the new data set via AJAX and update the Datatable.
After reloading the new data the original row is not in the table.
Kevin
If you need a working test case showing this, priority support is available.
Allan
Thanks a lot Kevin. I need to format my json object. Thanks for making me understand of this.
Dear Allan,Thanks a lot for the tremendous support.
Gracias por tu ejemplo, me sirvio esto:
var table = $('#tabla').DataTable();
table.clear();
table.rows.add(data.data).draw();
thanks you men
This code is working
table.clear();
table.ajax.reload();
table.draw();
Hi, this would be for the table in mode server side?
thanks you, @Bajaranglal
use this code work,
table.clear();
table.ajax.reload();
table.draw();
https://datatables.net/forums/discussion/comment/169410/#Comment_169410