I load data into my table as follows:
$(document).ready(function () {
var variable= 'sometext'
$.ajax({
type: "POST",
url: "GetJSON.ashx",
cache: false,
data: { param: variable},
dataType: "json",
success: function (data) {
var html = '';
for (var key = 0, size = data.length; key < size; key++) {
html += '<tr><td>' + data[key].SessionID + '</td><td>'
+ data[key].val1+ '</td><td>'
+ data[key].val2+ '</td><td>'
+ data[key].val3+ '</td><td>'
+ data[key].val4+ '</td><td>'
+ data[key].val5+ '</td><td>'
+ data[key].Status + '</td></tr>'
}
$('#table).append(html);
otableName = $('#table).dataTable({
//"bDestroy": true,
"bPaginate": true,
"sPaginationType": "bootstrap",
"iDisplayLength": 20,
"sDom": "<'row-fluid'<'span6'T><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"oTableTools": {
"sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
"aButtons": [
"copy",
"print",
{
"sExtends": "collection",
"sButtonText": 'Save <span class="caret" />',
"aButtons": ["csv", "xls", "pdf"]
}
]
}
})
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
});
this works perfectly fine, and renders a nice looking table. Now, i've added a drop down list which contains years (2009-2013), which when an users selects, calls out to another ashx page and retrieves all the records for a given year. What i'm struggling to do, is to send this new data set to the existing table.
I've tried this:
$('#ArchiveYears').change(function () {
var year = $("#ArchiveYears option:selected").text();
var senderBIC = 'DIAGGB2LACTB'
// Need to filter out the table with the year
$.ajax({
type: "POST",
url: "GetJSONYearly.ashx",
cache: false,
data: { param: value, year: year },
dataType: "json",
success: function (data) {
var dataTable = $('#table').dataTable();
dataTable.fnClearTable(this);
for (var i = 0; i < data.length; i++) {
dataTable.oApi._fnAddData(oSettings, data[i]);
}
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
dataTable.fnDraw();
}
});
});
which complains about oSettings not being declared!
So, what's the best way to drop existing table content, and load it with new?
Ok, so following your suggestion I tried the following :
success: function (data) {
var dataTable = $('#tblMsgDateDetail').dataTable();
dataTable.fnClearTable();
dataTable.fnAddData(data);
which throws this error dialog

oddly though the table redraws and display the correct amount of records, just no data.