Hoe kan ik de uitvoering van een "Select All" checkbox in mijn ASP.NET MVC app?

stemmen
9

Ik heb een lijst met een kolom vol met vakjes. Op de top zou ik graag één hebben Select All checkbox dat alle vakjes op die pagina zou controleren.

Hoe moet ik dit te implementeren? Ik ben met behulp van jQuery als mijn JavaScript-raamwerk als het er toe doet.

De vraag is gesteld op 31/07/2009 om 18:42
bron van user
In andere talen...                            


7 antwoorden

stemmen
4

jQuery ( Bewerkt naar check schakelen / vinkje bij alle):

$(document).ready(function() {
    $("#toggleAll").click(function() {  
      $("#chex :checkbox").attr("checked", $(this).attr("checked"));
    });    
});

De reden waarom ik moest doen een click()kijk dan voor checkedde status is, want als je probeert om " toggle" een checkbox, de checkbox wordt omgeschakeld zal niet zijn gecontroleerd status kan behouden. Op deze manier behoudt de status controleren en effectief schakelt.

HTML:

<input type="checkbox" id="toggleAll" />
<div id="chex">
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
</div>
antwoordde op 31/07/2009 om 18:46
bron van user

stemmen
0

KingNestor:

U bent ook van plan om deze link nodig hebt (als je nog niet hebt ontdekt):

http: //www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysLists ...

antwoordde op 31/07/2009 om 19:00
bron van user

stemmen
1

Terwijl de antwoorden eerder geplaatste zal werken, kom je in de problemen als je meer dan één set vakjes in een enkele pagina.

Dit formaat zal onafhankelijk werken:

<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>

en het script ...

$(function() {
    $('th > :checkbox').click(function() {
        $(this).closest('table')
            .find('td > :checkbox')
            .attr('checked', $(this).is(':checked'));
    });
});
antwoordde op 31/07/2009 om 19:56
bron van user

stemmen
1

Enigszins wijzigen markup uit het antwoord van Marve's (het geven van ID bij de tabel)

Working Demonstratie →

BEWERK:

Bijgewerkte versie waar de checkbox 'selectAll' correct weerspiegelt de toestand van de vakjes. Bijvoorbeeld als u alle vakjes handmatig te selecteren, zal selectAll checkbox automatisch gecontroleerd. Probeer de demo om het gedrag te begrijpen.

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>

Uw jQuery kan zo simpel zijn als dit:

$(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);
            }
        );
    }
 );
antwoordde op 31/07/2009 om 20:05
bron van user

stemmen
10

Dit zal alle afzonderlijke vakjes houden het zelfde als de "check all" één

$("#id-of-checkall-checkbox").click(function() {
    $(".class-on-my-checkboxes").attr('checked', this.checked);
});

Dit zorgt ervoor dat de "check all" één in sync met de vraag of de afzonderlijke vakjes zijn eigenlijk allemaal gecontroleerd of niet

$(".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);
    }
});
antwoordde op 01/08/2009 om 07:49
bron van user

stemmen
0

Probeer deze Voor HTML-tabel.

<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;  
   } 
antwoordde op 23/10/2014 om 11:06
bron van user

stemmen
0

Voor mij, Jeremy's oplossing meestal gewerkt, maar ik moest vervangen .attr met .prop. Anders zodra ik enkele geklikt op een vakje dat het zou stoppen met reageren op de checkbox "master".

in de _Layout.cshtml (master pagina)

$(document).ready(
    manageCheckboxGroup('chkAffectCheckboxGroup', 'checkbox-group')
);

in een verwezen 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 de HTML (Razor) pagina

<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>

BELANGRIJK: ook, in eerste instantie had ik een @foreachlus in de HTML en het werkte niet ..., moet u gebruik maken van een @for (int i = 0; i < Model.Clients.Count(); i++)lus in plaats anders eindig je met een lege lijst in het OnPostAsync().

antwoordde op 06/08/2019 om 23:32
bron van user

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more