Datatables PDF export - How to order for fix export ?

Datatables PDF export - How to order for fix export ?

zayderszayders Posts: 5Questions: 2Answers: 0

Hello,

I have two button PDF.
The first to export the table (with the sorting the user want) and an other one to export data with a specific order.

The problem is : how can i order data (asc or desc) just for one button ? I don't want to export data with basic order or the user order choice but with my own order.

example : the table is order like this at the beginning :

                            >  var table = $('#example1').DataTable( {

"scrollX": true,
"responsive": false,
"order": [[0,"desc"]],
autoWidth: true,
dom: 'lf<"floatright"B>rtip',
buttons: [
{
extend: 'pdfHtml5',
text: 'PDF',
orientation: 'landscape',
title: 'Trials',
},
{
second pdf button...
}
....

Is it possible to specify in the button a special order like : "order": [[0,"desc"]], ?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin
    Answer ✓

    What you would need to do for this is create a custom button and use order() to set the order for the table that you want to export. Then call the PDF button's original action function - there is an example in the buttons.buttons.action documentation showing how to do that.

    Allan

  • zayderszayders Posts: 5Questions: 2Answers: 0

    Thanks for your reply, it works fine with this code :
    `

                action: function ( e, dt, node, config )
                {
                    this.order([6,'desc']).draw();
    
                    $.fn.dataTable.ext.buttons.pdfHtml5.action.call(this, e, dt, node, config);
                },
    

    `

    I have a last question, is it possible to have the same result without using the draw() function ? Because without 'draw()', the order is not apply to the pdf. Or maybe i forgot something :blush:

  • allanallan Posts: 63,461Questions: 1Answers: 10,466 Site admin
    Answer ✓

    I have a last question, is it possible to have the same result without using the draw() function ?

    No - the draw actually enacts the order. What you would need to do is read the current order, then apply your own order and export the pdf. Then trigger the order to switch back to what it used to be.

    Allan

This discussion has been closed.