AngularJS remove & add select box option if other selection changes

后端 未结 3 425
囚心锁ツ
囚心锁ツ 2021-01-06 10:55

I\'m looking for a similar solution to a problem that has been resolved before with the following code:

http://jsfiddle.net/yLCVf/1/

This is the JS that I\'m

3条回答
  •  广开言路
    2021-01-06 11:32

    Here is the plunker example of what you are looking for. Select list changes automatically based on your other selection.

    http://plnkr.co/edit/yFrYQ1ql9a1x9jd9yGv0

    You can generalize for n number of list and just loop through all of them for any changes.

        
    
    
      
        
        Custom Plunker
        
        
      
    
      
        

    Hello {{name}}

    First List: {{first.option.name}}

    Second List: {{second.option.name}}

    Third List: {{third.option.name}}

    Angular code

    var app = angular.module('angularjs-starter', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';
      $scope.masterlist = 
          [ {name: 'John', selected: false}, {name: 'Bill', selected: false},
            {name: 'Smith', selected: false}, {name: 'Alex', selected: false},
            {name: 'Martin', selected: false}, {name: 'James', selectes: false}];
    
      $scope.first = {list: angular.copy($scope.masterlist), option: null};
      $scope.second = {list: angular.copy($scope.masterlist), option: null};
      $scope.third = {list: angular.copy($scope.masterlist), option: null};
    
      $scope.onChange = function(){
        $scope.enableAllOptions($scope.first.list);
        $scope.enableAllOptions($scope.second.list);
        $scope.enableAllOptions($scope.third.list);
    
        $scope.disableOptions($scope.first.list, $scope.second.list, $scope.second.option);
        $scope.disableOptions($scope.first.list, $scope.third.list, $scope.third.option);
    
        $scope.disableOptions($scope.second.list, $scope.first.list, $scope.first.option);
        $scope.disableOptions($scope.second.list, $scope.third.list, $scope.third.option);
    
        $scope.disableOptions($scope.third.list, $scope.first.list, $scope.first.option);
        $scope.disableOptions($scope.third.list, $scope.second.list, $scope.second.option);
      };
    
      //Enable all options by default.
      $scope.enableAllOptions = function(arr)
      {
        for(var i=0;i=0) destArr[index].selected = true;
        }
      };
    
    });
    

提交回复
热议问题