简体   繁体   English

禁用点击复选框; 让父div处理

[英]Disable clicking on checkbox; let parent div handle the

I've got a dynamically created form that has a simple list (created within a ngFor) that has: 我有一个动态创建的表单,该表单具有一个简单的列表(在ngFor中创建),该列表具有:

  • A parent div that handles the click: it selects or deselects the current item. 处理点击的父div:选择或取消选择当前项目。
  • A checkbox that's a child of that div, that is supposed to show the selected/unselected status of the given item. 一个复选框是该div的子级,应该显示给定项目的选定/未选定状态。

If you click the div, everything works as expected, but if you click in the checkbox, nothing really happens. 如果单击div,则一切正常,但如果单击复选框,则什么也没有发生。 It's like the click in the checkbox collides with the div click, not changing the value at all. 就像复选框中的点击与div点击冲突一样,根本没有改变其值。

The "cleanest" solution I've found this far, is not a clean solution at all, since I still cannot click in the checkbox, but at least it won't give me this weird functioning. 到目前为止,我发现的“最干净”的解决方案根本不是干净的解决方案,因为我仍然无法单击复选框,但至少它不会给我这种怪异的功能。 Check it out: 看看这个:

  <div class="popup-elem" *ngFor="let option of selectedCategory?.options" (click)=" option.active = !option.active"
    [ngStyle]="option.active == true 
              ? {'background-color':'aliceblue'} 
              : {'background-color':'white'}">
    <input type="checkbox" [checked]="option.active" style="z-index:550" [disabled]="true">
    {{ option.name }}
  </div>

I tried other stuff with NgModel and so, but this far this is the best I have. 我用NgModel尝试了其他东西,但是到目前为止,这是我最好的。 I would be happy if, at least, I could trigger the div's click event when clicking the checkbox, but I don't know how to do so. 如果至少在单击复选框时可以触发div的click事件,我会很高兴,但是我不知道该怎么做。

Any ideas? 有任何想法吗?

If you want to reenable the checkbox and get a proper behavior, you should add a handler for the click event on the checkbox to change the model: 如果要重新启用复选框并获得适当的行为,则应在复选框上为click事件添加处理程序以更改模型:

<input type="checkbox" [checked]="option.active" style="z-index:550" (click)="flip($event, option)" />

Add a handler in your controller : 在控制器中添加处理程序:

flip($event, option) {
    option.active=$event.target.checked;
}

Okay, I finally found a solution that, while is not fully perfect, works quite well and I'm happy with. 好的,我终于找到了一个解决方案,尽管它并不完全完美,但效果很好,我对此感到满意。 It won't allow me to do the select via clicking the parent div, but the end result is almost the same. 它不允许我通过单击父div进行选择,但是最终结果几乎相同。

  <div class="popup-elem" *ngFor="let option of selectedCategory?.options"
    [ngStyle]="option.active == true 
              ? {'background-color':'aliceblue'} 
              : {'background-color':'white'}">

    <label class="container"> {{ option.name }}
      <input type="checkbox" (change)="option.active = !option.active" [checked]="option.active">
      <span class="checkmark"></span>
    </label>

This way, when I click the checkbox, text or padding of that container, the item gets properly selected. 这样,当我单击该容器的复选框,文本或填充时,将正确选择该项目。 This doesn't really fix the issue that the event delegation gets broken from the parent div to the child checkbox, but by just setting a higher padding, you can actually achieve the same user experience, which is what I wanted. 这并不能真正解决事件委托从父div中断到子复选框的问题,但是通过设置更高的填充,您实际上可以实现相同的用户体验,这正是我想要的。

I hope this is useful for other people who stumble upon this annoying problem! 我希望这对偶然发现此烦人问题的其他人有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM