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éfono</th><th>Mó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"> </span></td></tr>