DataTable Excel Export
by raheelshah31
HTML
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/select/1.2.7/js/dataTables.select.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/buttons/1.2.4/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.4/js/dataTables.buttons.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.4/css/buttons.dataTables.min.css">
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.24/build/vfs_fonts.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/select/1.2.7/css/select.dataTables.min.css">
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td> <input class="checkbox" type="checkbox" data-checked="0"></td>
<td>Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>$320,800</td>
</tr>
<tr>
<td> <input class="checkbox" type="checkbox" data-checked="0"></td>
<td>Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>$170,750</td>
</tr>
<tr>
<td> <input class="checkbox" type="checkbox" data-checked="0"></td>
<td>Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>$86,000</td>
</tr>
<tr>
<td> <input class="checkbox" type="checkbox"...
JavaScript
$(document).ready(function() {
$('.checkbox').click(function() {
debugger
if ($(this).data("checked") === 0) {
$(this).data("checked", "1")
} else {
$(this).data("checked", "0")
}
});
$('#example').DataTable({
dom: 'Bfrtip',
buttons: [{
extend: 'copyHtml5',
exportOptions: {
orthogonal: 'export'
}
},
{
extend: 'excelHtml5',
exportOptions: {
orthogonal: 'export',
format: {
body: function(data, row, column, node) {
// Strip $ from salary column to make it numeric
debugger;
if (column === 0) {
data = $(node).children().data("checked");
}
return data;
}
}
}
},
{
extend: 'pdfHtml5',
exportOptions: {
orthogonal: 'export'
}
}
]
});
});