I found the next code for generating multiple checkboxes inside datatable and its works great:
https://jsfiddle.net/snqw56dw/
var table = $('#example').DataTable({
'ajax': 'https://api.myjson.com/bins/1us28',
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
But when I try to change the code to work with static table data (changed that data will not come from Ajax) its stopped working..
Here is my code:
https://jsfiddle.net/snqw56dw/3158/
var table = $('#example').DataTable({
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
'select': {
'style': 'multi'
},
'order': [[1, 'asc']]
});
I would love to get your help and understand what I'm doing wrong there..
Thanks
In your jQuery DataTables initialization code add checkboxes option for the column where you want the checkboxes to appear using either columns or columnDefs options. If you’re using Select extension, you may want to enable multiple row selection using select.style option.
To center checkbox in the cell, internal DataTables CSS class dt-body-center is used. Option render is used to prepare the checkbox control for being displayed in each cell of the first column. We use a trick with $ ('<div/>').text (data).html () to encode HTML entities that could be present in the data.
Checkboxes is an extension for the jQuery DataTables library that provides universal solution for working with checkboxes in a table. Example below shows a table powered by jQuery DataTables with Select and Checkbox extensions using client-side processing mode where data is received from the server via Ajax request.
As with the other DataTables examples, this is advantageous as an input since we can make use of the features offered by DataTables. Note that the multiple option must be set to true for the datatable to allow multiple selection.
You need to have unique data in the column containing checkboxes - 1
, 2
, 3
, etc.
See updated example for code and demonstration.
As per DataTable Document:
DataTables will automatically add it for you (note that this will work for Ajax and Javascript loaded data as well as for server-side processing).
You can read full document here: https://www.ksia.or.kr/plugin/DataTables-1.10.15/examples/server_side/ids.html
For static record, you can do it this way: https://jsfiddle.net/cwvr7kba/1/
$(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();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With