[英]Custom rendering when using server side processing
I was wondering if the rendering of the table after receiving an ajax response can be modified. 我想知道收到ajax响应后的表呈现是否可以修改。 This seems related to the
render
function described here: https://www.datatables.net/manual/orthogonal-data . 这似乎与此处描述的
render
功能有关: https : //www.datatables.net/manual/orthogonal-data 。
My server returns Data like 我的服务器返回的数据像
{
"name":
{
id: "123456",
value: "Tiger Nixon"
}
}
I want to add to each name cell the id of the name as data-attribute
or as id for the td
and want to add a .on( "click", handler )
for each cell. 我想向每个名称单元添加名称的ID作为
data-attribute
或td
ID,并为每个单元添加一个.on( "click", handler )
。
Is this possible? 这可能吗?
Thanks in advance 提前致谢
You can use DT_RowData
or DT_RowAttr
(requires DataTables 1.10.5 or higher) parameters in your returned data to assign attributes to <tr>
element which you can later retrieve in click handler, see Server-side processing chapter in the manual. 您可以在返回的数据中使用
DT_RowData
或DT_RowAttr
(需要DataTables 1.10.5或更高版本)参数来为<tr>
元素分配属性,这些元素以后可以在单击处理程序中检索,请参见手册中的服务器端处理章节。
Alternatively you can use render
method but it may not be as effective. 另外,您可以使用
render
方法,但可能效果不佳。 I assumed below that index of your name
column is 0
and that you want to set data-id
attribute. 我假设您的
name
列的索引下面是0
,并且您想要设置data-id
属性。
var table = $('#example').DataTable({
"columnDefs": [{
"data": "name.value",
"targets": 0,
"render": function ( data, type, full, meta ) {
if(type === 'display'){
$('#example').DataTable().cell(meta.row, meta.col).nodes().to$().attr('data-id', full['name']['id']);
}
return data;
}
}]
});
You can add click event handler using the code below: 您可以使用以下代码添加点击事件处理程序:
$(document).ready(function(){
var table = $('#example').DataTable({
// Define DataTables initialization options here
});
$('#example tbody').on('click', 'td', function(){
// Use table to access various API function
//
// For example:
// var data_cell = table.cell(this).data();
});
});
This is possible by using the columns.createdCell function. 这可以通过使用columns.createdCell函数来实现。
The answer of Gyrocode is correct for an old DataTables version. 对于旧的DataTables版本,Gyrocode的答案是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.