Trying to add Datatables into my Sinatra App. It has a feature to export tables built into the JS. I am able to run it, but get an error with this line to solve. Datatables Error
How do I incorporate this extra JS code into my .DataTable? I am not entirely sure where I need to place it. Here's the current code throwing the error (sorry if formatting is off):
$(document).ready(function() {
$('#ma_pen_report').DataTable( {
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
} );
} );
$(document).ready(function() {
$('#ma_pen_report').DataTable( {
initComplete: function () {
this.api().columns().every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $(column.footer()).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
} );
} );
I think I just need to insert the dom: & buttons: array into the next block of code But I can't find an example of how to do that. Any help would be awesome!
As the error says, the problem is because you're initialising the table twice - once on line 2, and again on line 10. You've duplicated the (document).ready(function()
too. To do it all in one hit, have it like this:
$(document).ready(function() {
$('#ma_pen_report').DataTable({
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
initComplete: function() {
this.api().columns().every(function() {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo($(column.footer()).empty())
.on('change', function() {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search(val ? '^' + val + '$' : '', true, false)
.draw();
});
column.data().unique().sort().each(function(d, j) {
select.append('<option value="' + d + '">' + d + '</option>');
});
});
}
});
});