如何使用 jQuery DataTables 在单元格中显示超链接 [英] How to display a hyperlink in a cell with jQuery DataTables
问题描述
我将一些 Ajax 数据读入了 jQuery DataTables.问题是我需要将第一列中的数据制作成超链接.如
.5555
I have some Ajax data read into jQuery DataTables. Problem is I need to make the data in the first column into a hyperlink. As in <td><a href = "5555.html">5555</a></td>
.
我的 JSON 数据:
My JSON data:
{
"data": [
["5555","07/17/2010","RCC Saturday Open","E10","Harris, Fred","1900","Nikolayev, Igor (FM)","2367","1-0"],
["5554","07/17/2010","RCC Saturday Open","B01","Nikolayev, Igor (FM)","2367","Motroni, Richard","1728","1-0"]
]
}
JavaScript:
JavaScript:
$(document).ready(function() {
$('#cccr').DataTable( {
"render": function ( data, type, row ) {
return '<a href="basic.php?game=' + data + '></a>'; //doesn't work
},
"ajax": 'games.json',
"deferRender": true
} );
} );
我对 JavaScript 不太了解.在 datatables.net 网站上搜索了几个小时后,我无法弄清楚.
I'm not too knowledgeable about JavaScript. I was unable to figure it out after hours of googling the datatables.net website.
有人可以帮忙吗?
推荐答案
CAUSE
Option render
应该是 columns
或 columnDefs
的子属性.
CAUSE
Option render
should be sub-property of either columns
or columnDefs
.
使用 columnDefs.render
选项显示超链接动态一个单元格.
Use columnDefs.render
option to display hyperlink in a cell dynamically.
例如:
var table = $('#cccr').DataTable({
/* ... skipepd other options ... */
columnDefs: [
{
targets: 0,
render: function ( data, type, row, meta ) {
if(type === 'display'){
data = '<a href="basic.php?game=' + encodeURIComponent(data) + '">' + data + '</a>';
}
return data;
}
}
]
});
演示
有关代码和演示,请参阅此 jsFiddle.
这篇关于如何使用 jQuery DataTables 在单元格中显示超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!