Ashish Patil
Ashish Patil

Reputation: 323

How to use export excel and print for selected columns in datatable?

I want to use export excel and print the datatable .I use following code but not able to use both options for selected columns.

$('#example').DataTable( {
    dom: 'Blfrtip',
                buttons: [
                        {
                    extend: 'excel','print',

                    exportOptions: {
                        columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
                    },

                }

                ],


                "lengthMenu": [[200, 250, 500, -1], [200, 250, 500, "All"]],
                 "bLengthChange" : true,


    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select><option value="">Select</option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
} );

I am not figure out what happens wrong.Please help me.

Upvotes: 4

Views: 10869

Answers (3)

Muhammad Zubair
Muhammad Zubair

Reputation: 574

Just modify your buttons option this way

buttons: [
  {
    extend: 'excel',
    exportOptions: {
      columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
    },
  },
  {
    extend: 'print',
    exportOptions: {
      columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
    },
  },
]

Elaborated @tdymicroit's answer

Upvotes: 2

Ahmed Elbatt
Ahmed Elbatt

Reputation: 1038

One of the most commonly used is the columns option which defines the columns that should be used as part of the export. This is given as a column-selector, making it simple to tell it if you want only visible columns, or a mix of the columns available.

and this is a simple of how it can be done :

function attachDataTable(tableId: string) {
        let companyFunctionTable = $('#' + tableId).DataTable({
            dom: 'Bfrtip',
            buttons: [
                //'csvHtml5'
                {
                    extend: 'csvHtml5',
                    exportOptions: {
                        columns: [0, 1, 2]
                    },
                    className: 'btn btn-sm btn-outline-secondary',
                    text: `<i class="fa fa-file-csv"></i> ${i18next.t('Export to CSV')}`, titleAttr: i18next.t('Export to CSV'),
                    bom: true
                },
            ],
            "initComplete": function (settings, json) {
                placeToolbarButton(tableId);
            },
            "paging": globalVar.dataTablesSettings.paging,
            "stateSave": false,
            "stateSaveParams": function (settings, data: any) {
                data.search.search = "";
            },
            "order": [],
            "lengthChange": false,
            "pageLength": globalVar.dataTablesSettings.pageLength,
            "language": {
                "url": globalVar.dataTablesSettings.languageUrl
            }
        });
    }

Please fellow that link : https://datatables.net/extensions/buttons/examples/html5/columns.html

Don't forget to include the required JS libraries needed to display the exporting functionality.

Upvotes: 0

tdymicroit
tdymicroit

Reputation: 31

{
 extend: 'excel'
 exportOptions: {
 columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
},
{
 extend: 'print'
 exportOptions: {
 columns: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
}

Upvotes: 3

Related Questions