DataTable export columns

DataTable export columns

Steveb13Steveb13 Posts: 11Questions: 5Answers: 0

Hi, I am wishing to export to PDF a table. I have used Colvis to select the columns but when exporting All columns are exported. Is their a f=unction for me to just Export the selected columns, Thanks

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406
    edited December 2022 Answer ✓

    Use

    ...
    exportOptions: {
        columns: ':visible'
    }
    

    There is also an example in the docs:
    https://datatables.net/extensions/buttons/examples/print/columns

  • Steveb13Steveb13 Posts: 11Questions: 5Answers: 0
    edited December 2022

    Thanks for replying. This worked for one option but I was wishing to have 3 options "Print. PDF and Excel. If i try to add them as extra options I have No Option display. Should I be pursuing this or is this a limitation. Thanks

    $(document).ready(function () {
    $('#myTable').DataTable(
    {
    dom: 'Bfrtip',
    buttons: [
    {
    extend: 'pdf',
    exportOptions: {
    columns: ':visible'
    }
    },
    'colvis'
    ],
    "scrollY": "550px",
    "scrollCollapse": true,
    "paging": true,
    "order": [[0, "asc"]],
    lengthMenu: [[10,25,-1],[10,25,"All"]],
    "columnDefs": [{
    "targets": [3,4,5,6],
    "searchable": false,
    "orderable": false
    }]
    }
    );
    });

  • Steveb13Steveb13 Posts: 11Questions: 5Answers: 0
    edited January 2023

    Hi, I have solved it. All I needed to do was add the "extend" separately for each option I wanted. Thanks for giving me the right direction, cheers

                dom: 'Bfrtip',
                buttons: [
                    {
                        extend: 'pdf',  
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                   
                    {
                        extend: 'excel',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
                    {
                        extend: 'print',
                        exportOptions: {
                            columns: ':visible'
                        }
                    },
    
  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Super - thanks for the update. Good to hear you've got it working as you need now.

    Allan

Sign In or Register to comment.