angular - 每个表格行有多个单选按钮
问题描述
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Agenda No</th>
<th scope="col">Agenda Description</th>
<th scope="col">Opinion</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of agendaList">
<td>Agenda {{item.AGENDANO}}</td>
<td>{{item.AGENDA_DESC}}</td>
<td>
<div class="custom-control custom-radio">
<input type="radio" value="1" id="customRadio1" name="customRadio" class="custom-control-input">
<label class="custom-control-label" for="customRadio1">Yes</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" value="0" id="customRadio2" name="customRadio" class="custom-control-input">
<label class="custom-control-label" for="customRadio2">No</label>
</div>
</td>
</tr>
</tbody>
</table>
我有 3 个值的表。每行将有 2 个单选按钮,“是”或“否”。如何分别从每一行中选择其中一个。
方法:
第一行 --> “是”、“否” ---> 我会选择是
第二行 --> "yes", "no" ---> 我会选择 no
第三行 --> “是”、“否” ---> 我会选择是
我怎样才能做到这一点?
解决方案
https://stackblitz.com/edit/angular-nwdean
你需要改变他们的名字属性。让每一行的它们都独一无二,例如你可以像这样写name="customRadio{{item.AGENDANO}}"
并创建一个更多的属性来将你的答案绑定到你的模型。我为你创建了示例 stackblitz 演示
<table class="table table-hover table-bordered">
<thead>
<tr>
<th scope="col">Agenda No</th>
<th scope="col">Agenda Description</th>
<th scope="col">Opinion</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of agendaList">
<td>Agenda {{item.AGENDANO}}</td>
<td>{{item.AGENDA_DESC}}</td>
<td>
<div class="custom-control custom-radio">
<input type="radio" value="1" id="customRadio{{item.AGENDANO}}" name="customRadio{{item.AGENDANO}}" class="custom-control-input">
<label class="custom-control-label" for="customRadio{{item.AGENDANO}}">Yes</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" value="0" id="customRadio2" name="customRadio{{item.AGENDANO}}" class="custom-control-input">
<label class="custom-control-label" for="customRadio{{item.AGENDANO}}">No</label>
</div>
</td>
</tr>
</tbody>
</table>
推荐阅读
- c# - 学习 Unity 中的保存和加载;由于未知原因遇到错误 CS1513
- python - Python signxml 自定义命名空间
- sql - 在另一个表中为外键创建新行
- java - 如何为 Windows 10 的 HDF5view 应用程序安装 java
- python - 为什么在 python 中以不同方式导入时 setattr 不起作用?
- visual-studio - VS2019 DataSet Designer Mangling INSERT 查询 - 删除 RETURNING 语句
- docker - 阻止 Docker 容器退出
- linux - 使用videobalance调整gstreamer管道中的对比度和亮度将相机流保存到文件
- javascript - 使用 react-redux 我正在尝试创建中间件来执行异步等待逻辑,以便能够使用 this.props.getUser 操作功能
- reactjs - Formik 表单未通过 onclick 更新