慕田峪4524236
2023-09-28 16:04:11
我正在使用數(shù)據(jù)表管道來生成表。我的表有動(dòng)態(tài)列意味著它沒有固定列。欄目列數(shù)隨月份的變化而變化。假設(shè)當(dāng)月該表有 4 列,但 11 月份有 32 列。當(dāng)我將月份更改為十一月時(shí),它給了我Cannot read property 'style' of undefined這個(gè)錯(cuò)誤。我的數(shù)據(jù)表初始化函數(shù): function monthlyAttendanceStatusDatatableInit(tableIdOrCss, url, columns, sortArr, pageLength, year, month) { console.log(columns); var param = { "responsive": false, // "columnDefs": [ // {responsivePriority: 1, targets: -1}, // {responsivePriority: 2, targets: 0} // ], "aLengthMenu": [[10, 20, 50, -1], [10, 20, 50, 'All']], "pageLength": pageLength || 10, "iDisplayLength": pageLength || 10, //"language": { search: "" }, "sPaginationType": "simple_numbers", // you can also give here 'simple','simple_numbers','full','full_numbers' "oLanguage": { "sSearch": "Search:", "sProcessing": "Loading..." }, "ajax": $.fn.dataTable.pipeline( { url: url, data: { 'month': month, 'year': year }, pages: 2 // number of pages to cache }), "processing": true, "serverSide": true, "searching": true, // "bPaginate": true, // "fnDrawCallback":function(){ // if(typeof callBack == 'function'){ // callBack(); // } // }, "destroy": true, "paging": true, "retrieve": false, "aoColumns": columns, "aaSorting": sortArr, //[[ 0, "asc" ],[ 1, "desc" ]] // Sort by first column descending // "scrollX": true, // "createdRow": function( row, data, dataIndex ) { // $(row).attr('id', 'employee-'+data.id); // } }; // $(tableIdOrCss).remove(); var table = $(tableIdOrCss).DataTable(param); return table; }
1 回答

千萬里不及你
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
正如您在 DataTable 文檔中關(guān)于destroy()方法所看到的,在列更改的情況下,您應(yīng)該在添加jQuery#empty()
新列之前銷毀數(shù)據(jù)表實(shí)例并通過調(diào)用刪除表元素的所有子節(jié)點(diǎn)。
var table = $('#myTable').DataTable();
?
$('#submit').on( 'click', function () {
? ? $.getJSON( 'newTable', null, function ( json ) {
? ? ? ? table.destroy();
? ? ? ? $('#myTable').empty(); // empty in case the columns change
?
? ? ? ? table = $('#myTable').DataTable( {
? ? ? ? ? ? columns: json.columns,
? ? ? ? ? ? data:? ? json.rows
? ? ? ? } );
? ? } );
} );
這是工作示例。
添加回答
舉報(bào)
0/150
提交
取消