How to export paginated table data in CSV?

Viewed 343

I have a problem exporting the paginated table in csv file. It cannot export the whole paginated table, just only export the first paginated table data.

Below is my coding:

HTML

<button onclick="exportTableToCSV('manufacturer_brand.csv')">Export</button>
<table id="table_js" class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th style="width: 10%;">Action</th>
                            <th style="width: 5%;">No.</th>
                            <th style="">Brand Name</th>
                    </thead>
                    <tfoot>
                        <tr>
                            <th style="width: 10%;">Action</th>
                            <th style="width: 5%;">No.</th>
                            <th style="">Brand Name</th>
                        </tr>
                    </tfoot>
                </table>

Javascript

function downloadCSV(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV file
    csvFile = new Blob([csv], {type: "text/csv"});

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // Create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Hide download link
    downloadLink.style.display = "none";

    // Add the link to DOM
    document.body.appendChild(downloadLink);

    // Click download link
    downloadLink.click();
}


    function exportTableToCSV(filename) {
        var csv = [];
        var rows = document.querySelectorAll("table tr");
        
        for (var i = 1; i < rows.length; i++) {
            var row = [], cols = rows[i].querySelectorAll("td, th");
            
            for (var j = 1; j < cols.length; j++) 
                row.push(cols[j].innerText);
            
            csv.push(row.join(","));        
        }

        // Download CSV file
        downloadCSV(csv.join("\n"), filename);
}

Export CSV file result like below, just can export first paginated page table, other paginate cannot export:

csv

Below is my table, actually I have a total of 7 paginated pages in the tablecsv2

How someone can guide me on how to export paginate table in a CSV file.

0 Answers
Related