How to print only filtered rows in with jQuery DataTables Buttons to pdf?

2.1k views Asked by At

I am trying to print current filter from jQuery DataTable to a PDF.

I am searching for a solution but usually results are for TableTools but it's retired, so I am using Buttons.

With TableTools, I tried to apply this code:

 $('#example').dataTable( {
    "sDom": 'T<"clear">lfrtip',
    "oTableTools": {
        "aButtons": [
            {
                "sExtends": "copy",
                "sButtonText": "Copy to clipboard",
                "oSelectorOpts": {
                    page: 'current'
                }
            }
        ]
    }
} );

From here https://datatables.net/extensions/tabletools/button_options#oSelectorOpts

And this is my code:

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            {
                extend: 'pdfHtml5',
                download: 'open'
            }
        ]
    } );
} );

Already tried to change many times, still prints all the rows. What am i doing wrong?

1

There are 1 answers

0
Plabon Dutta On

You need to do something like this:

`

<script>
$(document).ready(function() {
    var table = $('#example').DataTable( {
        "pagingType": "full_numbers",
        "iDisplayLength": 10,
        "dom": 'T<"clear">lfrtip',
        "oTableTools": {
          "aButtons": [
            {'sExtends':'copy',
              "oSelectorOpts": { filter: 'applied', order: 'current' },
            },
            {'sExtends':'xls',
              "oSelectorOpts": { filter: 'applied', order: 'current' },
            },
            {'sExtends':'print',
              "oSelectorOpts": { filter: 'applied', order: 'current' },
            }
          ]
        },
    });
});
</script>