简体   繁体   English

选中/未选中时,Angular2启用/禁用表中的无线电输入

[英]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: 我可以建议使用以下库:

PrimeNG PrimeNG

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.

相关问题 如果未选中无线电,则禁用按钮。 选中时启用 - Disable button if radio is unchecked. Enable when checked 选中/取消选中复选框时启用/禁用下拉菜单 - Enable/disable dropdowns when checkbox checked/unchecked 在单选按钮之一选中时启用表单字段,在未选中时禁用字段 - To enable form fields when one of the radio button checked & disable fileds when it unchecked 复选框:当有多个选中的复选框时,启用/禁用未选中的复选框 - Check box: Enable/Disable unchecked boxes when there is a number of checked ones 选中时将angular2复选框的值设置为1,将其取消选中以设置为0 - angular2 checkbox value when checked set 1 and unchecked to set 0 选中并取消选中表格中的单选按钮 - radio button in a table checked and unchecked 如果复选框被选中/未选中,启用/禁用提交按钮? - Enable/Disable submit button if checkbox is checked/unchecked? 取消选中复选框时,从收音机中删除“选中” - Remove “checked” from radio when a checkbox is unchecked 如何在语义UI中选中和取消选中复选框时动态地启用和禁用按钮 - how to dynamically enable and disable button when checkbox is checked and unchecked in semantic UI 未选中复选框时禁用输入 - Disable the inputs when checkbox not checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM