首页 > 解决方案 > Append Row in createRow Event

问题描述

With jquery Datatable, I am trying to add new row in createdRow Event but it not adding.

Anything wrong here?

$("test").DataTable({
    "createdRow": function (row, data, dataIndex) {

        $(row).after("<tr><td>test</td></tr>");
    }
});

标签: datatables

解决方案


我可能会假设当节点准备好但未插入到 DOMcreatedRow时调用回调,因此实际上并不知道 this 所指的位置。row.after()

当我试图用一些东西来欺骗 DataTable 时,比如:

createdRow: row => {
    let newRowTemplate = document.createElement('template');
    newRowTemplate.innerHTML = `${row.innerHTML}<tr><td>test</td></tr>`;
    row = newRowTemplate.content.children;
}

我也没有成功,因为 DataTable 可能会firstChild从 HTMLCollection 中过滤掉,所以我看不到<tr>这个回调抛出 2x 节点的方式。

我可能想到的解决方法是使用row().child(),它会给你你期望的输出:

createdRow: (row, data, dataIndex) => {
    $('#test').DataTable().row(dataIndex).child('<tr><td>test</td></tr>').show();
}

推荐阅读