javascript - 带有选项组的角度材料多选下拉列表仅从每个组中选择一项?
问题描述
我正在尝试使用选项组实现多选下拉列表,并且用户只能从多选下拉列表中的每个组中选择一个项目。
<mat-form-field class="kb-article-filter-field" floatLabel="never">
<mat-select name="searchSettings" multiple>
<mat-optgroup *ngFor="let group of textSearchTypes" [label]="group.name">
<mat-option *ngFor="let option of group.options" [value]="option.value">
{{option}}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
textSearchTypes = [
{
name: 'searchName',
options: [
{ name: 'test', value: 'test' },
{ name: 'test1', value: 'test1' },
{ name: 'test2', value: 'test2' }
]
},
{
name: 'searchId',
options: [
{name:'testId', value:'testId'},
{name:'testId1', value:'testId1'},
{name:'testId2', value:'testId1'}
]
}
];
任何人都可以帮助如何在多选下拉列表中从每个组中只选择一个项目吗?
解决方案
将 valueChange 事件附加到您的 mat-select。在您附加的方法中,您执行以下操作:
- 如果您的组中选择了一个项目,请禁用该组中的其他项目。
- 如果您的组中没有选择任何项目,请启用该组中的所有项目。
selectionChanged() {
// Perform enable / disable for your options
}
<mat-form-field class="kb-article-filter-field" floatLabel="never">
<mat-select name="searchSettings" multiple (valueChange)=selectionChanged()>
<mat-optgroup *ngFor="let group of textSearchTypes" [label]="group.name">
<mat-option *ngFor="let option of group.options" [value]="option.value">
{{option}}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
推荐阅读
- plot - 使用 Geopandas 绘制缺失值
- cron - cron 表达式在当月的最后一个工作日运行脚本
- jquery - 将上下文菜单与 FullCalendar 一起使用
- php - Google Geocode API 不同的结果
- javascript - jQuery:一个类的 .nextUntil()
- python-3.x - 有没有办法创建两个函数的组合
- bash - 如何编写一个十六进制文件
- geopandas - 如何通过坐标限制 geopandas 图?
- gradle - Gradle zip task fails because zip does not exist
- java - 如何将 Web 元素转换为 JSON 字符串?