Good evening, I'm working with Jquery , extracting data from a database and creating a dynamic table with the data. That is, I perform a data extraction in a javascript function:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", $urlEnvio, false);
xmlhttp.setRequestHeader("accept" ,"application/json");
xmlhttp.setRequestHeader("content-type" ,"application/json");
xmlhttp.send($urlEnvio);
var resultado = xmlhttp.response;
var long = resultado.length;
var elementos =JSON.parse(resultado);
Next, I create a header of the table (it's the same as in the dataTables example):
$('#infoDonantes').append("<div class='panel-body'>"+
"<table width='100%' class='table table-striped table-bordered table-hover' id='dataTables-example'>"+
"<thead>"+
"<tr role='row'>"+
"<th class ='sorting_asc'>Numero de Historial</th>"+
"<th class ='sorting_asc'>Nombre</th>"+
"</thead>"+
"<tbody>");
And finally I go through the data with a loop to print all the results:
for(var i = 0; i < elementos.count; i++){
$('#dataTables-example').append("<tr class='odd gradeA'>"+
"<tr class='odd gradeA'>"+
"<td>"+elementos.historias[i].nh+"</td>"+
"<td>"+elementos.historias[i].name+"</td>"+
"</tr>");
}
$('</tr> ').append("</tbody>"+
"</table>"+
"</div>"+
"<script src='vendor/datatables/js/jquery.dataTables.min.js'></script>"+
"<script src='vendor/datatables-plugins/dataTables.bootstrap.min.js'></script>"+
"<script>"+
"$(document).ready(function() {"+
"$('#dataTables-example').DataTable({"+
"responsive: true"+
"});"+
"});"
);
The problem is that this effectively shows me the results without problems, but as a normal table, that is, the search and sorting effect of the dataTables plugin does not apply. In the example that comes in the plugin the following lines are the ones that start the characteristics of the table:
<script src="../vendor/datatables-plugins/dataTables.bootstrap.min.js"></script>
<script src="../vendor/datatables-responsive/dataTables.responsive.js"></script>
<!-- Custom Theme JavaScript -->
<script src="../dist/js/sb-admin-2.js"></script>
<!-- Page-Level Demo Scripts - Tables - Use for reference -->
<script>
$(document).ready(function() {
$('#dataTables-example').DataTable({
responsive: true
});
});
</script>
But I do not know where to insert that fragment so that the plugin works correctly. I hope you have explained me well. I would appreciate a help because it brings me head.