Adding links to record field

Adding links to record field

bigsilkbigsilk Posts: 7Questions: 0Answers: 0
edited September 2010 in General
I'm using Datatables to create an archive of comics my client is creating, and would like the title of the comic to be a link to that comic.

Initialize:
[code]

$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "archive/archive.txt"
} );
} );

[/code]

HTML:
[code]



Author
Title
Episode
Date
Tags







[/code]

JSON:

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

Web page:
http://www.classicalmusicisboring.com/archive.html

Thanks.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    There are two options for links - you can either simply put the HTML for the link into your aaData array (any HTML will be put into the table 'as is'. Or you could use fnRender to render the data into the link HTML.

    Allan
  • bigsilkbigsilk Posts: 7Questions: 0Answers: 0
    edited September 2010
    Thanks for your reply.

    I don't suppose I understand. Might you show me an example from my data? I tried this:

    [code]["Bob",link,"Episode 1","9/30/2010","tag1,tag2,tag3"],[/code]

    Which, of course, didn't work.

    Further, while having read the bit about fnRender, I'm not quite sure how to use it.

    Adam
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    What about putting the link tag in as a string:

    [code]
    ["Bob",'link',"Episode 1","9/30/2010","tag1,tag2,tag3"],
    [/code]
    Allan
  • bigsilkbigsilk Posts: 7Questions: 0Answers: 0
    After entering this:

    [code]
    { "aaData": [
    ["Bob",'link',"Episode 1","9/30/2010","tag1,tag2,tag3"],
    ] }
    [/code]

    I got back an alert: DataTables warning: JSON data from server failed to load or be parsed. This is most likely to be caused by a JSON formatting error.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Oops yes - single quoted strings are strictly allow in JSON:

    [code]
    { "aaData": [
    ["Bob","link","Episode 1","9/30/2010","tag1,tag2,tag3"],
    ] }
    [/code]
    Allan
  • bigsilkbigsilk Posts: 7Questions: 0Answers: 0
    That did the trick. Thank you.
  • xlnerd.comxlnerd.com Posts: 4Questions: 0Answers: 0
    Hello, I am new to this Forum, but I was wondering if I could just jump in here since my question is along the same lines?
    I am pulling my data directly from my sql database using the default functions given in the server side processing php code. I am also trying to add a link just like bigsilk was. I see how he did it since he made his own array instead of grabbing his data from a database but I am lost on how to do this with pulled data from my db?

    I would apprecaite it if you could point me in the right direction or offer an example. Thank you very much!
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Are you using the PHP + MySQL script that comes with DataTables ( http://datatables.net/development/server-side/php_mysql )? If you have a look around line 159, you'll see I've got special code for the 'version' column. You can do the same for whatever column you want.

    Allan
  • xlnerd.comxlnerd.com Posts: 4Questions: 0Answers: 0
    Hey Allan,

    Thanks for the quick response. I apprecaite it. I will play around with it and see if I can figure out how to make them links with their id attached to each link. Thanks again man.
  • xlnerd.comxlnerd.com Posts: 4Questions: 0Answers: 0
    If anyone could help me out, I would truly apprecaite it. Right now I am trying to display a link to each report I have in my database, but the way I am currently calling it is only displaying every report_ID as the very last entry in the database.

    I guess my question is where I need to put the foreach or how I tell it to grab a new report_ID after each iteration.

    [code]
    //aaData is the array where the data from the database is pulled into
    $bll = new Bll();
    $arrNames = $bll -> getReports();
    foreach ($arrNames as $reportID){
    $id = $bll->getIdByName($reportID);
    $reportID = $bll->getReportIDbyId($id);
    }
    $sOutput .= '"aaData": [ ';
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {

    $sOutput .= "[";
    for ( $i=0; $i < count($aColumns); $i++ )
    {

    if ( $aColumns[$i] == "report_ID")
    {
    /* Special output formatting for 'report_id' Create the links*/
    $sOutput .= ($aRow[ $aColumns[$i] ] != "0") ?
    '"'.$reportID.'",' :
    '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
    }
    else if ( $aColumns[$i] != ' ' )
    {
    /* General output */
    $sOutput .= '"'.str_replace('"', '\"', $aRow[ $aColumns[$i] ]).'",';
    }
    }
    [/code]

    Thank you for any help.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I guess you need some method of converting the information known in the while loop (i.e. each result row) into a report ID. The way to do that will depend on your data structure - but some function call (or a stored array) will almost certainly be needed.

    Allan
  • xlnerd.comxlnerd.com Posts: 4Questions: 0Answers: 0
    Thanks Allan,

    Funny how you work on things for an hour or so then the next day you figure it out in 15 minutes.
    Thank you again, Have a great day.
This discussion has been closed.