jQuery DataTables – Row selection using checkboxes and Select extension
For more information, see
https://www.gyrocode.com/projects/jquery-datatables-checkboxes/
HTML
<link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.css">
<script src="https://cdn.datatables.net/v/dt/dt-1.10.16/sl-1.2.5/datatables.min.js"></script>
<link rel="stylesheet" href="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.10/css/dataTables.checkboxes.css">
<script src="//gyrocode.github.io/jquery-datatables-checkboxes/1.2.10/js/dataTables.checkboxes.min.js"></script>
<h3><a href="https://www.gyrocode.com/projects/jquery-datatables-checkboxes/">jQuery DataTables Checkboxes</a></h3>
<a href="https://www.gyrocode.com/projects/jquery-datatables-checkboxes/">See full article on Gyrocode.com</a>
<hr><br>
<form id="frm-example" action="/path/to/your/script.php" method="POST">
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr id="1">
<td></td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr id="2">
<td></td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
<tr id="3">
<td></td>
<td>Name</td>
<td>Position</td>
<td>Office</td>
<td>Extn</td>
<td>Start date</td>
<td>Salary</td>
</tr>
</tbody>
<tfoot>
<tr>
<th></th>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
<hr>
<p>Press <b>Submit</b> and check console for URL-encoded form data that would be...
JavaScript
$(document).ready(function() {
var table = $('#example').DataTable({
'columnDefs': [{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}],
'select': {
'style': 'multi'
},
'fnCreatedRow': function(nRow, aData, iDataIndex) {
$(nRow).attr('data-id', aData.DT_RowId); // or whatever you choose to set as the id
$(nRow).attr('id', 'id_' + aData.DT_RowId); // or whatever you choose to set as the id
},
'order': [
[1, 'asc']
]
});
// Handle form submission event
$('#frm-example').on('submit', function(e) {
var form = this;
var rows = $(table.rows({
selected: true
}).$('input[type="checkbox"]').map(function() {
return $(this).prop("checked") ? $(this).closest('tr').attr('data-id') : null;
}));
//console.log(table.column(0).checkboxes.selected())
// Iterate over all selected checkboxes
rows_selected = [];
$.each(rows, function(index, rowId) {
console.log(rowId)
// Create a hidden element
rows_selected.push(rowId);
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// FOR DEMONSTRATION ONLY
// The code below is not needed in production
// Output form data to a console
$('#example-console-rows').text(rows_selected.join(","));
// Output form data to a console
$('#example-console-form').text($(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});