我将数据加载到我的表中,如下所示:
$(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);
}
});
});
这工作得很好,并且呈现出漂亮的桌子。现在,我添加了一个包含年份(2009-2013)的下拉列表,当用户选择该列表时,会调用另一个 ashx 页面并检索给定年份的所有记录。我正在努力做的是将这个新数据集发送到现有表。
我试过这个:
$('#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();
}
});
});
抱怨 oSettings 没有被声明!
那么,删除现有表格内容并使用新内容加载的最佳方法是什么?
好的,所以按照您的建议,我尝试了以下方法:
success: function (data) {
var dataTable = $('#tblMsgDateDetail').dataTable();
dataTable.fnClearTable();
dataTable.fnAddData(data);
引发此错误对话框
奇怪的是,虽然表格重绘并显示正确数量的记录,但没有数据。