admin管理员组文章数量:1417682
I would like to return a jQuery object inside of a render
function. Since that doesn't seem possible, the code below is as close as I got.
Is this an acceptable solution? When is type type
called? I can't seem to find any info about it in the docs.
var dataSet = [
['Trident','Internet Explorer 4.0','Win 95+','4','X'],
['Trident','Internet Explorer 5.0','Win 95+','5','C'],
['Trident','Internet Explorer 5.5','Win 95+','5.5','A'],
['Trident','Internet Explorer 6','Win 98+','6','A'],
['Trident','Internet Explorer 7','Win XP SP2+','7','A'],
['Trident','AOL browser (AOL desktop)','Win XP','6','A']
];
$('#example').dataTable( {
columns: [
{
"title": "Engine",
"className": "foo",
"render": function ( data, type, row, meta ) {
var $td = $('#example').DataTable().cell(meta.row, meta.col).nodes().to$();
if(type === 'type'){
var $a = $('<a href="#"/>').data({ data: row }).text('foo');
$td.html($a);
}
return data;
},
},
{ "title": "Browser" },
{ "title": "Platform" },
{ "title": "Version", "className": "center" },
{ "title": "Grade", "className": "center" }
],
data: dataSet
});
/
I would like to return a jQuery object inside of a render
function. Since that doesn't seem possible, the code below is as close as I got.
Is this an acceptable solution? When is type type
called? I can't seem to find any info about it in the docs.
var dataSet = [
['Trident','Internet Explorer 4.0','Win 95+','4','X'],
['Trident','Internet Explorer 5.0','Win 95+','5','C'],
['Trident','Internet Explorer 5.5','Win 95+','5.5','A'],
['Trident','Internet Explorer 6','Win 98+','6','A'],
['Trident','Internet Explorer 7','Win XP SP2+','7','A'],
['Trident','AOL browser (AOL desktop)','Win XP','6','A']
];
$('#example').dataTable( {
columns: [
{
"title": "Engine",
"className": "foo",
"render": function ( data, type, row, meta ) {
var $td = $('#example').DataTable().cell(meta.row, meta.col).nodes().to$();
if(type === 'type'){
var $a = $('<a href="#"/>').data({ data: row }).text('foo');
$td.html($a);
}
return data;
},
},
{ "title": "Browser" },
{ "title": "Platform" },
{ "title": "Version", "className": "center" },
{ "title": "Grade", "className": "center" }
],
data: dataSet
});
http://jsfiddle/y3fnvzad/7/
Share Improve this question asked Jun 9, 2015 at 6:30 JohanJohan 35.2k62 gold badges189 silver badges306 bronze badges1 Answer
Reset to default 4When function is specified for render option, DataTables will call render function multiple times for the different data types that it needs - sorting, filtering and display.
Also, per documentation:
The return value from the function is what will be used for the data requested.
So you need to return a string (not jQuery object) that will contain data for type of operation requested.
If you would like to construct a link using jQuery, you need to return HTML string when type
equals to display
, see the code excerpt below:
"render": function ( data, type, row, meta ) {
if(type === 'display'){
return $('<a href="#"/>')
.text('foo')
.wrap('<div></div>')
.parent()
.html();
} else {
return data;
}
},
UPDATE:
It doesn't makes sense to assign data to the element using jQuery data()
since we're returning HTML string not jQuery object. In the example below I have demonstrated how you can access row data, when link is clicked.
$('#example tbody').on('click', 'a', function(){
// Get row data
console.log($('#example').DataTable().row($(this).closest('tr')).data());
});
See this JSFiddle for demonstration.
本文标签: javascriptReturn jQuery object from DataTables render functionStack Overflow
版权声明:本文标题:javascript - Return jQuery object from DataTables render function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745270041a2650832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论