0

In a jquery data table, if table header contains a column with name "URL", give hyperlink for that table column data. I was unable to write the logic for this. Please help..

$(document).ready(function(){ 
var index = $('th:contains("Mar")').eq(0).index(); 
   $("tr").each(function(rowIndex, row){ 
     $(row).find("th:eq(" + index + ")").css('background', 'green'); 
     $(row).find("td:eq(" + index + ")").html('<a href="index">+index+'</a>'); 
}); 

}); 

I have tried the above code, but I want to have the urls with dynamic data

8
  • 1
    What have you tried so far? Show us the code.. Commented Oct 16, 2018 at 8:35
  • @Zim84, $(document).ready(function(){ var index = $('th:contains("Mar")').eq(0).index(); $("tr").each(function(rowIndex, row){ $(row).find("th:eq(" + index + ")").css('background', 'green'); $(row).find("td:eq(" + index + ")").html('<a href="index">+index+'</a>'); }); }); I have tried the above code, but I want to have the urls with dynamic data. Commented Oct 16, 2018 at 8:38
  • You are using jquery datatable and have a column 'URL' where you want to give hyperlinks to that column. Commented Oct 16, 2018 at 8:39
  • @Rojen, I want to give hyperlinks for that column data(all tds for URL column) Commented Oct 16, 2018 at 8:40
  • Are you creating your own table or using jquery dataTable Commented Oct 16, 2018 at 8:44

2 Answers 2

2

you can use jquery datatable render example

 var table = $('#example').DataTable({
    columnDefs: [{
        targets: 5,
        render: function (data, type, full, meta) {
            console.log(data);
            if (data.indexOf("partofyourlink") >-1) {
                return '<a href="http://somelink' + data + '.jpg">' + data + '</a>';
            } else {
                return data;
            }
        }
    }]
});

you can take a look here

Sign up to request clarification or add additional context in comments.

4 Comments

what does this mean "partofyourlink"?
it means the search content like "Mar"
please take a look in jsfiddle
partofyourlink is dynamic and sometimes the position can be changed
0

Try this:

'columns':[{
 'data': //your url data from json,
 render: function(data, type, row) {
     return '<a href= "'+ data +'"></a>"
            }
        }]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.