[英]how to append json array in datatable using render function
I want to append json array into one popover attribute called data-content. 我想将json数组附加到一个名为data-content的popover属性中。 I am using jQuery DataTables plugin to perform UI functionalities.
我正在使用jQuery DataTables插件执行UI功能。 Using
data
variable table is populated with the data. 使用
data
变量表填充数据。
How could I insert titleDescription
array from data
variable into the attribute name called as data-content
inside a
tag in js , check my fiddle and go to datatable function there inside columnDefs
there is render
function. 我怎么能插入
titleDescription
从数组data
变量成称为属性名称data-content
里面a
在JS代码,请我的小提琴,到数据表功能有内部columnDefs
有render
功能。 In that function I have return and append a
tag, there only I have to append titleDescription
array. 在该函数中,我具有return并附加
a
标签,仅需附加titleDescription
数组。
You can use the row attribute, and have the data in a non visible column. 您可以使用row属性,并将数据放在不可见的列中。 Check the example https://datatables.net/examples/advanced_init/column_render.html
查看示例https://datatables.net/examples/advanced_init/column_render.html
$(document).ready(function() {
$('#example').DataTable( {
"columnDefs": [
{
// The `data` parameter refers to the data for the cell (defined by the
// `data` option, which defaults to the column being worked with, in
// this case `data: 0`.
"render": function ( data, type, row ) {
return data +' ('+ row[3]+')';
},
"targets": 0
},
{ "visible": false, "targets": [ 3 ] }
]
} );
} );
That would do the trick. 那可以解决问题。
You just need to add another column at the last of each row then you can access it from row like this 您只需要在每行的最后添加另一列,然后就可以像这样从行中访问它
render : function(data, type, row) {
return '<span class="favlist-icon"></span><span class="immediate-attention-icon"> </span> <span class="docx-name-list-link"> <a class="apopoverdata" href="#" data-content="'+row[4]+'" rel="popover" >'+data+'</a></span>';
}
Here is working fiddle 这是工作提琴
https://jsfiddle.net/2cn4b8bz/4/ https://jsfiddle.net/2cn4b8bz/4/
I'm not sure what library you were using for your pop over so I used tooltipster seeing as you had jQuery already for the DataTable. 我不确定您要使用哪个库进行弹出操作,因此我使用了工具提示来查看数据表,因为您已经有了jQuery。 Your data didn't have everything you needed either so I've altered it a wee bit:
您的数据也没有您需要的所有内容,因此我做了一点改动:
$(function() {
$('#documentListing-data').DataTable({
data: json.documentAll.document,
pageLength: 10,
columns:[
{
"data": "documentTitle",
"title": "Name",
"render": (data, type, row) => '<a href="' + row.documentUrl + '" class="tooltip" title="' + row.titleDescription + '">' + data + '</a>',
},
{
"data": "category",
"title":"Category",
"render": (data, type, row) => data.map((k, v) => k.trim()).join(", "),
},
{
"data": "publishedDate",
"title":"Published Date"
},
{
"data": "lastUpdatedDate",
"title": "Last Updated Date"
},
],
"drawCallback": function(settings) {
$('.tooltip').tooltipster();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.