Onay kutularını dolu bir sütun içeren bir tablo var. En üstte tek bir olduğunu sayfadaki tüm onay kutularını kontrol ediyorum onay kutusunu Tümünü Seç istiyoruz.
Bunu nasıl uygulamalıdır? Bu konularda benim JavaScript framework jQuery kullanıyorum.
Onay kutularını dolu bir sütun içeren bir tablo var. En üstte tek bir olduğunu sayfadaki tüm onay kutularını kontrol ediyorum onay kutusunu Tümünü Seç istiyoruz.
Bunu nasıl uygulamalıdır? Bu konularda benim JavaScript framework jQuery kullanıyorum.
jQuery ( DÜZENLENMİŞ İşaretleri Kaldır / çek geçiş yapmak için):
$(document).ready(function() {
$("#toggleAll").click(function() {
$("#chex :checkbox").attr("checked", $(this).attr("checked"));
});
});
Bir yapmak zorunda nedeni click()daha sonra kontrol checkedsize "dene çünkü eğer statüsünde olan toggle" bir onay kutusu, onay kutusu onun denetlenen durumunu korumaz getirildiğinde. Etkin onay durumunu korur ve bu şekilde geçiş yapılır.
HTML:
<input type="checkbox" id="toggleAll" />
<div id="chex">
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</div>
KingNestor:
Ayrıca (daha önce keşfedilmiş değil ise) bu bağlantıyı ihtiyacımız olacak:
http: //www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysLists ...
Daha önce yayınlanan cevaplar durumda da çalışır tek bir sayfadan onay kutusu birden çok grubu varsa, Sorunlarla edeceğiz.
Bu biçim ne olursa olsun çalışır:
<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>
ve senaryo ...
$(function() {
$('th > :checkbox').click(function() {
$(this).closest('table')
.find('td > :checkbox')
.attr('checked', $(this).is(':checked'));
});
});
Biraz Marve cevabı gelen işaretlemeyi değiştirerek (tabloya kimliği vererek)
DÜZENLE:
'SelectAll' kutusu doğru onay kutularını durumunu yansıtır versiyonunu güncellendi. Örneğin manuel tüm onay kutularını seçerseniz, selectAll onay kutusu otomatik olarak kontrol alacak. davranışlarını anlamak için demo deneyin.
Kod:
<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>
Sizin jQuery bu kadar basit olabilir:
$(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);
}
);
}
);
Bu "tüm kontrol" olarak tüm bireysel onay kutularını aynı tutacak
$("#id-of-checkall-checkbox").click(function() {
$(".class-on-my-checkboxes").attr('checked', this.checked);
});
Bu tutacak olsun veya olmasın bireysel onay kutularını aslında tüm kontrol veya olmayan ile senkronize bir "bütün kontrol"
$(".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);
}
});
HTML tablosunun için bu deneyin.
<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>
Senaryo:
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;
}
Benim için, Jeremy'nin çözüm çoğunlukla çalıştı ama yerine zorunda .attr olan .prop. Ben bir onay kutusu tıklandığında tamamını tek Aksi kez "ana" onay kutusunun tepki durdurmak olacaktır.
_Layout.cshtml (ana sayfası)
$(document).ready(
manageCheckboxGroup('chkAffectCheckboxGroup', 'checkbox-group')
);
Bir başvurulan 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);
}
});
}
HTML (Ustura) sayfasında
<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>
ÖNEMLİ:
Ayrıca, ilk başta bir vardı @foreachHTML döngü ve işe yaramadı ..., bir kullanmalıdır @for (int i = 0; i < Model.Clients.Count(); i++)yerine aksi takdirde bir boş liste ile sona döngü OnPostAsync().