Rahul kuchhadia
Rahul kuchhadia

Reputation: 299

How to convert this into DataTable?

I want to display this layout in 5 rows at a time type using DataTable so, please help me with this. I tried many things but I'm unable to do it.

<!DOCTYPE html>
<html>
 <head>
  <title>CSV File to HTML Table Using AJAX jQuery</title>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css"></style>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>

 </head>
 <body>
  <div class="container">
   <div class="table-responsive">
    <h1 align="center">CSV File to HTML Table Using AJAX jQuery</h1>
    <br />
    <div align="center">
     <button type="button" name="load_data" id="load_data" class="btn btn-info">Load Data</button>
    </div>
    <br />
    <div id="employee_table">
    </div>
   </div>
  </div>
 </body>
</html>

<script>


$(document).ready(function(){
 $('#load_data').click(function(){
  $.ajax({
   url:"iguana.csv",
   dataType:"text",
   success:function(data)
   {
    var employee_data = data.split(/\r/);
    var table_data = '<table class="table table-bordered table-sm table-responsive table-striped table-hover ">';
    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>'+cell_data[cell_count]+'</th>';
      }
      else
      {
       table_data += '<td>'+cell_data[cell_count]+'</td>';
      }
     }
     table_data += '</tr>';
    }
    table_data += '</table>';
    $('#employee_table').html(table_data);
   },
    complete: function (data) {
       $('#employee_table').DataTable();
        alert("AJAX request successfully completed");
     }
  });
 });

});


</script> 

I want to display this layout in 5 rows at a time type using DataTable so, please help me with this. I tried many things but I'm unable to do it. is there any way so I can achieve this?

Upvotes: 0

Views: 957

Answers (1)

Bala555
Bala555

Reputation: 189

You are trying to convert div as datatable.

Add one class in table tag, which will represent your table. Let's add myDataTable in table tag.

var table_data = '<table class="myDataTable table table-bordered table-sm table-responsive table-striped table-hover">';

now, convert the table to Datatable by replacing your code

 $('#employee_table').DataTable(); // employee_table is actually an id of div

to

$('.myDataTable').DataTable(); // class that added in table tag

Upvotes: 1

Related Questions