[英]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.