I have a table with a column full of checkboxes. At the top I would like to have a single "Select All" checkbox that would check all the checkboxes on that page.
How should I implement this? I'm using jQuery as my JavaScript framework if it matters.
This will keep all the individual checkboxes the same as the "check all" one
$("#id-of-checkall-checkbox").click(function() {
$(".class-on-my-checkboxes").attr('checked', this.checked);
});
This will keep the "check all" one in sync with whether or not the individual checkboxes are actually all checked or not
$(".class-on-my-checkboxes").click(function() {
if (!this.checked) {
$("#id-of-checkall-checkbox").attr('checked', false);
}
else if ($(".class-on-my-checkboxes").length == $(".class-on-my-checkboxes:checked").length) {
$("#id-of-checkall-checkbox").attr('checked', true);
}
});
jquery (EDITED to toggle check/uncheck all):
$(document).ready(function() {
$("#toggleAll").click(function() {
$("#chex :checkbox").attr("checked", $(this).attr("checked"));
});
});
The reason why I had to do a click() then check for checked status is because if you try to "toggle" a checkbox, the checkbox being toggled will not retain its checked status. This way it retains the check status and effectively toggles.
HTML:
<input type="checkbox" id="toggleAll" />
<div id="chex">
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</div>
For me, Jeremy's solution mostly worked, but I had to replace .attr with .prop. Otherwise once I single clicked on a checkbox it would stop reacting to the "master" checkbox.
in the _Layout.cshtml (master page)
$(document).ready(
manageCheckboxGroup('chkAffectCheckboxGroup', 'checkbox-group')
);
in a referenced .js
function manageCheckboxGroup(masterCheckboxId, slaveCheckboxesClass) {
$("#" + masterCheckboxId).click(function () {
$("." + slaveCheckboxesClass).prop('checked', this.checked);
});
$("." + slaveCheckboxesClass).click(function () {
if (!this.checked) {
$("#" + masterCheckboxId).prop('checked', false);
}
else if ($("." + slaveCheckboxesClass).length == $("." + slaveCheckboxesClass + ":checked").length) {
$("#" + masterCheckboxId).prop('checked', true);
}
});
}
in the HTML (Razor) page
<table class="table">
<thead>
<tr>
<th><input id="chkAffectCheckboxGroup" type="checkbox" checked="checked" /></th>
<th>
@Html.DisplayNameFor(model => model.Clients[0].ClientId)
</th>
<th>
@Html.DisplayNameFor(model => model.Clients[0].Name)
</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Clients.Count(); i++)
{
<tr>
<td>
<input type="hidden" asp-for="Clients[i].Id" class="form-control" />
<input type="hidden" asp-for="Clients[i].Name" />
<input type="checkbox" class="checkbox-group" asp-for="Clients[i].Selected" />
</td>
<td>
@Html.DisplayFor(modelItem => Model.Clients[i].Id)
</td>
<td>
@Html.DisplayFor(modelItem => Model.Clients[i].Name)
</td>
</tr>
}
</tbody>
</table>
IMPORTANT:
also, at first I had a @foreach loop in the HTML and it did not work..., you must use a @for (int i = 0; i < Model.Clients.Count(); i++) loop instead otherwise you end up with a empty list in the OnPostAsync().
Slightly modifying markup from Marve's answer (giving ID to the table)
EDIT:
Updated version where the 'selectAll' checkbox correctly reflects the state of the checkboxes. E.g. if you select all checkboxes manually, selectAll checkbox will automatically get checked. Try the demo to understand the behavior.
Code:
<table id='myTable'>
<thead>
<tr>
<th><input type="checkbox" /></th>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td>Tabular Data 1</td>
<td>Tabular Data 2</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Tabular Data 3</td>
<td>Tabular Data 4</td>
</tr>
</tbody>
</table>
Your jQuery could be as simple as this:
$(document).ready(
function()
{
$('#myTable th input:checkbox').click(
function()
{
$('#myTable td input:checkbox').attr('checked', $(this).attr('checked'));
}
);
//The following code keeps the 'selectAll' checkbox in sync with
//the manual selection of the checkboxes by user. This is an additional usability feature.
$('#myTable tr input:checkbox').click(
function()
{
var checkedCount = $('#myTable td input:checkbox:checked').length;
var totalCount = $('#myTable td input:checkbox').length;
$('#myTable th input:checkbox').attr('checked', checkedCount === totalCount);
}
);
}
);
While the answers posted previously will work, you'll run into issues if you have more than one set of checkboxes in a single page.
This format will work regardless:
<table>
<thead>
<tr>
<th><input type="checkbox" /></th>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td>Tabular Data 1</td>
<td>Tabular Data 2</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Tabular Data 3</td>
<td>Tabular Data 4</td>
</tr>
</tbody>
</table>
and the script...
$(function() {
$('th > :checkbox').click(function() {
$(this).closest('table')
.find('td > :checkbox')
.attr('checked', $(this).is(':checked'));
});
});
Try this For HTML table.
<table class="rptcontenttext" style="width: 100%; border-style: solid; border-collapse: collapse"
border="1px" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th style="text-align:left;width:10px;">
<input type="checkbox" value="true" name="chkVerifySelection" id="chkVerifySelection" onchange="return checkAllVerifySelection();" />
</th>
<td class="rptrowdata" align="left">
Employee ID
</td>
</tr>
</thead>
<tbody style="overflow-y: auto; overflow-x: hidden; max-height: 400px; width: 100%;">
@for (int i = 0; i < Model.EmployeeInformationList.Count; i++)
{
<tr>
<td style="width:10px">
@{
if (Model.EmployeeInformationList[i].SelectStatus==true)
{
@Html.CheckBoxFor(model => model.EmployeeInformationList[i].SelectStatus, new { @class = "VerifyStatus" ,disabled = "disabled",@checked="checked" })
}
else
{
@Html.CheckBoxFor(model => model.EmployeeInformationList[i].SelectStatus, new { @class = "VerifyStatus" })
}
}
</td>
<td class="rptrowdata" align="left" style="width: 70px">
@Html.Encode(Model.EmployeeInformationList[i].StrEmpID)
</td>
<td class="rptrowdata" align="center" style="width: 50px">
@Html.HiddenFor(m=>m.EmployeeInformationList[i].strEmpOldCardNo)
@Html.Encode(Model.EmployeeInformationList[i].strEmpOldCardNo)
</td>
</tr>
}
</tbody>
</table>
Script:
function checkAllVerifySelection() {
var flag = $('input[name=chkVerifySelection]').is(':checked');
if (flag) {
$(".VerifyStatus").each(function () {
$(this).attr('checked', true);
});
}
else {
$(".VerifyStatus").each(function () {
$(this).attr('checked', false);
});
}
return true;
}