Search code examples
angularjsangularjs-ng-repeatangular-filters

Stuck on angularjs ng-checked


I'm trying to implement that when a user clicks a check box it displays all products in the ng-repeat with a quantity of 0. Else when the check box is not check all items display. Currently I was able to get half the functionality.

Check box :

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" ng-checked="vm.OnHandQty()">   

Table

    <tr ng-repeat="item in vm.items | filter :search">                                           
        <td ng-bind="item.itemNo"> </td>
        <td ng-bind="item.description"></td>
        <td ng-bind="(item.listPrice | currency)"></td>
        <td ng-bind="item.onHandQty" ng-model="quantity"></td>
    </tr>

Controller

    vm.OnHandQty = function () {               
    $scope.search = {};

    vm.items.forEach(i => {
        if (i.onHandQty == 2) {
            console.log(i);
            $scope.search = i;
            return true;
        }
        else {
            $scope.search =i;
            return false;
        }
    });
}

Solution

  • I would propose changing the implementation of your checkbox and controller:

    Checkbox

    <input type="checkbox" ng-model="vm.checked" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
    

    - Here, we use "ng-model" instead of "ng-checked" and "vm.checked" is a variable that you define as the ng-model for your checkbox.

    Controller

    $scope.search = function(item) {
        return ($scope.vm.checked) ? item.onHandQuantity === 0: true;
    };
    

    - Here, we define the "ng-repeat" filter that you are using in your table.