neeraja sreelaxmi
neeraja sreelaxmi

Reputation: 57

How to convert multiple CSV file to Multiple HTML table using AJAX and Javascript

I have a code which will show an HTML table generated from a csv file on button click . but i want multiple csv file to convert to multiple HTML table on a single button click . Is it possible ?

So here is my script .

$(document).ready(function(){
 $('#Load-Data').click(function(){
  $.ajax({
   url:"OutputNew.csv",
   dataType:"text",
   success:function(data){
    var employee_data = data.split(/\r?\n|\r/);
    var table_data = '<div class="dropdown"><button class="dropbtn">Download</button><div class="dropdown-content"><a href="javascript:createPDF()">Download PDF</a><a href="javascript:downloadInnerHtml()" id="downloadLink">Download HTML</a><a href="javascript:exportTableToExcel()">Download Excel</a></div></div><input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.."><div id="VMTable"><div id="content"><table id="myTable" class="table table-striped""><thead>';
    
    for(var count = 0; count<employee_data.length; count++) {
     var cell_data = employee_data[count].split(',');
     table_data += '<tr>';
     for(var cell_count=0; cell_count<cell_data.length; cell_count++){
      if(count === 0){
       table_data += '<th id="headers" style="position=sticky">'+cell_data[cell_count]+'</th>';
      }else{
          if(cell_data[cell_count] .includes("Not Matching")){
                var ret = cell_data[cell_count].replace('Not Matching','');
                if (ret == ""){
                    table_data += '<td>'+ret+'</td>'
                }else{
                  table_data += '<td data-color="green"><span>'+ret+'</span></td>';
                }
          }else if(cell_data[cell_count] .includes("Matching")){
                var ret = cell_data[cell_count].replace('Matching','');
                if (ret == ""){
                  table_data += '<td>'+ret+'</td>';
                }else if(ret == " "){
                  table_data += '<td>'+ret+'</td>';
                }else{
                  table_data += '<td data-color="green"><span class="badge-complete" style="color:Green">'+ret+'</span></td>';
                }
          }else{
              table_data += '<td>'+cell_data[cell_count]+'</td>';
          }
      }
     }
     table_data += '</tr>';
    }
    table_data += '</table></div><iframe id="txtArea1" style="display:none"></iframe>';
    
    $('#employee_table').html(table_data);
   }
  });   
 }); 
});

and here is HTML button

<button type="button" name="Load-Data" id="Load-Data" class="btn btn-info">Generate Report</button>

Upvotes: 0

Views: 696

Answers (1)

gpl
gpl

Reputation: 1470

Check out this snippet:

<!DOCTYPE html>
<html>
<head>
    <title>CSV Files to HTML Tables</title>
    <!-- JQuery -->
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>   
    <script>
        //add your files to csv_files array
        var csv_files=['username.csv', 'username-password-recovery-code.csv']
        $(document).ready(function(){
            $('#btn_load').click(function(){
                for(var i=0; i<csv_files.length; i++)
                    $.ajax({
                        url: csv_files[i],
                        dataType:'text',
                        success:function(data){
                            var rows = data.split(/\r?\n|\r/);
                            var table = '<table border="1">';
                            //row - iteration

                            //print table header
                            var headings = rows[0].split(";")
                            table += '<thead><tr>';
                            for(var j=0; j<headings.length; j++)
                                table += '<th>' + headings[j] + '</th>';
                            table += '</tr></thead>';

                            //print table body
                            table += '<tbody>';
                            for(var j=1; j<rows.length; j++){
                                var data_cell = rows[j].split(";")
                                table += '<tr>';
                                for(var k=0; k<headings.length; k++)
                                    table += '<td>' + data_cell[k] + '</td>';
                                table += '</tr>';
                            }
                            table += '</tbody>';

                            //closing table, add line break, appending result to div
                            table += '</table><br>';
                            $('#div_results').append(table);
                        }
                    });   
            }); 
        });
    </script>
</head>
<body>
    <div id="div_results" style="border: 5px outset grey; padding: 10px;">
        <h2>--- Results ---</h2>
    </div>
    <button id="btn_load">Get External Content</button>
</body>
</html>

Because you haven't answered my questions I am presenting you a generalized solution. You can use this code to genereate HTML Tables for any no of CSVs having any no. of columns and rows. I have added <thead> and <tbody> tags for your ease. If you not wana use than you can remove them and do any further style specific alterations.

Upvotes: 1

Related Questions