dimanche 20 juin 2021

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

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></td>
                            <td></td>
                            <td></td>
                           
                        </tr>
                    </table>

And my angular js code

$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




Aucun commentaire:

Enregistrer un commentaire