DataTables append with AJAX

0

I need to add a result obtained through AJAX to a table, but I can not get the answer to show correctly. In fact, what happens is that each time the table is displayed, the result is added again.

$(document).ready(function(){

	  
var tableSucursal =  $('#SucursalesTabla').DataTable(
  {
    responsive: true,
	rowReorder: true,
	"scrollX": true 
});


tableSucursal.rows().every( function () {
	
    this.child( '<table id="InternosTabla" width="100%"><thead><tr><th  class="id">Apellido</th><th>Nombre</th><th>Cargo</th><th>Tel&eacute;fono</th><th>M&oacute;vil</th><th colspan="2">E-mail</th><th>Observaciones</th><th align="center"><a id="AgregarInternos" style="cursor:pointer; display:none;" onclick="AgregarInternos();">Nuevo</a></tr></th>'+
	'</thead>'+
	'<tbody>'+

	'</tbody>'+
	'</table>'	
	);
	

	
});

function datos(sucursal) {

$.ajax({
		type: "POST",
		url: "clientes_showint.php",
		data: {sucursal:sucursal},
		success: function(data){
	$("#InternosTabla tbody").append(data);
		}
		});
}


$('#SucursalesTabla tbody').on( 'click', 'tr', function () {
	var tr = $(this).closest('tr');
    var row = tableSucursal.row( tr );

    var child = tableSucursal.row(this).child;
	var sucursal=$(this).closest("tr").find(".id").text();	

 
    if (row.child.isShown()) {
        row.child.hide();
    }
    else {
    row.child.show();
	row.child(datos(sucursal)).show();
    }
});	  

The AJAX call returns

<tr class="Internos"><td></td><td id="idintaut53" class="idinterno" style="display:none;" data-campo="id_intaut">53</td><td id="intaut" class="" data-campo="apellido"><span>590005</span></td><td id="intaut" class="" data-campo="nombres"><span></span></td><td id="intaut" class="" data-campo="cargo"><span></span></td><td id="intaut" class="" data-campo="telfijo"><span></span></td><td id="intaut" class="" data-campo="telmovil"><span></span></td><td id="intaut" class="" data-campo="email" colspan="2"><span></span></td><td id="intaut" class="" data-campo="observaciones"><span></span></td><td id="intaut" class="" data-campo="ActivoNoActivo" align="center"><span class="label label-success">&nbsp;</span></td></tr>
    
asked by pointup 08.12.2018 в 00:47
source

1 answer

0

datatable js already has a function to load

tableSucursal.clear().rows.add(data).draw();

where:

answered by 08.12.2018 в 01:06