Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
export all tables to single csv

im using datatables.net and some of my pages have multiple tables. i cant figure out how to iterate over each table saving the data to a variable so that i can push it out as a single csv when the export button is clicked.

some tables have different headers and this is all fine. i just want to output all the th and td values from within the whole table to a single csv. if datatables doesnt support this already in some roundabout way then its fine if it is jquery or javascript.

thanks in advance

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this method

<table id="example1" class="table" style="width:100%">  
</table>

<table id="example2" class="table" style="width:100%">
</table>

JS:

var table = $('#example1').DataTable();
var data = table.buttons.exportData();
// Do something with the 'data' variable

buttons.exportData()

about 4 years ago · Juan Pablo Isaza Relatório

0

this is what worked for me. thanks.

$( "#exportalltocsv" ).on( "click", function(e) {
    e.preventDefault();
    let csvstring = "";
        $('.dataTable').each( function () {
            var columns = [];
            $(this).DataTable().columns().eq(0).each( function ( index ) {
                var column = this.column( index ).header();
                columns.push($(column).html());
            } );
            csvstring += columns.map(th => '"' + th.replace('"', '\"') + '"').toString() + "\n";
            $(this).DataTable().rows().every( function ( rowIdx, tableLoop, rowLoop ) {
                csvstring += this.data().map(value => '"' + value.replace('"', '\"') + '"').toString() + "\n";
            } );
            csvstring += "\n\n";
        });
    var hiddenElement = document.createElement('a');
    hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvstring);
    hiddenElement.target = '_blank';
    hiddenElement.download = '<?=$pagetitle.date(' Y-m-d H-i-s')?>.csv';
    hiddenElement.click();
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda