Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export PDF, CSV, Excel form Datatable using jquery?

I'm using this code and it is working fine.

But I want to get limited row data and limited words from a row from datatable.

What I'm doing Jquery code:

if( $('.clienttable').length > 0 ) {
        $('.clienttable').DataTable( {           
            pageLength: 10,
            responsive: true,
            dom: '<"html5buttons"B>lTfgitp',
            buttons: [
                {
                    extend: 'copyHtml5',
                    exportOptions: {
                        columns: [1, 2, 3,4,5,6]
                    }
                },
                {
                    extend: 'csvHtml5',
                    exportOptions: {
                        columns: [1, 2, 3,4,5,6]
                    }
                },
                {
                    extend: 'excelHtml5',
                    exportOptions: {
                        columns: [1, 2, 3,4,5,6]
                    }
                },
                {
                    extend: 'pdfHtml5',
                    exportOptions: {
                        columns: [1, 2, 3,4,5,6]
                    },
                    title: 'List of Clients',
                }
            ],
            columnDefs: [
                { targets: [0], orderable: false },
                { targets: [7], orderable: false }
             ],
            "order": [[ 6, "desc" ]]  
        });
    }

Frontend Code:

<table class="table table-striped table-bordered table-hover clienttable" >
</table>

Other Question:

How to remove white spaces text in columns when excel export..? i m getting issues like white spaces in the excel table cell, can this be possible to remove white spaces..?

like image 889
Vishvakarma Dhiman Avatar asked Jun 19 '26 22:06

Vishvakarma Dhiman


1 Answers

You can use DataTables Select extension: DataTables Select

Add to button export options:

modifier: {
      selected: true
}

and

select: true

to initialize DataTable code.

Your new code:

if( $('.clienttable').length > 0 ) {
    $('.clienttable').DataTable( {           
        pageLength: 10,
        select: true,
        responsive: true,
        dom: '<"html5buttons"B>lTfgitp',
        buttons: [
            {
                extend: 'copyHtml5',
                exportOptions: {
                    columns: [1,2,3,4,5,6],
                    modifier: {
                        selected: true
                    }
                }
            },
            {
                extend: 'csvHtml5',
                exportOptions: {
                    columns: [1,2,3,4,5,6],
                    modifier: {
                        selected: true
                    }
                }
            },
            {
                extend: 'excelHtml5',
                exportOptions: {
                    columns: [1,2,3,4,5,6],
                    modifier: {
                        selected: true
                    }
                }
            },
            {
                extend: 'pdfHtml5',
                exportOptions: {
                    columns: [1,2,3,4,5,6],
                    modifier: {
                        selected: true
                    }
                },
                title: 'List of Clients',
            }
        ],
        columnDefs: [
            { targets: [0], orderable: false },
            { targets: [7], orderable: false }
         ],
        "order": [[ 6, "desc" ]]  
    });
}

Now you can print only selected rows.

like image 81
Zafahix Avatar answered Jun 22 '26 12:06

Zafahix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!