简体   繁体   English

当我有一个空数组作为我的 check.bind 时,如何创建一个 select 全部复选框

[英]How do I create a select all checkbox when I have an empty array as my checked.bind

I have a table with checkboxes as the first column.我有一个带有复选框的表格作为第一列。 I want to have a select all checkbox in the header.我想在 header 中有一个 select all 复选框。 I'm not sure how to approach this problem.我不确定如何解决这个问题。

<template>
  <table class="table table-hover">
      <thead>
        <tr>
          <th><input type="checkbox" /></th>
          <th>ID</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input type="checkbox" checked.bind="selected" model.bind="site" /></td>
          <td>${site.id}</td>
        </tr> 
      </tbody>
    </table>  
</template>

So, the very best way to solve this problem has three parts.因此,解决此问题的最佳方法包括三个部分。 We need to bind the checked attribute and the indeterminate attribute, and we need to handle the change event.我们需要绑定checked属性和indeterminate属性,并且需要处理change事件。

First, we want the checkbox to be checked whenever the selected array has all the items in the items array.首先,我们希望在所选数组包含items数组中的所有项目时selected复选框。 So:所以:

<input type="checkbox" checked.bind="selected.length === items.length" />

This checks the checkbox whenever all items are selected, but checking the checkbox has no effect on the selected array yet. This checks the checkbox whenever all items are selected, but checking the checkbox has no effect on the selected array yet.

Next, we want the checkbox to be indeterminate whenever selected array has some but not all items in the items array.接下来,只要selected数组在items数组中有一些但不是所有项,我们希望复选框是不确定的。 So:所以:

<input type="checkbox 
  checked.bind="selected.length === items.length"
  indeterminate.bind="selected.length > 0 && selected.length < items.length" />

Now, the checkbox completely reflects the state of the selected array, but still has no effect on the selected array.现在,该复选框完全反映了selected阵列的 state,但仍然对selected阵列没有影响。

Finally, we can listen to the change event on the checkbox to update the selected array whenever the checkbox is checked or unchecked.最后,我们可以在复选框被选中或取消选中时监听复选框上的change事件以更新selected数组。 I use an inline handler below, and in this handler $event.target is the checkbox itself.我在下面使用了一个内联处理程序,在这个处理程序中$event.target是复选框本身。

<input type="checkbox"
  checked.bind="selected.length === items.length"
  indeterminate.bind="selected.length > 0 && selected.length < items.length"
  change.delegate="$event.target.checked ? selected = items.slice() : selected = []" />

At this point, the checkbox is working perfectly.此时,复选框工作正常。 Checked is normally a two-way binding, but in our example above it is only operating one-way. Checked 通常是一种双向绑定,但在我们上面的示例中,它只是单向操作。 We can make this small optimization:我们可以做这个小优化:

<input type="checkbox"
  checked.one-way="selected.length === items.length"
  indeterminate.one-way="selected.length > 0 && selected.length < items.length"
  change.delegate="$event.target.checked ? selected = items.slice() : selected = []" />

See this gist.run for a working example: https://gist.run/?id=61cc823708ff7f6ba195de9ff0353049有关工作示例,请参阅此gist.run:https://gist.run/?id=61cc823708ff7f6ba195de9ff0353049

Note that I typically find it useful to create a custom element that encapsulates this behavior.请注意,我通常发现创建一个封装此行为的自定义元素很有用。

暂无
暂无

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

相关问题 当我选中所有复选框时如何选择所有复选框 - How to select all checkbox when i checked all check boxes 如何在页面加载中将我的默认日期设置为默认复选框,而不是全选? - How do I set my default checked checkbox on pageload for my latest date in checkboxlist instead of select all? 选中复选框后,如何更改DIV的颜色? - How do I change the color of my DIV when checkbox is checked? 当所有字段都具有唯一ID时,如何知道该复选框是否已选中? - How can I know if the checkbox is checked when all the fields have unique id? 在检查我的复选框数组是否全部未选中后,如何显示此警告框 - How can i display this alert box after checking if my array of checkbox is all not checked 如何在具有选中复选框的行中选择`td`元素? - How Do I Select a `td` Element in a Row That Has a Checkbox That is Checked? 如何在选中复选框时更改 select 以输入文本? - How do I change select to input text while checkbox is checked? 当我选中一个复选框(相同的类)并将值插入文本框中时,如何检查另一个复选框 - How do i checked another checkbox when i checked a checkbox(same class) and insert value into a textbox 遍历我的表,我如何知道该复选框是否已选中? - Looping through my table, how do I know if the checkbox is checked? 选中复选框后,如何迭代表的行并将特定的单元格值传递给数组? - How do I iterate a tables rows and pass a specific cell value to an array when a checkbox is checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM