我正在创建一个dataTables表,用作生成漫画的网站的页面存档。在该档案页面上,我想让漫画的标题链接到该漫画的页面。
初始化:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "archive/archive.txt"
} );
} );
</script>
HTML:
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<thead>
<tr>
<th width="20%">Author</th>
<th width="25%">Title</th>
<th width="25%">Episode</th>
<th width="15%">Date</th>
<th width="15%">Tags</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
JSON数据:
{ "aaData": [
["Bob","Title One","Episode 1","9/30/2010","tag1,tag2,tag3"],
["Bob","Title One","Episode 2","10/2/2010","tag1,tag2,tag3"],
["Bob","Title One","Episode 3","10/4/2010","tag1,tag2,tag3"],
["Bob","Title Four","Episode 1","10/8/2010","tag1,tag2,tag3"],
["Bob","Title Five","Episode 1","10/11/2010","tag1,tag2,tag3"],
["Bob","Title Six","Episode 1","10/12/2010","tag1,tag2,tag3"],
["Kevin","Title Seven","Episode 1","10/15/2010","tag1,tag2,tag3"],
["Kevin","Title Eight","Episode 1","10/17/2010","tag1,tag2,tag3"],
["Kevin","Title Eight","Episode 2","10/20/2010","tag1,tag2,tag3"],
["Kevin","Title Ten","Episode 1","10/22/2010","tag1,tag2,tag3"],
["Kevin","Title Eleven","Episode 1","10/23/2010","tag1,tag2,tag3"],
["Kevin","Title Twelve","Episode 1","10/24/2010","tag1,tag2,tag3"]
] }
“Title One”或“Title Four”等,将是该漫画页面的链接。不可否认,我没有太多关于使用dataTables的chops的方式,所以如果你的解决方案可能是明确的,那将非常感激。
答案 0 :(得分:24)
您还可以将mRender
功能与aoColumnDefs
:
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "archive/archive.txt",
"aoColumnDefs": [
{
"aTargets": [ 2 ], // Column to target
"mRender": function ( data, type, full ) {
// 'full' is the row's data object, and 'data' is this column's data
// e.g. 'full[0]' is the comic id, and 'data' is the comic title
return '<a href="/comics/' + full[0] + '">' + data + '</a>';
}
}
]
});
这更加明确,可能更易于维护,因为您可以指定单个列在列级别的呈现方式,而不是通过行级别的DOM选择它们,这有助于您稍后添加列。
答案 1 :(得分:21)
您应该使用fnRowCallback
选项,请参阅documentation。
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "archive/archive.txt",
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
$('td:eq(2)', nRow).html('<a href="view.php?comic=' + aData[2] + '">' +
aData[2] + '</a>');
return nRow;
},
});
答案 2 :(得分:1)
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "archive/archive.txt"
"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
$('td:eq(2)', nRow).html('<a href="view.php?comic=' + aData[2] + '">' +
aData[2] + '</a>');
return nRow;
},
});
答案 3 :(得分:0)
下面是我在aaData对象数组中给定某个值时在列单元格中更改html文本所做的工作。这有效,但感觉很糟糕,因为html标记在javascript中如上所述。
var dataTableMeta = { "bProcessing": true,
"bServerSide": true,
"sAjaxSource": url
, "aoColumns": aoColumns
, "fnServerData": function (sSource, aoData, fnCallback) {
$.ajax({ "dataType": 'json', "type": "POST", "url": sSource, "data": aoData, "success": fnCallback,
'dataFilter': function (data, type) {
var jsObject = jQuery.parseJSON(data);
for (var i = 0; i < jsObject.aaData.length; i++) {
jsObject.aaData[i].CaseID = '<a href="" >' + jsObject.aaData[i].CaseID + '</a>';
}
var jsonString = JSON.stringify(jsObject);
return jsonString;
}
});
}
};
$('#caseSearchTable').dataTable(dataTableMeta);
答案 4 :(得分:0)
如果使用最新版本v1.10.16
,则可以使用render
function回调。
$('#example').dataTable({
"data": responseObj,
"columns": [
{ "data": "information" },
{
"data": "weblink",
"render": function(data, type, row, meta){
if(type === 'display'){
data = '<a href="' + row.myid + '">' + data + '</a>';
}
return data;
}
}
]
});
我刚刚更改了渲染功能。 data
仅指当前列数据,而row
对象指整行数据。因此,我们可以使用它来获取该行的任何其他数据。
如果您想根据当前列的值进行链接,可以使用
if(type === 'display'){
data = '<a href="' + data + '">' + data + '</a>';
}
答案 5 :(得分:0)
我无法获得任何答案来完成我想做的事情。
我想在datatable列中显示DisplayedColumn,但是单击时将ID与请求一起发送。我也不想显示ID列。
这是我实现这一目标的方法:
columns: [
{ "data": "ID", "visible" : false },
{ "data": "DisplayedColumn" },
...
columnDefs: [ {
targets: [1],
"render": function (data, type, row, meta) {
return '<a href="/Area/Controller/Action/' + row.ID + '">' + data + '</a>';
}
}
...
可悲的是,这很难找到对我有用的答案,我希望它能对某人有所帮助。