javascript - Angularjs ng-repeat 从多个选择框中删除选定的选项
问题描述
使用 ng-repeat am 加载多个选择下拉元素,并且每个下拉列表都加载了相同的一组值。一旦用户从选择框中选择了一个选项;所选选项不应出现在其他选择框(选项)中。
示例:如果用户从select中选择选项A:1;然后在 select: 2 或任何其他 select: N 选项 A 不应该出现,反之亦然。
Plunker:https ://plnkr.co/edit/yCFIanCXPece49dk?open=lib%2Fscript.js&deferRun=1&preview
代码:
<option ng-repeat="option in data.availableOptions | filter:arrayFilter(select.selectedOption,$index)" value="{{option.id}}">
$scope.arrayFilter = function(selectionArray, position) {
return function(item, index) {
return selectionArray.indexOf(item.id) == -1 || item.id == selectionArray[position];
}
}
解决方案
您可以使用函数获取嵌套 ng-repeat 的选项,然后过滤掉不是当前选项并被选中的选项:
html:
<select name="repeatSelect{{$index}}" id="repeatSelect{{$index}}" ng-model="select.selectedOption">
<option
ng-repeat="option in getAvailableOptions(select.selectedOption)"
value="{{option.id}}"
>
{{option.name}}
</option>
</select>
控制器:
$scope.getAvailableOptions = function(current) {
return this.data.availableOptions.filter((o) => o.id === current || !this.data.availableSelect.some(s => s.selectedOption === o.id));
};
这是一个分叉的plunker
推荐阅读
- windows - Laradock:docker-compose.yml 在 Windows 上是无效格式 (PORT[/PROTOCOL])
- c++ - neovim 在执行 .out 文件时自动提供输入
- c++ - 智能指针可以通过禁止自动变量更安全吗?
- reactjs - 使用 Firebase 和匿名用户设置 Stream 活动供稿的推荐方法是什么?
- php - 使用Mpdf(Laravel)生成pdf时如何使用bangla字体
- variables - (Pinescript) 将当前柱形指示器状态永久存储在变量中?
- clojure - 递归树搜索:产生具有副作用的并发工作者
- swift - 打开任何项目时 Xcode 12.4 崩溃
- python - 如何使用 python-telegram-bot 向电报频道发送消息
- google-sheets - 在 Google 表格中插入不同时区的时间