-2

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

jdag
  • 5
  • 1
  • Do you want to do this in javascript? – Zano Oct 13 '21 at 21:58
  • 1
    Please show what you have tried and clearly define where your challenge lies with that attempt. Post as actual code here please. – Mark Schultheiss Oct 13 '21 at 22:18
  • What would such a CSV file look like? One CSV file typically only has one heading row, and all the data rows have the same number of fields as that one header row. If you have multiple tables, you also have multiple different header rows - and tables with potentially different numbers of columns. – andrewJames Oct 13 '21 at 22:20

2 Answers2

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()

Amendra
  • 1
  • 1
-1

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();
});

jdag
  • 5
  • 1
  • Some notes, in case they help future visitors to this answer: (1) This answer appears to be based on the code from this other answer: [Export and append object to csv](https://stackoverflow.com/a/57543226/12567365). (2) The end result is not a valid CSV file - but perhaps the OP is OK with that, in this case. – andrewJames Oct 15 '21 at 13:23
  • (3) The code does not handle double-quotes correctly here: `value.replace('"', '\"')`. This will replace `"` with `"`. It will also only replace the _first_ occurrence of a `"` in a given string, because it is replacing a literal, and not using a regular expression. The code needs to use `value.replaceAll('"', '\\"')`. (4) Because the approach does not use a CSV library, there may be other ways in which the exported data will not be valid CSV data. – andrewJames Oct 15 '21 at 13:24