Export PDF, CSV, Excel form datatable using jquery?

I use this code and it works fine.

But I want to get bounded string data and bounded words from a string from datatable.

What am I 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>

Another question:

How to remove text spaces in columns when exporting export.? I get issues like spaces in excel table cell, is it possible to remove spaces ..?

+4
source share
1 answer

You can use the DataTables extension: DataTables Select

Add to button export options:

modifier: {
      selected: true
}

and

select: true

to initialize the 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 lines.

+1
source

Source: https://habr.com/ru/post/1694862/


All Articles