gpt4 book ai didi

angularjs - 处理具有相同数据的动态下拉列表

转载 作者:行者123 更新时间:2023-12-02 10:20:13 26 4
gpt4 key购买 nike

我有 2 个下拉菜单,其中 1 个下拉菜单是隐藏的,从第一个选择框中选择一个选项后将显示

HTML 代码:

<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>

Angular 代码:

$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);
}

对于选择一个属性和子属性,效果很好。 如果我第二次单击“添加属性”,第一个下拉列表和子下拉列表中的值将为空。这部分需要一些帮助

最佳答案

我采取了一些自由措施,通过删除冗余代码和范围对象并使功能在本质上更加动态,对整个代码进行了一些实现更改。这是一个工作 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>

我希望这能解决您的问题。

关于angularjs - 处理具有相同数据的动态下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com