如何在angularjs中使用checkbox过滤器?

时间:2015-07-27 13:24:31

标签: javascript angularjs checkbox

我想根据选择选中复选框。假设在这个例子中我有四个逻辑操作想要动态地做。

  1. 如果选择显示全部(选中),则只需选择     自动。

  2. 如果取消选中全部显示,我必须删除所有选中的未选中。
        如果Show Activated&显示在

  3. 已激活已选中(已选中) - 显示全部应自动获取 选择。

  4. 如果删除了任何一个(未选中) - 显示已激活或显示 激活) - 显示全部应自动取消选择。

  5. 注意:默认情况下显示所有选定的

    这里附有源代码。

    $scope.objectTypes = ['Show Activated', 'Show InActivated', 'Show All'];    
    $scope.selected = ['Show All'];   
    $scope.objectSelection = function objectSelection(objectType) {
             var idx = $scope.selected.indexOf(objectType);  
    
    
              // is currently selected
             for(var i=0; i<$scope.objectTypes.length;i++) {
             if (idx > -1) {
                    if($scope.objectTypes[i]==objectType) {
                          $scope.selected.splice(idx, 1);
                          $scope.selected.splice(1, 1); 
                      }          
             }       
             else if($scope.selected!=0) {      
                    if($scope.objectTypes[i]==$scope.selected) {
                          $scope.selected.push(objectType);
                          if(!$scope.selected["Show All"])
                          $scope.selected.push("Show All");
                    }            
               } 
              else {
                  $scope.selected.push(objectType);
                  break;
               }
             }
        };
    

    HTML代码:

    <div class="row">
        <div class="col-sm-6">
            <div class="panel panel-primary">
                <div class="panel-heading">Object Information</div>
                <div class="panel-body col-sm-offset-2">
                    <div class="form-group">
                        <div class="row">
                            <label for="objectName" class="col-sm-3">Select Object
                                Types</label>
                            <div class="col-sm-4">
                                <ul class="list-unstyled">
                                    <li ng-repeat="objectType in objectTypes">
                                            <div class="checkbox">
                                              <label>
                                                <input type="checkbox" name="selectedObjectTypes[]" value="{{objectType}}" ng-checked="selected.indexOf(objectType) > -1" ng-click="objectSelection(objectType)"> {{objectType}}
                                              </label>
                                            </div>
                                        </li>
                                </ul>
                                <button class="k-button" ng-click="objectFilterSubmit()">OK</button>
                                <button class="k-button" ng-click="objectFilterCancel()">Cancel</button>
                                 <br>Selected: <b>{{ selected | json }}</b>
                            </div>                      
                        </div>                  
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    根据链接 - http://plnkr.co/edit/eCRsfwMEEYX5iGsraqjd?p=preview
     它现在正在运作。非常感谢你。

    现在我有另外一个基于此的要求。

    如何在数组中获取所选值并需要执行过滤, 在我手动检查的每个选定值的源代码下面,但我需要动态检查 请任何人帮助实现这一目标。

    $scope.objectFilterSubmit = function () {    
             $scope.selectedObject = [];
            if($scope.selected.length>1) {
                 $scope.IsActive = "";
                 $scope.selectedObjectDatas = $scope.getCustomObjectsBasedOnObjectTypeSelection($scope.IsActive);
                 clearPartialElement();
                 $scope.objectCreationPage();
            } else {
                angular.forEach($scope.selected, function(value,key){
                    if(angular.equals(value, "Show Activated")) {   
                       $scope.IsActive = "true";   
                       $scope.selectedObjectDatas = $scope.getCustomObjectsBasedOnObjectTypeSelection($scope.IsActive);
                       clearPartialElement();
                       $scope.objectCreationPage();  
                    }   
                    else if(angular.equals(value, "Show InActivated")) {    
                         $scope.IsActive = "false";              
                         $scope.selectedObjectDatas = $scope.getCustomObjectsBasedOnObjectTypeSelection($scope.IsActive);
                         clearPartialElement();
                         $scope.objectCreationPage();   
                    }
                    else {
                         $scope.IsActive = "";
                         $scope.selectedObjectDatas = $scope.getCustomObjectsBasedOnObjectTypeSelection($scope.IsActive);
                         clearPartialElement();
                         $scope.objectCreationPage();
                    }
                });
           }
          };
    

0 个答案:

没有答案