How to add hyperlink to each row in Datatable?

How to add hyperlink to each row in Datatable?

jerry98225jerry98225 Posts: 10Questions: 0Answers: 0
edited January 2012 in General
I already searched the forum and found some similar posts, but I still can't figure out how to do this. Hope someone can help me out.

I am trying to add links for all the rows that generate data from database in Datatable.I tried the code below but it won't work. Datatable just ignore the tag i created for . Anyone can help? Thanks a lot.

below is the view file for my application



<?php foreach ($result as $row):?>



//the link to the search controller to get detailed information of each row
<?= $row->job_number; ?>
<?= $row->city; ?>
<?= (empty($row->street_number))? 'N/A':$row->street_number;?>
<?= $row->street;?>
<?= $row->map;?>


<?php endforeach; ?>

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Looks like it should work to me. Can you link to the page that shows the problem please?

    Allan
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited January 2012
    Do you have PHP short tags enabled?
    Alternatively try <?php echo $row->job_number;?> instead of <?= $row->job_number;?>.
  • jerry98225jerry98225 Posts: 10Questions: 0Answers: 0
    Thanks guys. I already got it. The problem was on another portion of the code. :D
  • dafitoffdafitoff Posts: 5Questions: 0Answers: 0
    edited June 2012
    Hi all! I'm new to this fantastic plugin. Already configured my table as a simple server side processing model (PHP).

    What i'm trying to do now is just add a column with a hyperlink containing data from let say first column. I have got 3 columns: invoiceNo, data, time and I would like to add the fourth one containing the hyperlink like a . What is the simplest way to do this?

    That's my code for the table:

    [code]
    $(document).ready(function() {
    $('#tab1').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "iDisplayLength": 25,
    "sAjaxSource": "?q=table/1"
    } );
    } );
    [/code]

    [code]
    /*
    * Script: DataTables server-side script for PHP and MySQL
    * Copyright: 2010 - Allan Jardine
    * License: GPL v2 or BSD (3-point)
    */

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * Easy set variables
    */

    /* Array of database columns which should be read and sent back to DataTables. Use a space where
    * you want to insert a non-database field (for example a counter or static image)
    */
    $aColumns = array( 'invoiceNo', 'date', 'time' );

    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "id";

    /* DB table to use */

    $sTable = "invoices";

    /* Database connection information */
    $gaSql['user'] = "***";
    $gaSql['password'] = "***";
    $gaSql['db'] = "db1";
    $gaSql['server'] = "localhost";


    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is
    * no need to edit below this line
    */

    /*
    * MySQL connection
    */
    $gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
    die( 'Could not open connection to server' );

    mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
    die( 'Could not select database '. $gaSql['db'] );


    /*
    * Paging
    */
    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
    $sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
    mysql_real_escape_string( $_GET['iDisplayLength'] );
    }


    /*
    * Ordering
    */
    if ( isset( $_GET['iSortCol_0'] ) )
    {
    $sOrder = "ORDER BY ";
    for ( $i=0 ; $i $iFilteredTotal,
    "aaData" => array()
    );

    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
    $row = array();
    for ( $i=0 ; $i
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Two options:

    1. Use fnRender on the client-side
    2. Add a condition where my " /* Special output formatting for 'version' column */" comment is to add link formatting.

    Allan
  • dafitoffdafitoff Posts: 5Questions: 0Answers: 0
    Thanks Allan!

    Ok, so I added this to the initialisation part:

    [code]
    "aoColumnDefs": [
    {
    "fnRender": function ( oObj ) {
    return 'SomeLink';
    },
    "aTargets": [ 3 ]
    }
    ]
    [/code]

    becouse i want to put the link into the fourth column. I also added a column to the html table:

    [code]



    invoiceNo
    date
    time
    link






    Ładowanie danych z serwera...




    invoiceNo
    date
    time
    link




    [/code]

    and i got the error:
    [quote]
    DataTables warning (table id = 'example'): Requested unknown parameter '3' from the data source for row 0
    [/quote]
    it works but displaying this error. My server side script is sending just 3 columns.. the fourth one should be build using the data from the first one.
    WHAT should I do to avoid this kind of error?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    My guess is that you aren't passing in 4+ parameters in the array (or alternatively if you are using mDataProp then you haven't defined a value). This is an important point, because fnRender is given two values, the data object you currently have referenced, and a second parameter, the value of the cell. So DataTables is trying to get the value, but it hasn't been given one.

    The easiest way to deal with this is to give it a default value with sDefaultContent . Just give it an empty string.

    Allan
  • dafitoffdafitoff Posts: 5Questions: 0Answers: 0
    Allan, thanks a lot! Default Content solved the problem!
    regards,
    david.
This discussion has been closed.