datatables - jQuery DataTables - 使用 jQuery 对象替换/更新行。或者 fnUpdate

标签 datatables datatables-1.10

我使用以下代码成功地向数据表中添加了一行:

作品:

var serversTable = $('#datatable-table').dataTable().api();

var serverRowTemplate = Handlebars.compile($('#serverRowTemplate').html());

var $row = $(serverRowTemplate(data.results[r]));

serversTable.row.add($row).draw(false);

但是当我尝试使用类似的方法更新(替换)一行时,我没有运气。我也试过使用 fnUpdate,但我似乎无法找出通过 jQuery 对象基本上替换/更新现有行的正确方法。这是无法更新表格的代码:

不起作用:

var serversTable = $('#datatable-table').dataTable().api();

var oldRow = $('#dashboardTemplateContainer tr[data-tag="'+ data.results[r].tag + '"]'); // select the row i'm looking for

var serverRowTemplate = Handlebars.compile($('#serverRowTemplate').html()); //compiles the handlebars template. i need this template as i have a lot of conditionals etc. 

var $row = $(serverRowTemplate(data.results[r])); // turning the template into a jQuery object worked for the row.add() function in the above example.

serversTable.rows(oldRow).data($row).draw(false); // replace the old row with the new row. doesn't work.

有什么建议吗?

最佳答案

API 方法 rows().data() 只能用于检索数据。使用 row().data() 而是为一行设置数据。

但是,您将无法为 <TR> 使用 jQuery 对象节点更新行的数据。在这种情况下,最好的选择可能是删除带有 row().remove() 的行。如果你只有 <TR>,则重新添加它节点。

关于datatables - jQuery DataTables - 使用 jQuery 对象替换/更新行。或者 fnUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776781/

相关文章:

javascript - 如何在ajax响应后刷新数据表

jquery - 获取 DataTable 中的所有行,包括过滤掉的行

json - 数据表:标题中带点 (.) 的列显示不正确

javascript - DataTables 的自动填充扩展与输入元素不起作用

javascript - 使用父列作为子行的 DataTables 行详细信息

require.js 中的 jQuery 数据表错误 : undefined is not a function

codeigniter - 点火+数据表->排序依据

jquery - 如何在使用 jQuery 添加新行时刷新简单的数据表

javascript - 无法读取动态数据表的未定义属性 'aDataSort'

javascript - 自动宽度属性不适用于数据表