I want to create links in record fields in DataTables from JSON data
you should use fnRowCallback
option see 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;
},
});
You could also use the mRender
function with 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>';
}
}
]
});
This is more explicit and likely more maintainable because you can specify how individual columns render at the column level, rather than selecting them through the DOM at the row level, which helps when you're adding columns later on.