[英]Angular2 enable/disable radio inputs in table when checked/unchecked
I've created a table where the priority of top 3 rows are set by using radio buttons. 我创建了一个表,其中使用单选按钮设置了前3行的优先级。 Goal is to make the siblings radio buttons enabled/disabled when checked/unchecked.
目标是在选中/取消选中时使同级单选按钮启用/禁用。 I'm able to get the buttons checked/unchecked vertically by providing the same name on each row but I'm not being able to do that to it's siblings horizontally.
我可以通过在每一行上提供相同的名称来垂直检查/取消选中按钮,但是我无法对水平的兄弟姐妹做到这一点。
I've tried using ([attr.disabled]="boolean") on input element but that disables all the buttons in the table. 我试过在输入元素上使用([attr.disabled] =“ boolean”),但是禁用了表中的所有按钮。 I also tried doing it with javascript but it makes the code really long which is still not perfect.
我也尝试用javascript来做,但是它使代码很长,仍然不够完美。
If anyone could help !! 如果有人可以帮助!
Below is my code. 下面是我的代码。
HTML 的HTML
<tr *ngFor="let news of newss">
<td>{{ news.newstitle }}</td>
<td>{{ news.newsdescription }}</td>
<td>{{ news.display }}</td>
<td>{{ news.status }}</td>
<td><span> {{ news.photo }}</span></td>
<td>
<div class="radio">
<label><input type="radio" name="priority1" #priority1
(change)="handleChange($event, news, priority1)"
[attr.disabled]="disprior1" /></label>
</div>
<div class="radio">
<label><input type="radio" name="priority2" #priority2
(change)="handleChange($event, news, priority2)"
[attr.disabled]="disprior2" /></label>
</div>
<div class="radio">
<label><input type="radio" name="priority3" #priority3
(change)="handleChange($event, news, priority3)"
[attr.disabled]="disprior3" /></label>
</div>
</td>
</tr>
TS: TS:
handleChange(evt, news, priority) {
let target = evt.target;
if (target.checked) {
if (priority.name == "priority1") {
this.disprior2 = true;
this.disprior3 = true;
} else if (priority.name == "priority2") {
this.disprior1 = true;
this.disprior3 = true;
} else if (priority.name == "priority3") {
this.disprior1 = true;
this.disprior2 = true;
}
}
}
<tr *ngFor="let news of newss; let i = index">
<td>{{ news.newstitle }}</td>
<td>{{ news.newsdescription }}</td>
<td>{{ news.display }}</td>
<td>{{ news.status }}</td>
<td><span> {{ news.photo }}</span></td>
<td>
<div class="radio">
<label>
<input type="radio" [name]="'priority' + i" (change)="handleChange($event, news)"/>
</label>
</div>
<div class="radio">
<label>
<input type="radio" [name]="'priority' + i" (change)="handleChange($event, news)"/>
</label>
</div>
<div class="radio">
<label>
<input type="radio" [name]="'priority' + i" (change)="handleChange($event, news)"/>
</label>
</div>
</td>
</tr>
you can simply declare the index in *ngFor and use it to set the [name] attribute for each group of radio buttons. 您只需在* ngFor中声明索引,并使用它为每组单选按钮设置[name]属性。
May I suggest the use of the following libraries: 我可以建议使用以下库:
very easy to use and very powerful. 非常易于使用,功能非常强大。 I use check boxes in the following way:
我通过以下方式使用复选框:
<p-checkbox *ngFor="let part of partsLoaded; let i = index"
name="group1"
label="{{ part }}"
value="{{ part }}"
[(ngModel)]="selectedParts"
(click)="showOrHidePart()"
(mouseover)="selectMesh(part, i)"
(mouseout)="deSelectMesh(part, i)"
[class.active]="i == selectedMeshIndex"
[class.no-mesh]="getStyle(part)"
[disabled]="getStatus(part)">{{ part }}
</p-checkbox>
as you can see I disable the check boxes according to the value returned by the function getStatus(): 如您所见,我根据getStatus()函数返回的值禁用了复选框:
getStatus(part) {
let selectedMesh = this.meshCollection.find(obj => obj.name === part).object;
if (selectedMesh) {
return false;
} else {
return true;
}
}
You can do something similar for your radio boxes. 您可以为收音机盒做类似的事情。 You can also leverage the use of name property to accomplish your goal.
您还可以利用name属性的使用来实现您的目标。 Dino
迪诺
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.