Multiple tables with only one button differing
Multiple tables with only one button differing
I have a page that queries data from WordPress and WooCommerce to produce anywhere from one to 20 tables. All the buttons and vars between each table are the same, but I want the print message to fill in a value specific to each table so it is clear what one was printed.
Example page: https://sandbox.lockersoccer.com/rosters/?class=11161
And my DataTables code that appears once for this page:
<script>
$(document).ready(function() {
var table = $('table.display').DataTable( {
"columnDefs": [
{ targets: [-1, -2, -3], visible: false}
],
"paging": false,
dom: 'Bfrtip',
buttons: [
{
extend: 'collection',
text: '<i class="fas fa-file-export"></i> Export',
buttons: [
'csv',
{
extend: 'excel',
messageTop: '<?php echo get_the_title( $product_id ); ?>'
},
'pdf',
]
},
{
text: '<i class="fas fa-print"></i> Print',
extend: 'print',
customize: function ( win ) {
$(win.document.body)
.css( 'font-size', '9pt' );
$(win.document.body).find( 'dd' )
.css( 'display', 'inline-block' )
.css( 'padding-right', '.25em' )
.css( 'margin', '0' );
$(win.document.body).find( 'dt' )
.css( 'display', 'inline-block' )
.css( 'padding-right', '.25em' )
.css( 'margin', '0' );
$(win.document.body).find( 'dl' )
.css( 'display', 'inline-block' )
.css( 'margin', '.2em' );
$(win.document.body).find( 'table' )
.addClass( 'compact' )
.css( 'font-size', 'inherit' );
},
exportOptions: {
columns: ':visible'
},
message: '<?php echo get_the_title( $product_id ); ?><br><?php echo $class_details ?>',
},
{
text: '<i class="far fa-copy"></i> Copy Emails',
extend: 'copy',
header: false,
title: '',
exportOptions: {
columns: '.email',
rows: function ( idx, data, node ) {
// If rows are selected, export those, if not, export all
if(table.rows( { selected: true } ).any())
return $.inArray(parseInt(data.asset_id), table.rows( { selected: true } )) !== 1;
return true;
}
}
}, 'colvis'
],
select: true
} );
} );
</script>
As you can see, I'm trying to load a variable into the message portion, but obviously this is only using the first variable and not the one specific to each loop value. Works fine for one table per page, but not multiples. I having trouble finding a way to apply the different print message to each table specifically.
Answers
Maybe you can use a function for
message
as described here:https://datatables.net/reference/button/print
Kevin