如果从复选框中选择多个值,则打印“多个”

时间:2017-10-06 19:47:57

标签: angularjs

如果用户选择了多个复选框,则应打印“多个值”。现在它显示了用户选择的所有值。我正在尝试if else条件与array.length(如果array.length> 1),但它不适用于我的情况。下面是HTML和AngularJS代码

<div class="btn-group acc-vel-env">
                <div class="btn-group metric-dropdown" uib-dropdown is-open="status.isopen">
                  <button id="metricDropdown" type="button" class="btn" uib-dropdown-toggle ng-disabled="disabled">
                     {{metricObject.selectedMetrics.toString().toUpperCase()}}<span class="caret"></span>
                  </button>
                  <ul class="dropdown-menu metric-dropdown-menu" uib-dropdown-menu role="menu"
                        aria-labelledby="metricDropdown" ng-click="$event.stopPropagation()">
                        <li ng-repeat="metricItem in metricObject.metricGroups">
                            <div class="checkbox">
                                <input type="checkbox" id="{{metricItem.id}}" name="{{metricItem.name}}" 
                                    ng-checked="metricObject.selectedMetrics.indexOf(metricItem.id) > -1"
                                    ng-value="metricItem.id" ng-click="setSelectedMetrics(metricItem.id)"> 
                                    <label for="{{metricItem.id}}">{{metricItem.name}}</label>
                            </div>

Angular JS: -

      if(metricId === 'all'){
        mId = $scope.metricObject.selectedMetrics.indexOf(metricId);
        $scope.metricObject.selectedMetrics = [];

        if (mId > -1) {
            $scope.metricObject.selectedMetrics = [];
            $scope.metricObject.selectedMetrics.push('c1');

        }
        else {
            $.each($scope.metricObject.metricGroups, function(i,metricObj){
                $scope.metricObject.selectedMetrics.push(metricObj.id);
            });
        }
    } 

    else {
        mId = $scope.metricObject.selectedMetrics.indexOf(metricId);            
        if (mId > -1) {
            $scope.metricObject.selectedMetrics.splice(mId, 1);
        } else {
            $scope.metricObject.selectedMetrics.push(metricId);
        }
    }

    $timeout(function(){
        $scope.showTimeSeries(metricId);
    });
}

0 个答案:

没有答案