Reputation: 1233
I've 1 to n
tables in a HTML page and want to print all of them in one flow.
jQuery Datatables delivers a "build-in" print button but this only works for the assigned table.
However, there must be some logic behind to collect all the data from the rows and print them.
Is it possible to extend this logic to work for several tables on the same page?
I've created this fiddle as a playground.
This is the build in print button logic:
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
'print'
]
} );
Those 2 minified js are needed to make the button work: https://cdn.datatables.net/buttons/2.2.2/js/dataTables.buttons.min.js https://cdn.datatables.net/buttons/2.2.2/js/buttons.print.min.js
Upvotes: 0
Views: 4116
Reputation: 10510
Print Multiple Tables in Multiple Pages
You can make the "Print All" button call a function that will call each table's "Print" button.
So, using your fiddle elemets' ids, define printAll
:
function printAll() {
$('#example_wrapper .buttons-print').click();
$('#example2_wrapper .buttons-print').click();
}
... which will be called on-click:
$('#print-all').click(printAll);
Do note, however, that this will open two different pages for print.
Print Multiple Tables in Single Page
If you want the print to happen in a single page, you'll need to use some customization, by following these steps:
customize
option for each of your "Print" buttons.printAll
function:print
.Full fiddle is here, but for completeness I'll put the final js code in the following snippet:
var all = '';
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'print',
customize: function (win) {
all += $(win.document.body).find('table')[0].outerHTML;
win.close();
},
}
]
} );
$('#example2').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'print',
customize: function (win) {
all += $(win.document.body).find('table')[0].outerHTML;
win.close();
},
}
]
} );
$('#print-all').click(printAll);
});
function printAll() {
$('#example_wrapper .buttons-print').click();
$('#example2_wrapper .buttons-print').click();
var win = window.open("about:blank", "Print View");
win.document.write(
'<html>\
<head>\
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.min.css">\
</head>\
<body></body>\
</html>');
win.document.close();
$(win.document.body).append(all);
setTimeout(function () { win.print(); }, 1000);
}
Further reading:
print
documentationUpvotes: 1