I think the best thing to do is to remove the paging, then do the export, then turn the paging back on once it's done.
I made a couple minor changes:
$(function ()
{
var table = $('#example').DataTable();
$("#btnExport").click(function(e)
{
table.page.len( -1 ).draw();
window.open('data:application/vnd.ms-excel,' +
encodeURIComponent($('#example').parent().html()));
setTimeout(function(){
table.page.len(10).draw();
}, 1000)
});
});
Updated fiddle: http://jsfiddle.net/jzdjdo3z/176/
Page Length docs: https://datatables.net/reference/api/page.len()
Paging option docs: https://datatables.net/reference/option/paging
I'm not sure why initializing with dataTables vs DataTables made a difference, but it did. So keep an eye out for that.
Answer from dmgig on Stack OverflowCatch Export to Excel onclick event — DataTables forums
jQuery datatables exporting data to Excel
How to call Datatable csv button from custom button
jquery - automatic export to excel using datatables - Stack Overflow
Videos
I think the best thing to do is to remove the paging, then do the export, then turn the paging back on once it's done.
I made a couple minor changes:
$(function ()
{
var table = $('#example').DataTable();
$("#btnExport").click(function(e)
{
table.page.len( -1 ).draw();
window.open('data:application/vnd.ms-excel,' +
encodeURIComponent($('#example').parent().html()));
setTimeout(function(){
table.page.len(10).draw();
}, 1000)
});
});
Updated fiddle: http://jsfiddle.net/jzdjdo3z/176/
Page Length docs: https://datatables.net/reference/api/page.len()
Paging option docs: https://datatables.net/reference/option/paging
I'm not sure why initializing with dataTables vs DataTables made a difference, but it did. So keep an eye out for that.
If you want download as .xlsx format, use following code. Here you can define the name of the file also. Scripts:
<script src="https://unpkg.com/xlsx/dist/shim.min.js"></script>
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
<script src="https://unpkg.com/[email protected]/Blob.js"></script>
<script src="https://unpkg.com/[email protected]/FileSaver.js"></script>
Javacript:
var table = $('#example').DataTable();
$("#btnExport").click(function(e)
{
var fileName = "test";
var fileType = "xlsx";
var table_obj = document.getElementById("example");
var wb = XLSX.utils.table_to_book(table_obj, {sheet: "Sheet JS"});
return XLSX.writeFile(wb, null || fileName + "." + (fileType || "xlsx"));
});
But issue is, you can download first page only. If any one found to full table, update here. Thanks in Advance.
At last i found the solution.
In Datatable configuration , i added click event for the button to be triggered.
buttons: [
{
extend: 'csv',
}
]
$("#ExportReporttoExcel").on("click", function() {
table.button( '.buttons-csv' ).trigger();
});
This works fine for me thanks for the comments and answers
dataTables export buttons is by default enriched with signature classes like .buttons-excel, .buttons-pdf, .buttons-csv and so on. Take advantage of that :
$('#ExportReporttoExcel').on('click', function() {
$('.buttons-excel').click()
});
You could click() the excel button in the initComplete callback :
initComplete: function() {
$('.buttons-excel').click()
}
forked fiddle (not intentionally) -> https://jsfiddle.net/850z70do/
You can add a 'drawCallback' option to your datatables configuration.
drawCallback: function () {
$('.buttons-excel').trigger('click')
}
Add this script
$(document).ready(function() {
$('#all_sub').DataTable( {
dom: 'lBfrtip',
buttons: [
'excelHtml5',
]
} );
} );
And load the below datatable libraries
https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css
https://cdn.datatables.net/buttons/1.5.6/css/buttons.dataTables.min.css
https://code.jquery.com/jquery-3.3.1.js
https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js
https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js
https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js
https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js
https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js
https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js
Note:- Please note - this property requires the Buttons extension for DataTables.
CSS
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.5.6/css/buttons.dataTables.min.css">
JS
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
<script type="text/javascript" language="javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
HTML
<table id="all_sub" class="table">
<thead class="thead-light">
<tr>
<th>Subscriber Name</th>
<th>Course Name</th>
</tr>
</thead>
<tbody>
<?php foreach($allsubscriber as $allsub):?>
<tr>
<td><?php echo $allsub->user_firstname;?></td>
<td><?php echo $allsub->course_title;?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
JQuery Script
$(document).ready(function() {
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
} );
} );
You can manually trigger the event of the button you wish to 'click'.
Example:
var myDataTable1 = $('#dt1').DataTable({
buttons: [
'excelHtml5'
]
});
// Use jQuery selector (in this case looking for known class name) for the hidden button, then trigger it's action event.
myDataTable1.buttons('.buttons-excel').trigger();
Extra: Based on the above example, here's a quick and dirty little function you can use.
//example: TriggerDataTableButton(myDataTable1, '.buttons-excel');
function TriggerDataTableButton(dataTable, buttonSelector) {
dataTable.buttons(buttonSelector).trigger();
};
Couple what @DelightedD0D said in his comment:
You could always just do
$('#floatingBtn').click(function(){ $('#someExportBtn').click(); });
... with some CSS to force the original buttons out of view off-screen and you'd wind up with only one set of buttons being shown.
.buttons-html5, .buttons-print {
position: absolute;
left: -9999px;
}

