bmancell
bmancell

Reputation: 13

jQuery AJAX table: create hyperlinks

I have a dynamic table that is created from a parsed csv file. I am using jQuery and AJAX to do this. I need to create hyperlinks for each job number in the 'Job Number' field. The URL for the hyperlinks would be something like this: 'http://test_site/JobsQueryByNumberServlet?passjobnumber=xxxxxxxxx' where the 'xxxxxxxxx' is the job number. The user would be able to click on a job number and open the url. Can anyone help me out? I am new to jQuery and AJAX. Below is my code and an image of the table. Thanks!

$(document).ready(function(){
 $(function(){
  $.ajax({
   url:"https://raw.githubusercontent.com/Biggest_Projects.csv",
   dataType:"text",
   success:function(data)
   {
    var construction_data = data.split(/\r?\n|\r/);
    var table_data = '<table class="table table-hover table-condensed">';

    for(var count = 0; count<construction_data.length; count++)
    {

     var cell_data = construction_data[count].split(",");

     table_data += '<tr>';
     for(var cell_count=0; cell_count<cell_data.length; cell_count++)
     {
      if(count === 0)
      {
       table_data += '<th>'+cell_data[cell_count]+'</th>';
      }
      else
      {
       table_data += '<td>'+cell_data[cell_count]+'</td>';
      }
     }
     table_data += '</tr>';

    }

    table_data += '</table>';

    $('#Major_Construction').html(table_data);
   }
  });
 });

});

table example

Upvotes: 1

Views: 43

Answers (1)

fdomn-m
fdomn-m

Reputation: 28611

From the sample table, the job number is the 3rd column: cell_count==2

There are other options, but you can extend your if :

if (count === 0) {
  table_data += '<th>' + cell_data[cell_count] + '</th>';
} else {
  if (cell_count === 2) {
    table_data += '<td>' 
                + '<a href="http://test_site/JobsQueryByNumberServlet?passjobnumber=' 
                + cell_data[cell_count] 
                + '">' 
                + cell_data[cell_count] 
                + '</a>' 
                + '</td>';
  } else {
    table_data += '<td>' + cell_data[cell_count] + '</td>';
  }
}

Upvotes: 1

Related Questions