Adam Adam - 2 months ago 9x
JSON Question

I want to create links in record fields in DataTables from JSON data

I'm creating a dataTables table to use as an archive of pages for a site that produces a comic strip. On that archives page, I'd like to have the title of the comic be a link to the page of that comic strip.


<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "archive/archive.txt"
} );
} );



<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
<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>



JSON Data:

{ "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"]
] }

Where "Title One" or "Title Four" etc, would be a link to the page of that comic. Admittedly, I don't have much in the way of chops with dataTables, so if you might be explicit in your solution, that would be well appreciated.


you should use fnRowCallback option see documentation.

     "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;