I have a datatable with some json data. I have added a checkbox in every row but the problem I want to be selecting a single row at a time. i.e when I click on a check box on one row the other check boxes not to be selected. i.e to behave like a radio button.Where am I going wrong with my code.
table
<div>
<h1>Accounts Table</h1>
<table id="employeesTable" class="display">
<!-- Header Table -->
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Amount</th>
<th>Date</th>
<th>Description</th>
<th>Active</th>
</tr>
</thead>
<!-- Footer Table -->
<tfoot>
<tr>
<th>Id</th>
<th>Name</th>
<th>Amount</th>
<th>Date</th>
<th>Description</th>
<th>Active</th>
</tr>
</tfoot>
</table>
</div>
Jquery datatable
$(document).ready( function () {
var table = $('#employeesTable').DataTable({
"sAjaxSource": "/account/list",
"sAjaxDataProp": "",
"order": [[ 0, "asc" ]],
"aoColumns": [
{ "mData": "id"},
{ "mData": "account_type" },
{ "mData": "amount" },
{ "mData": "date" },
{ "mData": "description" },
{ "mData": null,
className: "center",
defaultContent:'<input type="checkbox" name="name1"/>' }
]
}
)
$('input[name="name1"]').on('change', function() {
var checkedValue = $(this).prop('checked');
// uncheck other checkboxes (checkboxes on the same row)
$(this).closest('tr').find('input[name=""]').each(function(){
$(this).prop('checked',false);
});
$(this).prop("checked",checkedValue);
});
2
Answers
Your description is little unclear, but if this is what you need?
Your input:
is not finding anything or maybe itself.
closest
is finding ONLY ancestors.HTML markup goes:
table > thead OR tfoot > tr > input
you got stuck at
tr
and ignoredthead
andtfoot
you need to find witch of those is it and find inputs inside.You can edit properties values by loop only the checked checkboxes:
And when getting table values from ajaxcall, Include onchange of the element in initComplete attribute of datatable,.. like below (because datatable checkbox HTML element will accessible once fully loaded from ajax, untill it won’t accessible. So onchange for checkbox is not applied here…)