2016-05-04 47 views
0

我試圖實現,當用戶點擊一個複選框時,它顯示ng-repeat中所有產品的數量爲0.否則,當複選框不勾選所有項目時顯示。目前我能夠獲得一半的功能。Stuck on angularjs ng-checked

複選框:

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

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

控制器

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

回答

0

我建議改變你的複選框,控制器執行:

複選框

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

- 在這裏,我們用 「NG-模式」 而不是 「NG-檢查」 和「vm.checked 「是您定義爲複選框的ng模型的變量。

控制器

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

- 在這裏,我們定義了您正在使用您的表中的「NG重複」過濾器。

+0

這消除了在支票上的所有對象和取消選中的地方他們回來 –

+0

編輯我的答案。我忘記了包含「項目」有一個輸入到搜索功能 – vlin

+0

我的錯誤的錯字謝謝 –

0

試試這個:

<tr ng-repeat="item in vm.items" ng-show="onoffswitch==true && item.onHandQty != 0? false : true"> 
    <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> 

如果onoffswitch爲真,並且onHandQty爲零,則只顯示該行,否則將顯示該行。因此,如果onoffswitch爲真並且onHandQty不爲零,則該行將被隱藏。

0

您可以使用ng-changeng-model

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

然後,你checkBoxChange功能,適用於您的業務邏輯:

vm.checkBoxChange = function(checked){ 
    if(checked){ 
     //Do something 
    }else{ 
    //Something else 
    } 
};