Cannot reinitialise DataTable

Cannot reinitialise DataTable

skinny1991skinny1991 Posts: 4Questions: 2Answers: 0
edited January 2019 in Free community support

Buenas noches progradores

Tengo el siguiente prolblema de Inicializacion de datable, mi tabla tiene varias funciones, exportar a excel, pdf, mostrar en columna individuales, mostrar y ocultar columnas, pero cada vez que recargo la pagina me aparece el error :

DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

Segun la pagina " http://datatables.net/tn/3", hay que destruir la primera funcion, pero de ser así quedaria sin funcionamiento las otras solicitudes de accion.

Este es el codigo. cual seria la solucion? Sin que afecte el funcionamiento de una u otra funcion

<script>

$(document).ready(function() {
// Setup - add a text input to each footer cell
table = $('#example').DataTable({

// Botones para exportar a excel, pdf u otro
dom: 'Blfrtip',buttons: ['copy', 'csv', 'excel', 'pdf', 'print'],
// Botones para mostar numero de registros
"lengthMenu": [[10,30,50,80,100,120, -1], [10,30,50,80,100,120, "All"]]} );


// Botones para mostar y ocultar columnas
MOSTRAR = $('#example').DataTable({
    
});
$(".boton_ocultar_mostrar").on('click', function(){
indice = $(this).index(".boton_ocultar_mostrar");

$(".boton_ocultar_mostrar").eq(indice).toggleClass("btn-danger");
columna = MOSTRAR.column(indice);
columna.visible(!columna.visible());
});

// Agrega campo de búsqueda al pie de la tabla
$('#example tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Buscar por '+title+'" />' );
} );
// Aplicando busqueda por columna
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
} );

</script>

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    Answer ✓

    Hi @skinny1991 ,

    On line 14 you have

    MOSTRAR = $('#example').DataTable({
         
    });
    

    The braces are saying initialise the table with no options - but you have already initialised it with options earlier on line 5. Change this to

    MOSTRAR = $('#example').DataTable();
    

    Cheers,

    Colin

  • skinny1991skinny1991 Posts: 4Questions: 2Answers: 0

    Muchas gracias!!!Con exito el funcionamiento!!

This discussion has been closed.