简体   繁体   English

如何隐藏下拉列表中已选择的值?

[英]How to hide the already selected value from the dropdown list?

I need to hide already selected value from the dropdown list. 我需要从下拉列表中隐藏已经选择的值

For example, I have the dropdown with 2 options: 例如,我有2个选项的下拉列表:

  • Cancelled 取消
  • Confirmed 确认

落下

If user select the "Cancelled" option - the dropdown list shouldn't contain the selected option. 如果用户选择“已取消”选项-下拉列表不应包含所选选项。 In this case the only one 'Confirm' option should be available in the dropdown list. 在这种情况下,下拉列表中只有一个“确认”选项可用。

If user select "Confirm" option - the dropdown should contain the only one "Cancelled" option. 如果用户选择“确认”选项-下拉列表应仅包含一个“已取消”选项。

In other words - I need to display selected value in the select (for example 'Cancelled'), but doesn't display this value in the dropdown list (dropdown will contain the only 'Confirmed' value). 换句话说-我需要在显示所选值select (例如“已取消”), 但不显示在下拉列表 (下拉列表将包含唯一的“确认”值) 这个值

The question is - is it possible to implement, and if yes - how? 问题是-是否可以实施,如果可以,如何执行?

I'm using AngularJS on my frontend side. 我在前端使用AngularJS。

<select ng-options="status.id as status.name for status in appt.possibleStatuses" 
   ng-model="appt.statusId">
</select>

I understand, that it's possible to implement with custom UI control, but I want to do it via default select tag. 我了解,可以使用自定义UI控件来实现,但是我想通过默认的select标签来实现。

But if you know easy and nice solutions for this problem without using of the select tag - you are welcome :) 但是,如果您不使用select标签就可以轻松解决该问题,欢迎您:)

Thanks! 谢谢!

You can use a function instead of appt.possibleStatuses 您可以使用函数代替appt.possibleStatuses

<select ng-options="status.id as status.name for status in filterFunction()" 
   ng-model="appt.statusId">
</select>

And in your controller code 并在您的控制器代码中

$scope.filterFunction = function () {
    var statuses = $scope.appt.possibleStatuses;
    // any custom filter logic for statuses
    return statuses;
}

Going back to my "hack" method mentioned in the question comments, you could implement something like this: 回到问题注释中提到的“ hack”方法,您可以实现以下内容:

<form id="form" method="post">
  <select name="value_Name">
     <option value="" selected></option>
     <option value="value1">value1</option>
     <option value="value2">value2</option>
  </select>
</form>

The "selected" tag informs the browser that this is the default value. “选择的”标签通知浏览器这是默认值。 You can then implement any means of input validation you wish. 然后,您可以实现所需的任何输入验证方法。

Personally, I prefer to do everything at a servlet-level but you can implement this via JQuery or JavaScript functions. 就个人而言,我更喜欢在servlet级别上做所有事情,但是您可以通过JQuery或JavaScript函数来实现。

You need to add ng-change="changeFunction(appt.statusId)" like 您需要添加ng-change="changeFunction(appt.statusId)"

<select ng-options="status.id as status.name for status in appt.possibleStatuses" ng-model="appt.statusId" ng-change="changeFunction(appt.statusId)">

Put bellow js code in your controller 将下面的js代码放入您的控制器中

$scope.changeFunction = function(status){
    if(status == "Cancelled"){
        /* code for hide or remove "Confirm" option */
    } else if(status == "Confirm"){
        /* code for hide or remove "Cancelled" option */
    }
}

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

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