Chỉ cần thêm tham số này vào tuyên bố của bạn:
"bDestroy" : true,
Sau đó, bất cứ khi nào bạn muốn "tạo lại" tab le nó sẽ không thả một lỗi
P.S. Bạn có thể tạo một hàm để lấy các tham số bắt buộc và sau đó khởi tạo lại bảng với các tùy chọn khác nhau khi chạy.
Ví dụ tôi sử dụng một trong các chương trình của tôi, nếu nó giúp bạn có thể thích ứng với nhu cầu của bạn
initDataTable($('#tbl1'), 2, 'asc', false, false, 25, false, false, false, 'landscape', rptHdr); /* Initialize Table */
/*---------------------- Datatable initialization --------------------------- */
/*
* @$table Table id which be initialized
* @sortCol Column number that will be initially sortered
* @sorOrder Ascendant (asc) or Descendant (desc)
* @enFilter Boolean value to enable or not the filter option
* @enPaginate Boolean value to enable or not the filter option
* @dplyLength Number of records contained per page when pagination is enabled
* @enInfo Boolean value to show or not the records info
* @autoWidth Boolean value to enable or not table autowidth
* @enTblTools Boolean value to enable or not the table tools addin
* @pdfOrientation Page orientation (landscape, portrait) for pdf documents (required enTblTools == enabled)
* @fileName Output file naming (required enTblTools == enabled)
/*------------------------------------------------------------------------------*/
function initDataTable($table, sortCol, sortOrder, enFilter, enPaginate, dplyLength, enInfo, autoWidth, enTblTools, pdfOrientation, fileName) {
var dom = (enTblTools) ? 'T' : '';
var oTable = $table.dataTable({
"order": [
[sortCol, sortOrder]
],
"bDestroy": true,
"bProcessing": true,
"dom": dom,
"bFilter": enFilter,
"bSort": true,
"bSortClasses": true,
"bPaginate": enPaginate,
"sPaginationType": "full_numbers",
"iDisplayLength": dplyLength,
"bInfo": enInfo,
"bAutoWidth": autoWidth,
"tableTools": {
"aButtons": [{
"sExtends": "copy",
"sButtonText": "Copy",
"bHeader": true,
"bFooter": true,
"fnComplete": function(nButton, oConfig, oFlash, sFlash) {
$().shownotify('showNotify', {
text: 'Table copied to clipboard (no formatting)',
sticky: false,
position: 'middle-center',
type: 'success',
closeText: ''
});
}
}, {
"sExtends": "csv",
"sButtonText": "Excel (CSV)",
"sToolTip": "Save as CSV file (no formatting)",
"bHeader": true,
"bFooter": true,
"sTitle": fileName,
"sFileName": fileName + ".csv",
"fnComplete": function(nButton, oConfig, oFlash, sFlash) {
$().shownotify('showNotify', {
text: 'CSV file saved in selected location.',
sticky: false,
position: 'middle-center',
type: 'success',
closeText: ''
});
}
}, {
"sExtends": "pdf",
"sPdfOrientation": pdfOrientation,
"bFooter": true,
"sTitle": fileName,
"sFileName": fileName + ".pdf",
"fnComplete": function(nButton, oConfig, oFlash, sFlash) {
$().shownotify('showNotify', {
text: 'PDF file saved in selected location.',
sticky: false,
position: 'middle-center',
type: 'success',
closeText: ''
});
}
},
{
"sExtends": "Other",
"bShowAll": true,
"sMessage": fileName,
"sInfo": "Please press escape when done"
}
]
}
/*"fnDrawCallback": function(oSettings) {alert('DataTables has redrawn the table');}*/
});
/* If is IE then avoid setting the sticky headers */
if (!navigator.userAgent.match(/msie/i) && enPaginate == false) {
new $.fn.dataTable.FixedHeader(oTable);
}
return oTable
}
Ivan.
Hiện chúng tôi mã của bạn, nhưng bạn có thể sử dụng 'if (oTable = null!) OTable .fnDestroy(); ' – sdespont
Đã thêm mã của tôi vào câu hỏi. Bạn có thể giải thích làm thế nào để tiêu diệt? – LittleLebowski
Tại sao bạn cần tạo lại dữ liệu của mình? Bạn có thể làm mới cái hiện có: 'oTable.fnDraw (false);' – sdespont