I am trying to implement functionality whereby clicking a button on the screen will cause my jQuery dataTable to refresh (as the server-side data source may have changed since the dataTable was created).
Here’s what I have:
JavaScript
x
6
1
$(document).ready(function() {
2
$("#my-button").click(function() {
3
$("#my-datatable").dataTable().fnReloadAjax();
4
});
5
});
6
But when I run this, it does nothing. What’s the proper way to refresh the dataTable when the button is clicked?
Advertisement
Answer
You can try the following:
JavaScript
1
46
46
1
function InitOverviewDataTable() {
2
oOverviewTable = $('#HelpdeskOverview').dataTable({
3
"bPaginate": true,
4
"bJQueryUI": true, // ThemeRoller-stöd
5
"bLengthChange": false,
6
"bFilter": false,
7
"bSort": false,
8
"bInfo": true,
9
"bAutoWidth": true,
10
"bProcessing": true,
11
"iDisplayLength": 10,
12
"sAjaxSource": '/Helpdesk/ActiveCases/noacceptancetest'
13
});
14
}
15
16
function RefreshTable(tableId, urlData) {
17
$.getJSON(urlData, null, function(json) {
18
table = $(tableId).dataTable();
19
oSettings = table.fnSettings();
20
21
table.fnClearTable(this);
22
23
for (var i = 0; i < json.aaData.length; i++) {
24
table.oApi._fnAddData(oSettings, json.aaData[i]);
25
}
26
27
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
28
table.fnDraw();
29
});
30
}
31
// Edited by Prasad
32
function AutoReload() {
33
RefreshTable('#HelpdeskOverview', '/Helpdesk/ActiveCases/noacceptancetest');
34
35
setTimeout(function() {
36
AutoReload();
37
}, 30000);
38
}
39
40
$(document).ready(function() {
41
InitOverviewDataTable();
42
setTimeout(function() {
43
AutoReload();
44
}, 30000);
45
});
46