简体   繁体   English

基于以前的下拉Angularjs的选择在下拉列表中混排项目的问题

[英]issue in shuffling items in drop down based on selection of previous dropdown angularjs

I am trying to shuffles items in second drop down based on the selection of item in previous drop down , every thing is working fine but the problem is it do not change value in parent drop down but adds correct value to its child drop down. 我正在尝试根据上一个下拉列表中的项目选择对第二个下拉列表中的项目进行洗牌,一切正常,但问题是它不会更改父下拉列表中的值,而是为其子下拉列表添加了正确的值。

here is my Html for dynamic drop down :- 这是我的动态下拉列表HTML:

 <div class="row" > <div class="col col-100 prod_det"> <div class="desc select_prod"> <div class="prod_col"> <div class="col_list cont_padd" ng-repeat="item in configurableDataList"> <label class="item item-input item-select"> <div class="input-label"> <b>{{item.label}}</b> </div> <select id = "sel" class="form-control input-lg" ng-init="initSelection($index,item)" ng-model="subjectSelectedId[$index]" required="required" ng-change="selectedSize(subjectSelectedId[$index],item.id )"> <option ng-repeat="option in item.options" selected="selected" value="{{option}}">{{option.label}}</option> <option value="" disabled hidden>select</option> </select> </label> </div> </div> </div> </div> </div> 

Here is my java script code :- 这是我的Java脚本代码:-

 $scope.subjectSelectedId = []; $scope.subjectSelected = []; $scope.initSelection = function(index, item) { $scope.subjectSelected.push(item); $scope.subjectSelectedId.push(''); } var UserSelectedArr = []; var isSelectedFirstTime = true; var btatrue = false; var dummyArr = []; $scope.selectedSize = function(value, attributeId) { var Sizedata = angular.fromJson(value); var sizedataArray = []; sizedataArray.push(Sizedata); if (dummyArr.length > 0) { check(sizedataArray, dummyArr); } function check(sizedataArray, dummyArr) { angular.forEach(sizedataArray, function(value1, key1) { angular.forEach(dummyArr, function(value2, key2) { alert("dummyArr" + value2.id + " " + "Sizedata" + value1.id); if (value1.id === value2.id) { btatrue = true; } else { btatrue = false; } }); }); } if (btatrue) { return; } $scope.configurableDataList = LocalStorage .getObject("configurableDataListFull"); for ( var i = 0; i < $scope.configurableDataList.length; i++) { if (attributeId != $scope.configurableDataList[i].id) { if (dummyArr.length > 0) { dummyArr = []; } for ( var j = 0; j < Sizedata.products.length; j++) { for ( var k = 0; k < $scope.configurableDataList[i].options.length; k++) { for ( var l = 0; l < $scope.configurableDataList[i].options[k].products.length; l++) { alert(JSON .stringify(Sizedata.products[j])); alert(JSON .stringify($scope.configurableDataList[i].options[k].products[l])); if (Sizedata.products[j] == $scope.configurableDataList[i].options[k].products[l]) { dummyArr .push($scope.configurableDataList[i].options[k]); $scope.configurableDataList[i].options = dummyArr; alert("check :" + JSON .stringify($scope.configurableDataList[i].options)); return; } else { alert("chak 250 :") } } } } console.log('dummyArr :', dummyArr); } else { if (isSelectedFirstTime) { console.log('abc'); UserSelectedArr = $scope.configurableDataList[i].options; isSelectedFirstTime = false; } } } 

Here is my json :- http://piratepad.net/ep/pad/view/ro.5GeXFI9Xy2P/latest 这是我的json:-http: //piratepad.net/ep/pad/view/ro.5GeXFI9Xy2P/latest

what i need is that when i select black the other show dropdown contain only value corresponding to black based on condition which in case is "S" ie small and in case of blue shows me "XL".Although its working fine but when i select black then it always shows me blue in parent dropdown but my child drop down do acquire "S" in it.The problem is that how can i make parent drop down select black. 我需要的是,当我选择黑色时,其他显示下拉列表仅包含基于条件的黑色值,在这种情况下,条件是“ S”,即很小,在蓝色情况下,则显示“ XL”。黑色,则在父下拉列表中始终显示蓝色,但我的孩子下拉列表中确实显示“ S”。问题是如何使父下拉列表选择黑色。

I tried to make plunker as i am new i am not able to ..but i put all the required code in it. 我试着做点小矮人,因为我是新手,但是我无法将所有必需的代码放进去。

In short please provide me way to get $index in :- 简而言之,请提供给我获取$ index的方法:

 <option ng-repeat="option in item.options" selected="selected" value="{{option}}">{{option.label}}</option> 

plunker :- http://plnkr.co/edit/PUKaIRUApSQu4qLqtD2e?p=preview plunker: - http://plnkr.co/edit/PUKaIRUApSQu4qLqtD2e?p=preview

I tried with following approach but its does not provide me corresponding option in child drop down :- 我尝试了以下方法,但在子下拉菜单中没有为我提供相应的选项:

  selected="selected" ng-options="item.options.indexOf(subjectSelectedId[$index]) as subjectSelectedId[$index] for subjectSelectedId[$index] in item.options"> <option value="" disabled hidden>select</option> 

And with :- 与 :-

 <option ng-selected="subjectSelectedId[$parent.$index] == option.label" ng-repeat="option in item.options" value="{{option}}" selected="selected">{{option.label}}</option> <option value="" disabled hidden>select</option> 

Any help will be appreciated 任何帮助将不胜感激

Thanks 谢谢

Finally I am done ..I solved it by using ng-options :- 最后,我完成了..我通过使用ng-options解决了它:

Here is my solution :- 这是我的解决方案:

 <select class="form-control input-lg" ng-init="initSelection($index,item)" ng-model="subjectSelectedId[$index]" required="required" ng-change="selectedSize(subjectSelectedId[$index],item.id )" ng-selected="subjectSelectedId[$parent.$index] == option.id" ng-options="option.label for option in item.options track by option.id"> <option value="" disabled hidden>Please select</option> </select> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM