Cum pot pune în aplicare o „Select All“ din caseta mea app ASP.NET MVC?

voturi
9

Am un tabel cu o coloană plină de casete de selectare. In partea de sus, aș dori să aibă un singur „Select All“ caseta de selectare care ar verifica toate casetele de selectare de pe acea pagina.

Cum ar trebui să pună în aplicare acest lucru? Sunt folosind jQuery ca cadru meu JavaScript în cazul în care contează.

Întrebat 31/07/2009 la 18:42
sursa de către utilizator
În alte limbi...                            


7 răspunsuri

voturi
4

jQuery ( EDITATĂ pentru a comuta de verificare / debifa toate):

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

Motivul pentru care am avut de a face o click()atunci verifica checkedstarea este pentru că dacă încercați să „ toggle“ o casetă de selectare, caseta fiind comutată nu va păstra statutul său de verificat. În acest fel se păstrează statutul de verificare și eficient comutã.

HTML:

<input type="checkbox" id="toggleAll" />
<div id="chex">
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
    <input type="checkbox" />
</div>
Publicat 31/07/2009 la 18:46
sursa de către utilizator

voturi
0

KingNestor:

Sunteți de asemenea, va avea nevoie de acest link (dacă nu ați descoperit-o):

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

Publicat 31/07/2009 la 19:00
sursa de către utilizator

voturi
1

În timp ce răspunsurile postate anterior vor lucra, va rula în probleme dacă aveți mai mult de un set de casete de selectare într-o singură pagină.

Acest format va lucra indiferent:

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

și script-ul ...

$(function() {
    $('th > :checkbox').click(function() {
        $(this).closest('table')
            .find('td > :checkbox')
            .attr('checked', $(this).is(':checked'));
    });
});
Publicat 31/07/2009 la 19:56
sursa de către utilizator

voturi
1

modificarea usoara de marcare din răspunsul lui Marve (da ID-ul la masa)

Demo de lucru →

EDITAȚI | ×:

versiune în cazul în care caseta de selectare „selectAll“ reflectă în mod corect starea casetele actualizate. De exemplu, dacă selectați toate casetele de selectare manuală, selectAll caseta de selectare se va face automat verificat. Încercați demo pentru a înțelege comportamentul.

Cod:

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

jQuery dvs. ar putea fi la fel de simplu ca acest lucru:

$(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);
            }
        );
    }
 );
Publicat 31/07/2009 la 20:05
sursa de către utilizator

voturi
10

Acest lucru va păstra toate casetele individuale, la fel ca și cea „verifica toate“

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

Acest lucru va menține „verifica toate“, una în sincronizare cu sau nu casetele individuale sunt de fapt toate verificate sau nu

$(".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);
    }
});
Publicat 01/08/2009 la 07:49
sursa de către utilizator

voturi
0

Încercați acest lucru pentru tabel HTML.

<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;  
   } 
Publicat 23/10/2014 la 11:06
sursa de către utilizator

voturi
0

Pentru mine, soluția Jeremy a lucrat cea mai mare parte, dar a trebuit să -l înlocuiască .attr cu .prop. În caz contrar , o dată ce am unică făcut clic pe o casetă de selectare ar opri reacția la „maestru“ caseta de selectare.

în (pagina principală) _Layout.cshtml

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

într-un .js care se face referire

    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);
        }
    });
}

în pagina HTML (aparat de ras)

<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: De asemenea, la început am avut o @foreachbuclă în HTML și nu a funcționat ..., trebuie să utilizați o @for (int i = 0; i < Model.Clients.Count(); i++)buclă în loc altfel va termina cu o listă goală din OnPostAsync().

Publicat 06/08/2019 la 23:32
sursa de către utilizator

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