简体   繁体   English

无法使用angular js按正确的顺序填充下拉列表

[英]Unable to populate dropdowns in correct sequence using angular js

  • I have 3 dropdowns , lets say A,B,C . 我有3个dropdowns ,可以说A,B,C
  • When I select item from dropdown A , then dropdown B is filled, and accordingly c is also filled. 当我从dropdown A选择项目时, dropdown A dropdown B被填充,因此c也被填充。 (not like cascading read my question fully) (不喜欢级联,请完整阅读我的问题)
  • Based on the dropdown C item selection, more dropdowns will be populated. 根据dropdown C项的选择,将填充更多下拉菜单。 For ex. 对于前。 if i select option 1 from dropdown c , then 3 dropdowns will be populated, if I select option 2, then 2 dropdowns will be populated. 如果我从dropdown c选择option 1 ,则将填充3个下拉菜单,如果我选择选项2,则将填充2个下拉菜单。

So, till point 2 I have achieved, but while implementing point 3 , I am not able to populate dropdowns. 因此,直到实现第2点为止,但是在实现第3点时 ,我无法填充下拉列表。 When I select any option from dropdown c , then entire dropdowns are getting generated. 当我从dropdown c选择任何选项时,就会生成整个下拉菜单。

 var option1Options = ["Men", "women", "kids"]; var option2Options = [ ["m-top", "m-bottom", "m-f0otwear"], ["w-top", "w-bottom", "w-footwear"], ["k-top", "k-bottom", "k-footwear"] ]; var option3Options = [ ["m-Shirt", "mt-shirt"], ["m-jeans", "m-trousers"], ["m-chapals", "m-formal shoes"], ["w-Shirt", "wt-shirt"], ["w-jeans", "w-trousers"], ["w-chapals", "w-formal shoes"] ]; function myCtrl($scope) { $scope.opt1 = ["opt1", "opt2", "opt3"]; $scope.opt2 = ["opt1", "opt2"]; $scope.opt3 = ["opt1", "opt2"]; $scope.opt4 = ["opt1", "opt2"]; $scope.opt5 = ["opt1", "opt2"]; $scope.opt6 = ["opt1", "opt2"]; $scope.opt7 = ["opt1", "opt2"]; $scope.opt8 = ["opt1", "opt2"]; $scope.opt9 = ["opt1", "opt2"]; $scope.opt10 = []; $scope.onOption3Change = function() { $scope.obj = {}; $scope.selectC = $scope.options3.indexOf($scope.option3) } $scope.showDropDownOptions = []; $scope.showDropDownOptions[0] = [2, 3, 6]; $scope.showDropDownOptions[1] = [1, 4]; $scope.showDropDownOptions[2] = [5, 9, 10, 7]; $scope.showDropDownOptions[3] = [8, 10]; //$scope.second[0] = [1]; $scope.obj = {}; $scope.options1 = option1Options; $scope.options2 = []; // we'll get these later $scope.options3 = []; $scope.getOptions2 = function() { $scope.options2 = option2Options[option1Options.indexOf($scope.option1)]; $scope.getOptions3(); }; $scope.getOptions3 = function() { var mergedOptions2 = [].concat.apply([], option2Options) $scope.options3 = option3Options[mergedOptions2.indexOf($scope.option2)]; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div data-ng-app data-ng-controller="myCtrl"> <select data-ng-model="option1" data-ng-options="option for option in options1 " data-ng-change="getOptions2($index)"> </select> <select data-ng-model="option2" data-ng-options="option for option in options2" data-ng-show='options2.length' data-ng-change="getOptions3()"> </select>{{option2}} <select data-ng-model="option3" data-ng-options="option for option in options3" data-ng-show='options3.length' ng-change="onOption3Change()"> </select> <hr> <select ng-if="showDropDownOptions[selectC].indexOf(1) != '-1' && selectC && opt1.length>0" ng-model="obj.selectedName245" ng-options="x for x in opt1"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(2) != '-1' && selectC && opt2.length>0" ng-model="obj.selectedName64" ng-options="x for x in opt2"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(3) != '-1' && selectC && opt3.length>0" ng-model="obj.selectedName321" ng-options="x for x in opt3"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(4) != '-1' && selectC && opt4.length>0" ng-model="obj.selectedName34" ng-options="x for x in opt4"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(5) != '-1' && selectC && opt5.length>0" ng-model="obj.selectedName21" ng-options="x for x in opt5"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(6) != '-1' && selectC && opt6.length>0" ng-model="obj.selectedName12" ng-options="x for x in opt6"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(7) != '-1' && selectC && opt7.length>0" ng-model="obj.selectedName222" ng-options="x for x in opt7"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(8) != '-1' && selectC && opt8.length>0" ng-model="obj.selectedName22" ng-options="x for x in opt8"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(9) != '-1' && selectC && opt9.length>0" ng-model="obj.selectedName2" ng-options="x for x in opt9"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(10) != '-1' && selectC && opt10.length>0" ng-model="obj.selectedNamelast" ng-options="x for x in opt10"> <option>opt10</option> </select> Test Model: {{obj}} </div> 


See Fiddle here 在这里看到小提琴

Your problem is with these lines 你的问题是这些线

<select ng-if="showDropDownOptions[selectC].indexOf(10) != '-1' && selectC && opt10.length>0" ng-model="obj.selectedNamelast" ng-options="x for x in opt10">

On the third dropdown, when you select the first option selectC will be set to 0 which is a falsey value meaning the && selectC will make it so that the select will not be shown 在第三个下拉列表中,当您选择第一个选项时, selectC将被设置为0 ,这是一个假数值,这意味着&& selectC将使其&& selectC ,从而不会显示选择

you can change this to be selectC != null and change the function to be 您可以将其更改为selectC != null并将函数更改为

$scope.getOptions3 = function() {
    $scope.selectC = null;
    var mergedOptions2 = [].concat.apply([], option2Options)
    $scope.options3 = option3Options[mergedOptions2.indexOf($scope.option2)];
}

 var option1Options = ["Men", "women", "kids"]; var option2Options = [ ["m-top", "m-bottom", "m-f0otwear"], ["w-top", "w-bottom", "w-footwear"], ["k-top", "k-bottom", "k-footwear"] ]; var option3Options = [ ["m-Shirt", "mt-shirt"], ["m-jeans", "m-trousers"], ["m-chapals", "m-formal shoes"], ["w-Shirt", "wt-shirt"], ["w-jeans", "w-trousers"], ["w-chapals", "w-formal shoes"] ]; function myCtrl($scope) { $scope.opt1 = ["opt1", "opt2", "opt3"]; $scope.opt2 = ["opt1", "opt2"]; $scope.opt3 = ["opt1", "opt2"]; $scope.opt4 = ["opt1", "opt2"]; $scope.opt5 = ["opt1", "opt2"]; $scope.opt6 = ["opt1", "opt2"]; $scope.opt7 = ["opt1", "opt2"]; $scope.opt8 = ["opt1", "opt2"]; $scope.opt9 = ["opt1", "opt2"]; $scope.opt10 = []; $scope.onOption3Change = function() { $scope.obj = {}; $scope.selectC = $scope.options3.indexOf($scope.option3) } $scope.showDropDownOptions = []; $scope.showDropDownOptions[0] = [2, 3, 6]; $scope.showDropDownOptions[1] = [1, 4]; $scope.showDropDownOptions[2] = [5, 9, 10, 7]; $scope.showDropDownOptions[3] = [8, 10]; //$scope.second[0] = [1]; $scope.obj = {}; $scope.options1 = option1Options; $scope.options2 = []; // we'll get these later $scope.options3 = []; $scope.getOptions2 = function() { $scope.options2 = option2Options[option1Options.indexOf($scope.option1)]; $scope.getOptions3(); }; $scope.getOptions3 = function() { $scope.selectC = null; var mergedOptions2 = [].concat.apply([], option2Options) $scope.options3 = option3Options[mergedOptions2.indexOf($scope.option2)]; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div data-ng-app data-ng-controller="myCtrl"> <select data-ng-model="option1" data-ng-options="option for option in options1 " data-ng-change="getOptions2($index)"> </select> <select data-ng-model="option2" data-ng-options="option for option in options2" data-ng-show='options2.length' data-ng-change="getOptions3()"> </select>{{option2}} <select data-ng-model="option3" data-ng-options="option for option in options3" data-ng-show='options3.length' ng-change="onOption3Change()"> </select> <hr> <select ng-if="showDropDownOptions[selectC].indexOf(1) != '-1' && selectC != null && opt1.length>0 " ng-model="obj.selectedName245" ng-options="x for x in opt1"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(2) != '-1' && selectC != null && opt2.length>0" ng-model="obj.selectedName64" ng-options="x for x in opt2"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(3) != '-1'&& selectC != null && opt3.length>0" ng-model="obj.selectedName321" ng-options="x for x in opt3"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(4) != '-1' && selectC != null && opt4.length>0" ng-model="obj.selectedName34" ng-options="x for x in opt4"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(5) != '-1' && selectC != null && opt5.length>0" ng-model="obj.selectedName21" ng-options="x for x in opt5"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(6) != '-1' && selectC != null && opt6.length>0" ng-model="obj.selectedName12" ng-options="x for x in opt6"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(7) != '-1' && selectC != null && opt7.length>0" ng-model="obj.selectedName222" ng-options="x for x in opt7"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(8) != '-1' && selectC != null && opt8.length>0" ng-model="obj.selectedName22" ng-options="x for x in opt8"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(9) != '-1' && selectC != null && opt9.length>0" ng-model="obj.selectedName2" ng-options="x for x in opt9"> </select> <select ng-if="showDropDownOptions[selectC].indexOf(10) != '-1' && selectC != null && opt10.length>0" ng-model="obj.selectedNamelast" ng-options="x for x in opt10"> <option>opt10</option> </select> Test Model: {{obj}} </div> 

JSFiddle JSFiddle

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

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