简体   繁体   English

从带有角的选择框中删除非空选项

[英]Remove non-empty option from select box with angular

I have a form which has 3 email addresses, and 3 corresponding dropdowns, for email type. 我有一个表格,其中包含3个电子邮件地址和3个相应的下拉列表(用于电子邮件类型)。

The types are 'Preferred', 'Home', 'Work' and 'Other' 类型为“首选”,“家庭”,“工作”和“其他”

Only one email address can be preferred, so I want to remove that option from the other 2 select boxes when it is selected, and add it back if it is deselected. 只能选择一个电子邮件地址,因此,当我选择该电子邮件地址时,我想将其从其他2个选择框中删除,如果取消选择,则将其添加回去。

I am using Angular. 我正在使用Angular。

Is there a way to do this? 有没有办法做到这一点? I'm guessing a filter of some sort could do it, but I'm pretty new to Angular and have never done anything like this before. 我猜想某种过滤器可以做到,但是我对Angular还是很陌生,以前从未做过这样的事情。

Looking for as close to a "pure" Angular solution as I can get. 寻找尽可能接近的“纯” Angular解决方案。

I don't have control over how the data is stored, so please refrain from suggesting an alternate scheme. 我无法控制数据的存储方式,因此请不要提出替代方案。 I also have to build the UI to spec, so I can't add a preferred checkbox - which to me makes WAY more sense, but there it is- or anything like that. 我还必须构建符合规范的UI,所以我无法添加首选复选框-对我来说,这使WAY更有意义,但确实如此-或类似的东西。

Thanks in advance. 提前致谢。

You can have a filter for each select: 您可以为每个选择设置一个过滤器:

<select ng-model='first' ng-options='email as email for email in emails | filter:filterFirst'></select>
<select ng-model='second' ng-options='email as email for email in emails | filter: filterSecond'></select>
<select ng-model='third' ng-options='email as email for email in emails | filter: filterThird'></select>

Filters: 筛选条件:

 $scope.filterFirst = function(value){
   return isPreferred(value, $scope.second, $scope.third);
 }

 $scope.filterSecond = function(value){
   return isPreferred(value, $scope.first, $scope.third);
 }

 $scope.filterThird = function(value){
   return isPreferred(value, $scope.second, $scope.first);
 }

 function isPreferred(target, other1, other2){
   var preferred = "Preferred";
   if(target !== preferred) return true;
   if(other1 ===preferred) return false;
   if(other2 === preferred) return false;
   return true;
 }   

Update Changed so only the "Preferred" options is excluded. 更新已更改,因此仅排除了“首选”选项。

example http://plnkr.co/edit/RJuMMosFRQslsk0iifI7?p=preview 示例http://plnkr.co/edit/RJuMMosFRQslsk0iifI7?p=preview

It sounds like you have a software UI designer that was forced into web work. 听起来您有一个被迫从事网络工作的软件UI设计器。 It's a difficult change for them, and it results in very poor specs like what you have to deal with. 对于他们来说,这是一个困难的改变,并且会导致规格很差,例如您必须处理的内容。 Please accept my condolences. 请接受我的哀悼。 That being said, let's try to solve this. 话虽如此,让我们尝试解决这个问题。

You'll need a way to set/reset the values of the dropdowns: 您需要一种方法来设置/重置下拉菜单的值:

$scope.resetEmailTypes = function(){
    var EmailTypes = ['Home', 'Work', 'Other', 'Preferred'];
    $scope.emailType1 = angular.copy(EmailTypes);
    $scope.emailType2 = angular.copy(EmailTypes);
    $scope.emailType3 = angular.copy(EmailTypes);
};

On each select tag, attach an ng-change event calling a function similar to this: 在每个选择标签上,附加一个ng-change事件,该事件调用类似于以下功能的函数:

$scope.setPreferred = function(emailID){
    switch(emailID){
        case 1:
            if($scope.selectedEmailType1 === 'Preferred'){
                $scope.emailType2.pop();
                $scope.emailType3.pop();
            } else {
                $scope.resetEmailTypes();
            }
        case 2:
            if($scope.selectedEmailType2 === 'Preferred'){
                $scope.emailType1.pop();
                $scope.emailType3.pop();
            } else {
                $scope.resetEmailTypes();
            }
        default:
            if($scope.selectedEmailType3 === 'Preferred'){
                $scope.emailType1.pop();
                $scope.emailType2.pop();
            } else {
                $scope.resetEmailTypes();
            }
    }
};

Data binding should take care of the rest. 数据绑定应解决其余问题。

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

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