2016-11-04 117 views
0

在我的頁面上,我在每個面板中都有角度的ui手風琴,我正在渲染帶有項目和複選框的列表,同時我也有複選框「全選」。 對於選擇方法和邏輯我使用this resource。在這個資源邏輯似乎工作,但是我把這個邏輯到我的代碼,它停止工作。如果所有複選框都選中,請選擇全部,角度js

我想要實現的是當所有複選框被選中時,自動選中複選框「全部選中」,如果某些複選框未被選中,則複選框「全選」也必須取消選中。

我試過多個建議here,here,here,但最後我得到了同樣的結果。

我很感激有人能幫我解決我的問題。

$scope.categories = [ 
{ 
    id: 1, 
    name: "category 1" 
}, 
{ 
    id: 2, 
    name: "category 2" 
}, 
{ 
    id: 3, 
    name: "category 3" 
} 
] 

       $scope.selectedAll = false; 
       $scope.selectAll = function(array) { 
        $scope.selectedAll = !$scope.selectedAll; 
        angular.forEach(array, function(item) { 
         item.Selected = $scope.selectedAll; 
        }); 
       }; 

       $scope.checkIfAllSelected = function(array) { 
        $scope.selectedAll = array.every(function(item) { 
         return item.Selected == true 
        }) 
       }; 

HTML

<div> 
<div class="row" ng-class="{'selected': selectedAll, 'default': !selectedAll}"> 
    <div>Select all 
     <input type="checkbox" 
       ng-model="selectedAll" ng-click="selectAll(categories)" > 
    </div> 
</div> 
<div class="row" ng-repeat="item in categories | orderBy : 'id'" ng-class="{'selected': item.selected, 'default': !item.selected}"> 
    <div > {{ item.name }} 
     <input type="checkbox" 
       ng-model="item.Selected" ng-click="checkIfAllSelected(categories)" 
     > 
    </div> 
</div> 

This is my plunker

+0

在'array.every'函數中嘗試'return item.Selected === true;'。 –

+0

@ Alex.S仍然是一樣的行爲 – antonyboom

回答

1

請看一看這個叉子您plunker的:https://plnkr.co/edit/OW3F1VMke9iLuNkt5p3o?p=preview

兩件事情:
1.這是一個很好PRACTIC e爲您的視圖模型創建一個對象(您可以在plunker $scope.model的名稱模型下找到它。這將解決2種數據綁定問題。
2.我已將ng-click更改爲ng-change(但這不是解決方案的一部分 - 在我看來它更正確)。

如果您需要更多說明,請讓我知道。

+0

model.selectedAll有一個小錯誤。如果選擇全部,則在每個面板中選擇複選框https://plnkr.co/edit/XPdqwAXIVgYDU4rMr5DO?p=preview。但我已經得到了期望的結果,非常感謝! – antonyboom

+0

歡迎您! – vlio20