简体   繁体   English

Aurelia单向绑定到一个复选框

[英]Aurelia one-way binding to a checkbox

I'm looking for a way to one-way bind a checkbox in aurelia, whilst still allowing the checkbox to accept a click from the user. 我正在寻找一种单向绑定aurelia中的复选框的方法,同时仍允许复选框接受来自用户的单击。

Assume a view similar to the following that displays one of a list of selectable items: 假设类似于以下的视图显示可选项列表之一:

<template>
    <div click.trigger="selected()">
        <label......>${vm.code}</label>
        <label....>${vm.description}</label>
        <img...../>
        <input type="checkbox" checked.one-way="vm.selected"></input>
    </div>
</template>

The user should be able to click anywhere in the view to select the item, thus the click.trigger="selected()" is attached to the container. 用户应该能够单击视图中的任意位置以选择项目,因此click.trigger="selected()"将附加到容器。 Within selected() , the vm.selected property that the checkbox is bound to is updated. selected() ,更新复选框绑定的vm.selected属性。

The checkbox should also be clickable as well, but should allow selection to be controlled by the selected() method. 该复选框也应该是可点击的,但应该允许选择由selected()方法控制。

readonly can not be used on the input control for the checkbox as that is used to control input.value and in this case it is the checked property that is of interest. readonly不能用于checkbox的输入控件,因为它用于控制input.value ,在这种情况下,它是感兴趣的checked属性。

Calling preventDefault on the event args disables default checkbox checked behavior, which can be achieved in Aurelia by returning false from the click delegate. 在事件args上调用preventDefault会禁用默认复选框选中的行为,这可以通过从单击委托返回false在Aurelia中实现。 That would require attaching another handler to the input control, and has the problem that the click gets handled by the input control and doesn't bubble up to the delegate attached to the container ( selected() ) to actually control selection. 这将需要将另一个处理程序附加到输入控件,并且具有输入控件处理单击的问题,并且不会冒泡到附加到容器的委托( selected() )以实际控制选择。

Maybe there is another approach to this that I am missing, one that I was considering was to use two font-awesome icons that look like checked and unchecked checkboxes and switch between the two based on the value of vm.selected. 也许我还缺少另一种方法,我考虑的是使用两个字体很棒的图标,看起来像是选中和未选中的复选框,并根据vm.selected的值在两者之间切换。

Update 更新

On the delegation of events this answer does solve the issue if you are not using a checkbox but the browser prevents the state from toggling on the checkbox so as we discussed the best approach is probably to use an icon or sprite to show the 'state' of the checked piece since the control should be one-way only, which can be accomplished like this - 在事件委托中,如果您没有使用复选框但浏览器会阻止状态切换复选框,则此答案可以解决问题,因此我们讨论了最佳方法可能是使用图标或精灵来显示“状态”由于控件应该是单向的,因此可以像这样完成 -

<i class="fa fa-${this.selected ? 'check-square-o' : 'square-o'}"></i>

Original answer on delegation 代表团的原始答案

If we use delegate instead of trigger then our event bubbles and we can handle it a bit more gracefully and prevent the state from changing - 如果我们使用delegate代替trigger那么我们的事件就会起泡,我们可以更优雅地处理它并防止状态发生变化 -

<div click.delegate="clicked()">
  <input type="checkbox" click.delegate="clicked(false)" checked.one-way="vm.selected" />
</div>

And then in our view model we can handle the click event that bubbles - 然后在我们的视图模型中,我们可以处理冒泡的click事件 -

  clicked(event){
    if (event !== false) {
      // do something
    } else {
      return false;
    }
  }

This isn't a perfect solution but it should prevent the event from occurring which changes the state of the control but still allow the event to take place at the div level. 这不是一个完美的解决方案,但它应该防止事件发生,这会改变控件的状态,但仍然允许事件发生在div级别。

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

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