繁体   English   中英

如何在 DataTable().row.add() 之后继续自定义 dataTable?

[英]How to continue custom dataTable after DataTable().row.add()?

当我执行 DataTable().row.add() 时,我已经初始化了一次 dataTable,但我仍然需要对该数据表执行一些额外的方法。 例如,我需要这样做 api 方法: $('#example').DataTable( { dom: 'Bfrtip', buttons: [ 'colvis' ] } ); 但是有一个警报DataTables 警告:table id=dataTable - 无法为我的数据表中的每一行重新初始化 DataTable ,这意味着如果我有 10 行,此警报将显示 10 次,尽管在我跳过所有这些警报之后,一切看起来那么很好,控制台中也没有错误这是我的代码:

在下面的一些评论之后,我更新了我的代码

            var myTable = $('#dataTable').DataTable({ dom: 'Bfrtip', buttons: ['colvis'] });
            var row = $('#dataTable tbody tr:first');
            myTable.row.add([
                '<img width="70px" height="100px" src="' + childData.poster + '">',
                '<p style="height:100px;overflow:auto;text-align:center">' + childData.title + '</p>',
                '<a style="text-align:center" href="javascript: void(0)" onclick="showModalListChapter(' + "'" + childKey + "'" + "," + "'" + childData.title + "'" + ')">' + totalChapters + '</a>',
                '<p style="height:100px;overflow:auto;text-align:center">' + childData.author + '</p>',
                '<p style="width:100px;height:100px;overflow:auto;text-align:center">' + childData.category + '</p>',
                '<p style="text-align:center">' + childData.state + '</p>',
                '<p style="width:350px;height:100px;overflow:auto;text-align:center">' + childData.description + '</p>',
                '<p style="text-align:center">' + childData.totalViews + '</p>',
                '<p style="text-align:center">' + childData.totalLikes + '</p>',
                '<button class="btn btn-danger" style="margin-bottom:20px" onclick="deleteManga(' + "'" + childKey + "'" + ')">xóa</button>' +
                '<button class="btn btn-warning" onclick="showModalUpdateManga(' + "'" + childKey + "'" + ')">sửa</button>',
                '<p style="text-align:center">' + childData.createdDate + '</p>',
                '<p style="text-align:center">' + childData.updatedDate + '</p>',
            ]).draw(false);

同样的问题仍然发生

最有可能(很难说 100%,因为您没有给我们您的代码段),您需要将数据表初始化为一个变量,然后在该变量中使用 object 来添加您的行。 所以:

$(document).ready(function() {
    var myTable = $('#example').DataTable( { dom: 'Bfrtip', buttons: [ 'colvis' ] } ); 
    //and then, under some condition, and likely inside a function:
     myTable.row.add(data);
} );

row.add()上的DataTables 文档有更多示例。

更新

这是一个小提琴演示,它显示了初始化数据表然后添加一行的基础知识: https://jsfiddle.net/zephyr_hex/enkmhb2t/13/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM