Search code examples
javascriptdatatableexportcol

Export from datatable only selected columns


I am trying to export a datatable to a excel, pdf and just to copy it. But i just want to export the selected columns from colvis. When i am exporting the table, it always shows me the full datatable. Where is my mistake?

Thx

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $('#tbl').DataTable( {
        dom: 'lBfrtip', 
        language: {
            searchBuilder: {
                button: 'Filter',
            }
        },
        buttons:[
            'searchBuilder',
            {
            extend: 'colvis',
                collectionLayout: 'fixed columns',
                collectionTitle: 'Spalten Auswahl'
            },
            {
            extend: 'copy',
            text: 'Copy',
            exportOptions: {
                modifier: {
                    columns: ':visible'
                }
            }
        }, {
            extend: 'excel',
            text: 'Excel',
            exportOptions: {
                modifier: {
                    columns: ':visible'
                }
            }
        }, 'pdf'        
        ],       
        colReorder: true
        //scrollX: true,
    });
}); 
</script>

Solution

  • Instead of this:

    exportOptions: {
      modifier: {
        columns: ':visible'
      }
    }
    

    Just use this:

    exportOptions: {
      columns: ':visible'
    }
    

    An example is provided here: Export options - column selector. This is for the print button, but it is the same for the other buttons.

    columns: The column selector to use. Default - all columns.

    The modifier option is used to control something different:

    modifier: How the ordering and search state of the table should be applied. Default - {search: 'applied', order: 'applied'}