使用相同数据处理动态下拉列表

时间:2016-09-17 06:49:57

标签: angularjs angularjs-ng-repeat ng-options

我有2个下拉列表,其中隐藏了1个下拉列表,从第一个选择框中选择一个选项后会显示该下拉列表

HTML CODE:

<div ng-repeat="choice in exSize">
            <label>Attribute {{$index+1}} </label>
            <div id="select">
              <select ng-model="choice.attributeid" ng-options = "a.attributeid as a.attributename for a in modifyList" ng-change="changeSelect(choice.attributeid)" class="minimal" required>
                <option value="">Please Select An Attrtibute</option>
              </select>
              <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSize($index)">+</span></span> Add Attribute<span ng-hide="exSize.length==1">/</span></label>
              <label class="extra extra_remove" ng-hide="exSize.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSize($index)">-</span></span> Remove</label>
            </div>
            <!--  &&&&  ng-if="choice.attributeid" &&&& -->
            <!-- <div ng-repeat="(index1,sub) in subSize" ng-if="choice.attributeid"> -->
            <div ng-repeat="sub in afterChangeAttr">
              <div id="select" ng-if="choice.attributeid">
                <div>
                  <div ng-show="afterChangeAttr.length ==1">
                    <select ng-model="sub.values" ng-options="v.attributevalue as v.attributevalue for v in hasArr" style="width:17%;margin-top:10px;">
                      <option value="">Select</option>
                    </select>
                    <input type="text" ng-model="sub.attributeprice" style="width:35%;" name="subAttr" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
                    <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSubSize(choice.attributeid)">+</span></span> Add<span ng-hide="afterChangeAttr.length==1 ||afterChangeAttr.length==5">/</span></label>
                    <label class="extra extra_remove" ng-hide="afterChangeAttr.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSubSize($index)">-</span></span> Remove</label>
                  </div>
                  <div ng-show="afterChangeAttr.length ==2">
                    <select ng-model="sub.values" ng-options="v.attributevalue as v.attributevalue for v in hasArr" style="width:17%;margin-top:10px;">
                      <option value="">Select</option>
                    </select>
                    <input type="text" ng-model="sub.attributeprice" style="width:35%;" name="subAttr" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
                    <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSubSize(choice.attributeid)">+</span></span> Add<span ng-hide="afterChangeAttr.length==1">/</span></label>
                    <label class="extra extra_remove" ng-hide="afterChangeAttr.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSubSize($index)">-</span></span> Remove</label>
                  </div>
                  <div ng-show="afterChangeAttr.length ==3">
                    <select ng-model="sub.values" ng-options="v.attributevalue as v.attributevalue for v in hasArr" style="width:17%;margin-top:10px;">
                      <option value="">Select</option>
                    </select>
                    <input type="text" ng-model="sub.attributeprice" style="width:35%;" name="subAttr" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
                    <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSubSize(choice.attributeid)">+</span></span> Add<span ng-hide="afterChangeAttr.length==1">/</span></label>
                    <label class="extra extra_remove" ng-hide="afterChangeAttr.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSubSize($index)">-</span></span> Remove</label>
                  </div>
                  <div ng-show="afterChangeAttr.length ==4">
                    <select ng-model="sub.values" ng-options="v.attributevalue as v.attributevalue for v in hasArr" style="width:17%;margin-top:10px;">
                      <option value="">Select</option>
                    </select>
                    <input type="text" ng-model="sub.attributeprice" style="width:35%;" name="subAttr" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
                    <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSubSize(choice.attributeid)">+</span></span> Add<span ng-hide="afterChangeAttr.length==1">/</span></label>
                    <label class="extra extra_remove" ng-hide="afterChangeAttr.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSubSize($index)">-</span></span> Remove</label>
                  </div>
                  <div ng-show="afterChangeAttr.length ==5">
                    <select ng-model="sub.values" ng-options="v.attributevalue as v.attributevalue for v in hasArr" style="width:17%;margin-top:10px;">
                      <option value="">Select</option>
                    </select>
                    <input type="text" ng-model="sub.attributeprice" style="width:35%;" name="subAttr" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
                    <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSubSize(choice.attributeid)">+</span></span> Add<span ng-hide="afterChangeAttr.length==1">/</span></label>
                    <label class="extra extra_remove" ng-hide="afterChangeAttr.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSubSize($index)">-</span></span> Remove</label>
                  </div>
                </div>
                <div ng-messages="addMenu.subAttr.$error" class="error" role="alert" ng-if="addMenu.subAttr.$dirty">
                 <div ng-message="pattern">Please Enter Only Numbers</div>
                  <div ng-message="required">Sales Price Is Mandatory</div>
                </div>
              </div>
            </div>
          </div>

角色代码:

