Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make column data as hyperlink (dataTable JQUERY)

I am trying to make a column as hyperlink with datatable but no success.

function successCallback(responseObj){

  $(document).ready(function() {
         $('#example').dataTable( {
        "data":responseObj ,
        "bDestroy": true,
        "deferRender": true ,
        "columns": [
                    { "data": "infomation" },
                    { "data": "weblink" },
                ]
  } );

  } );

}

I need the weblink to display the link and be an hyperlink in that column so users can click and be redirected to another page. I looked into render but with less information there on links, i can't succeed to do it.

I also looked into this example but it wasn't very helpful.

like image 381
Undisputed007 Avatar asked May 27 '15 17:05

Undisputed007


3 Answers

Use columns.render API method to dynamically produce content for a cell.

$('#example').dataTable({    "data": responseObj,    "columns": [       { "data": "information" },        {           "data": "weblink",          "render": function(data, type, row, meta){             if(type === 'display'){                 data = '<a href="' + data + '">' + data + '</a>';             }              return data;          }       }     ] }); 

See this example for code and demonstration.

like image 77
Gyrocode.com Avatar answered Sep 19 '22 15:09

Gyrocode.com


If you are looking to add link based on other column data then can use the below approach.

$('#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;          }       }     ] }); 

I have just changed the render function. data refers to only current column data, while row object refers to entire row of data. Hence we can use this to get any other data for that row.

like image 25
Adarsh Madrecha Avatar answered Sep 19 '22 15:09

Adarsh Madrecha


    $('#example').dataTable( {
  "columnDefs": [ {
    "targets": 0,
    "data": "download_link",
    "render": function ( data, type, full, meta ) {
      return '<a href="'+data+'">Download</a>';
    }
  } ]
} );

From the documentation. It is quite clear and straightforward to me, what is it specifically that you do do not understand? What errors do you see?

For a more complete example, see here

like image 35
ozz Avatar answered Sep 19 '22 15:09

ozz