Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

javascript - How to handle cascading/chained dropdowns using multiple ng-options

I am trying to get drop downs to only populate if the drop down above them has something selected. I had someone point me in the right starting direction with something like this.

<select class="selectLevel0" ng-model='scope1' ng-change='scope1Change()' 
        ng-options='obj.name for obj in array track by obj.id'>
</select>

<select class="selectLevel1" ng-model="scope2"  ng-change='scope2Change()' 
        ng-options='obj.name for obj in array2 track by obj.id' ng-hide='!scope1'>
</select>

<select class="selectLevel2" ng-model="scope3" ng-change='scope3Change()' 
        ng-options='obj.name for obj in array3 track by obj.id' ng-hide='!scope2'>
</select>

<select class="selectLevel3" ng-model="scope4" ng-change='scope4Change()' 
        ng-options='obj.name for obj in array4 track by obj.id' ng-hide='!scope3'>
</select>

<select class="selectLevel4" ng-model="scope5" 
        ng-options='obj.name for obj in array5 track by obj.id' ng-hide='!scope4'>
</select>

This is fine for the first round - the drop downs below will not populate until the previous one has something selected. The tricky part is I'm trying to figure out if say I have selected all the way down to the 5th one and I go an reselect the 2nd level one. I then want the third level to show up and 4-5 to disappear. I was thinking maybe something like ng_show="!scope2 || !scope3" (or something similar where i pass multiple arguments), but I cannot seem to get something like this working in angular. Is there a better way of dealing with this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would probably try avoid repeating by rearranging the data and tweak the logic a bit. I would just have one select with ng-repeat and my source data will be a 2D array.

Example:-

//It does not matter where i get the data from 
$scope.selects = [[{name:'level11', id:1}, {name:'level12', id:2}, {name:'level13', id:3}], 
    [{name:'level21', id:1}, {name:'level22', id:2}, {name:'level23', id:3}],
    [{name:'level31', id:1}, {name:'level32', id:2}, {name:'level33', id:3}],
    [{name:'level41', id:1}, {name:'level42', id:2}, {name:'level43', id:3}],
    [{name:'level51', id:1}, {name:'level52', id:2}, {name:'level53', id:3}]];

Then i would just keep an array to store respective models:-

$scope.selected = Array.apply(null, { length: $scope.selects.length }).map(Object); 

Then have just one changehandler for all of them, which currently pass index you could even pass data.

 $scope.handleChange = function(index) {
     //reset data for others when a dropdown in selected
     $scope.selected = $scope.selected.map(function(itm, idx){
           return (idx > index) ? {} : itm;
     });

    //This will give you selected value of the current item if you need
    var selected = $scope.selected[index];
    //DO something with the value
  }

And finally my markup would just be:-

<select ng-repeat="select in selects" ng-class="selectLevel{{$index}}"
         ng-change='handleChange($index)' <!-- Handler for Change event pass index of even model -->
         ng-model='selected[$index].value' <!-- Yes this is the model -->
         ng-show='!$index || selected[$index-1].value' <!-- Show only if its previous model has a value -->
         ng-options='obj.name for obj in select track by obj.id'>
</select>

Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...