$scope.hasArr = [];
$scope.hasArr1 = [];
$scope.hasArr2 = [];
$scope.hasArr3 = [];
$scope.hasArr4 = [];
$scope.changeSelect = function(id){
    $scope.sizeId = id;
    angular.forEach($scope.modifyList,function(key,value){
        if(key.attributeid === id){
            for (var i = 0; i < key.attribuevaluelist.length; i++) {
                $scope.hasArr[i]= key.attribuevaluelist[i].attributevalue;
                $scope.hasArr1[i]= key.attribuevaluelist[i].attributevalue;
                $scope.hasArr2[i]= key.attribuevaluelist[i].attributevalue;
                $scope.hasArr3[i]= key.attribuevaluelist[i].attributevalue;
                $scope.hasArr4[i]= key.attribuevaluelist[i].attributevalue;
            };
        }
    })
    /*for (var i = 0; i < $scope.exSize.length; i++) {
        if($scope.exSize.length == 1){
            console.log('inside if');
            $scope.hasArr[1] = $scope.hasArr;
        }
        if($scope.exSize.length == 2){
            console.log('inside 2nd if');
            $scope.hasArr1[i] = $scope.hasArr1;
            console.log($scope.hasArr1);
        }
    };*/
    for (var i = 0; i < $scope.exSize.length; i++) {
        if($scope.exSize.length==1){
            $scope.hasArr = $scope.modifyList[i].attribuevaluelist;
        }
        if($scope.exSize.length ==2){
            $scope.hasArr1 = $scope.modifyList[1].attribuevaluelist;
        }
        if($scope.exSize.length == 3){
            $scope.hasArr2 = $scope.modifyList[2].attribuevaluelist;
        }
        if($scope.exSize.length == 4){
            $scope.hasArr3 = $scope.modifyList[3].attribuevaluelist;
        }
        if($scope.exSize.length == 5){
            $scope.hasArr4 = $scope.modifyList[4].attribuevaluelist;
        }
    };
    /*console.log($scope.hasArr);
    console.log($scope.hasArr1);
    console.log($scope.hasArr2);
    console.log($scope.hasArr3);
    console.log($scope.hasArr4);*/
}
$scope.addSize = function(index){
    /*$scope.choiceArr.push($scope.choice);
    $scope.subSizeArr.push($scope.subSize);
    $scope.finalArr.push({"attributeid":$scope.choiceArr,"attributevaluelist":$scope.subSizeArr});
    console.log($scope.subSize);
    console.log($scope.choiceArr);
    console.log($scope.subSizeArr);*/
    /*$scope.finalArr.push({"attributeid":$scope.sizeId,"attributevaluelist":$scope.subSize});*/
    $scope.exSize.push({"index":index});

}
$scope.afterChangeAttr4 = [{}];
$scope.addSubSize = function(id){
    //$scope.sizeId = id;
    $scope.afterChangeAttr.push({});
    /*$scope.hasArr.push({});*/
    console.log('id'+id);
}

为了选择一个属性和子属性,它可以正常工作。  如果我第二次单击add属性,则第一个下拉列表和子下拉列表中的值为null。在这部分需要一些帮助

1 个答案:

答案 0 :(得分:0)

我采取了一些自由,并通过删除冗余代码和范围对象并使功能更具动态性,对整个代码进行了一些实现更改。对于我所做的更改,这是一个有效的plunker 。现在还可以改进,但我刚刚回答了你的问题。

您的changeSelect功能看起来像

$scope.changeSelect = function(id, index) {
            $scope.sizeId = id;
            angular.forEach($scope.modifyList, function(key, value) {
                    if (key.attributeid === id) {
                        $scope.hasArr[index] = key.attribuevaluelist;
}
})
};

您的HTML已更改为

<div ng-repeat="choice in exSize track by $index">
        <label>Attribute {{$index+1}} </label>
        <div id="select">
            <select ng-model="choice.attributeid" ng-options="a.attributeid as a.attributename for a in modifyList" ng-change="changeSelect(choice.attributeid,$index)" class="minimal" required>
                <option value="">Please Select An Attrtibute</option>
            </select>
            <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSize($index)">+</span></span> Add Attribute<span ng-hide="exSize.length==1">/</span></label>
            <label class="extra extra_remove" ng-hide="exSize.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSize($index)">-</span></span> Remove</label>
        </div>
        <!--  &&&&  ng-if="choice.attributeid" &&&& -->
        <!-- <div ng-repeat="(index1,sub) in subSize" ng-if="choice.attributeid"> -->
        <div ng-repeat="sub in afterChangeAttr track by $index">
            <div id="select" ng-show="choice.attributeid">
                <div>
                    <div>

                        <select ng-model="sub.values[$parent.$index]" ng-options="v.attributevalue as v.attributevalue for v in hasArr[$parent.$index]" style="width:17%;margin-top:10px;">
                            <option value="">Select</option>
                        </select>
                        <input type="text" ng-model="sub.attributeprice" style="width:35%;" name="subAttr" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/">
                        <label class="extra"><span class="plusCircle"><span id="addTaxBtn" ng-click="addSubSize(choice.attributeid)">+</span></span> Add<span ng-hide="afterChangeAttr.length==1 ||afterChangeAttr.length==5">/</span></label>
                        <label class="extra extra_remove" ng-hide="afterChangeAttr.length==1"><span class="plusCircle"><span id="addTaxBtn" ng-click="rmSubSize($index)">-</span></span> Remove</label>
                    </div>

                </div>
                <div ng-messages="addMenu.subAttr.$error" class="error" role="alert" ng-if="addMenu.subAttr.$dirty">
                    <div ng-message="pattern">Please Enter Only Numbers</div>
                    <div ng-message="required">Sales Price Is Mandatory</div>
                </div>
            </div>
        </div>
    </div>

我希望这可以解决您的问题。