简体   繁体   English

复选框选中/取消选中不能正常使用 angularjs

[英]Checkbox check/uncheck not working properly with angularjs

I am working on a project where I am using a partial view in ASP.NET MVC and in that partial view there is a html table and in the table header there is a checkbox on click of that checkbox all the checkboxes besides the table body data should be checked and if from table body data any checkbox is unchecked then top header checkbox should be also unchecked.我正在开发一个项目,我在 ASP.NET MVC 中使用局部视图,在该局部视图中有一个 html 表,在表 header 中,单击该复选框时有一个复选框,除了表体数据外,所有复选框都应该是选中,如果从表体数据中取消选中任何复选框,则顶部 header 复选框也应取消选中。 This functionality is working with my current code but it is working only once second time if I uncheck top header checkbox and check it again and try to uncheck any table body data checkbox top header checkbox is not getting unchecked I am not able to figure out why this is not working此功能适用于我当前的代码,但如果我取消选中顶部 header 复选框并再次选中它并尝试取消选中任何表体数据复选框顶部 header 复选框未取消选中我无法弄清楚为什么这是行不通的

Here is my code这是我的代码

<table class="table table-condensed table-bordered table-hover left" style="width:100%;margin:0px;">
    <thead>
        <tr>
            <th>
                <input type="checkbox" style="margin: 3px 0;" ng-model="IsAllChecked" ng-change="CheckUncheckAll(IsAllChecked)" ng-checked="isChecked"/>
            </th>
            <th><label>Name</label></th>
            <th><label>City</label></th>
            <th><label>Country</label></th>
        </tr>
    </thead>

    <tr ng-repeat="clientData in $Clients.Clients| orderBy:'Name'|limitTo: totalDisplayed | filter: searchClient"
        ng-mouseover="MouseOverOnUnassigned(clientData );" ng-mouseleave="MouseLeaveOnUnassigned(clientData)">

        <td>
 
            <input type="checkbox" ng-model="clientData.IsSelectedClients" ng-change="CheckUncheckHeader(clientData.IsSelectedClients)" />
        </td>
        <td>{{clientData.Client}}</td>
        <td>{{clientData.City}}</td>
        <td>{{clientData.Country}}</td>       
    </tr>
</table>

And my angular js code还有我的 angular js 代码

$scope.CheckUncheckHeader = function (checked) {
    $scope.isChecked = true;
    for (var i = 0; i < $scope.$Client.Clients.length; i++) {
        if (!$scope.$Client.Clients[i].IsSelectedClients) {
            $scope.isChecked = false;
            break;
        }
    };
};

$scope.CheckUncheckAll = function (IsAllChecked) {
    for (var i = 0; i < $scope.$Client.Clients.length; i++) {
        $scope.$Client.Clients[i].IsSelectedClients= IsAllChecked;
    }
};

Can anybody tell what is the best way to achieve this without failing.任何人都可以告诉什么是实现这一目标而不会失败的最佳方法。 Thanks in advance提前致谢

Please, try with below code.请尝试使用以下代码。 Good Luck祝你好运

$scope.CheckUncheckAll = function (IsAllChecked) {
    for (var i = 0; i < $scope.$Client.Clients.length; i++) {
        if(IsAllChecked == true) 
        {
            $scope.$Client.Clients[i].IsSelectedClients= true;
            $scope.isChecked = true;
        } else 
        {
           $scope.$Client.Clients[i].IsSelectedClients= false;
           $scope.isChecked = false;
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM