2014-02-17 44 views
3

我有一個元素列表,每個元素都有一個複選框。 我添加了一個checkAll按鈕來選擇我所有的元素。如何在搜索結果中只選擇可見元素

http://jsbin.com/dipumemo/3edit

如何提高我的用戶界面,並選擇當我點擊「checkAll」按鈕纔可見元素?

我的工作流程:在過濾器

  1. 追加1(隱藏與顯示:無;表2)
  2. 點擊checkboxAll
  3. 清晰過濾器
  4. 請參閱列表1檢查

enter image description here enter image description here

我的HTML:

<div ng-app="listsModule"> 
    <div ng-controller="listsController"> 
     <input type="text" id="filter_lists" ng-model="search" placeholder="Search a list"> 

     <table> 
      <thead> 
      <tr> 
       <th><input type="checkbox" ng-model="checkAll"/></th> 
       <th>List name</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr ng-repeat="list in lists | filter:search | orderBy:'name'"> 
       <td><input type="checkbox" ng-model="list.isSelected" ng-checked="checkAll"></td> 
       <td>{{ list.name }}</td> 
       <td>{{ list.isSelected }}</td> 
      </tr> 
      </tbody> 
     </table> 
    </div> 
</div> 

我的javascript:

var app = angular.module('listsModule', []); 

app.controller('listsController', function ($scope) { 
    $scope.lists = [ 
     {"id": 39, "name": "list1", "isSelected": false}, 
     {"id": 40, "name": "list2", "isSelected": false} 
    ] 
}); 
+0

它實際上有點矛盾,你看可以嗎?當你清除過濾器並且「全部檢查」被選中時,所有* * ** **應該被檢查。 – dfsq

+0

@dfsq是它是一個問題嗎?我能改善我的問題嗎? –

+0

我的意思是你實際上試圖讓用戶界面變得更糟,因爲如果你成功了,它會使應用程序不一致。 – dfsq

回答

2

更改該列表過濾會給你訪問過濾列表在當前範圍的方式。然後,您可以從該過濾列表中獲取所選項目。將手錶添加到checkAll將讓您切換對已過濾項目的選擇。

HTML:

<div ng-app="listsModule"> 
    <div ng-controller="listsController"> 
     <input type="text" id="filter_lists" ng-model="search" placeholder="Search a list"> 

     <table> 
      <thead> 
      <tr> 
       <th><input type="checkbox" ng-model="checkAll"/></th> 
       <th>List name</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr ng-repeat="list in filtered = (lists | filter:{name:search}) | orderBy:'name'"> 
       <td><input type="checkbox" ng-model="list.isSelected"></td> 
       <td>{{ list.name }}</td> 
       <td>{{ list.isSelected }}</td> 
      </tr> 
      </tbody> 
     </table> 
     <div>Selected: 
      <div ng-repeat="list in filtered | filter:{isSelected:true} | orderBy:'name'"> 
       {{list.name}} 
      </div> 
     </div> 
    </div> 
</div> 

的javascript:

var app = angular.module("listsModule", []); 

app.controller('listsController', ['$scope', function($scope) { 
    $scope.lists = [ 
     {"id": 39, "name": "Fluffy", "isSelected": false}, 
     {"id": 40, "name": "Muffy", "isSelected": false}, 
     {"id": 41, "name": "Dingo Jr.", "isSelected": false}, 
     {"id": 42, "name": "Bertram", "isSelected": false} 
    ]; 

    $scope.$watch('checkAll', function(newValue, oldValue) { 
     if ($scope.filtered) 
     for (i=0; i<$scope.filtered.length; i++) { 
      $scope.filtered[i].isSelected = newValue; 
     } 
    }); 
}]); 
+0

謝謝你的工作就像一個魅力,解決方案很乾淨。我添加了jsbin –

相關問題