Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
How to add textfile download button in Javascript DataTables?

I need to add a button to download the datatable content as a text file. I have only one column in the datatable. My code is given below. Copy, excel, csv, pdf and print are working properly with below code.

$(document).ready(function () {
         $('#dataTableExample').DataTable({
            "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'copyHtml5', footer: true
                },
                {
                    extend: 'excelHtml5', footer: true
                },
                {
                    extend: 'csvHtml5', footer: true
                },
                {
                    extend: 'pdfHtml5', footer: true
                }
            ],

        });
    });
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The built-in DataTables buttons don't support exporting a single column as a plain text file. You can however achieve this by defining your own button with a custom action.

The snippet below should achieve what you are looking for, it can be added to the buttons array passed to DataTable on setup. This adds a button named 'TXT' which takes the filtered values from the first column in the table, joins then with CR+LF, and then triggers a download of the result as a .txt file.

{
  text: 'TXT',
  action: function (e, dt, node, config) {
    // Generate the text to be exported
    // Please note...
    // - This only takes column 0
    // - The output is filtered based on the applied search
    // - This doesn't include the header or footer
    // - This uses CR+LF line endings
    let text = dt.columns(0, { search: 'applied' }).data().toArray()[0].join('\r\n');

    // Add an element to the page contianing the encoded txt to download
    // click the element to trigger the download, then remove the element
    let element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    element.setAttribute('download', 'data.txt');
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
  }
}

A working example can be seen at https://jsfiddle.net/a3eucptL/.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!