Alpine.js 的不确定复选框
Indeterminate Checkboxes with Alpine.js
我正在尝试使用 Alpine.js
实现不确定的复选框
您可以 select 所有复选框,但是当您尝试取消选中某些复选框时,所有复选框都未选中。
看看:https://codepen.io/nuno360/pen/gOwXpXP
<table x-data="{ allChecked: [] }"
x-init="
$watch('allChecked', value => {
if (value.length === 0) {
$refs.all.indeterminate = false;
$refs.all.checked = false
} else if (value.length == all.length) {
$refs.all.indeterminate = false;
$refs.all.checked = true
} else {
$refs.all.indeterminate = true
}
})
" class="bg-white min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th scope="col" class="h-14 leading-none" width="60"><input id="all" x-ref="all" @change="allChecked = $event.target.checked ? all : []; console.log(allChecked)" type="checkbox" class="form-checkbox border-gray-300 h-5 w-5 cursor-pointer rounded"></th>
<th scope="col" class="h-14 text-left text-xs text-gray-800 uppercase">Name</th>
<th scope="col" class="h-14 relative px-6"><span class="sr-only">Editar</span></th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
<tr class="hover:bg-gray-100">
<td class="h-14 leading-none text-center" width="60">
<input name="delete[]" x-model="allChecked" value="1" type="checkbox" class="form-checkbox border-gray-300 h-5 w-5 cursor-pointer rounded">
</td>
<td class="py-4 whitespace-nowrap text-sm text-gray-500">
Regional Paradigm Technician
</td>
<td class="px-5 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="text-blue-600 hover:text-blue-900">Editar</a>
</td>
</tr>
<tr class="hover:bg-gray-100">
<td class="h-14 leading-none text-center" width="60">
<input name="delete[]" x-model="allChecked" value="2" type="checkbox" class="form-checkbox border-gray-300 h-5 w-5 cursor-pointer rounded">
</td>
<td class="py-4 whitespace-nowrap text-sm text-gray-500">
Inter Paradigm
</td>
<td class="px-5 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="text-blue-600 hover:text-blue-900">Editar</a>
</td>
</tr>
</tbody>
</table>
感谢您的帮助。
无法取消选择单个选项的原因是因为下面一行中的 all
给出了 ref,它是 watch
函数的 dom 元素。
<input id="all" x-ref="all" @change="allChecked = $event.target.checked ? all : []; console.log(allChecked)" type="checkbox" class="...">
如果选中 all
复选框,我们需要将 allChecked
数组设置为 ['1','2']
所以要解决这个问题,
<input id="all" x-ref="all" @change="allChecked = $event.target.checked ? ['1','2'] : []; console.log(allChecked)" type="checkbox" class="...">
更新
要自动传递值,我们可以使用一个函数将复选框的所有值收集到一个数组中,如下所示。
<table x-data="{ allChecked: [], getValues(){
var array = []
var checkboxes = document.querySelectorAll('input[x-model=allChecked]')
for (var i = 0; i < checkboxes.length; i++) {
array.push(checkboxes[i].value)
}
return array;
} }" x-init="..." >
并使用 getValues
函数而不是如下硬编码。
<input id="all" x-ref="all" @change="allChecked = $event.target.checked ? getValues() : []; console.log(allChecked)" type="checkbox" class="...">
我正在尝试使用 Alpine.js
实现不确定的复选框您可以 select 所有复选框,但是当您尝试取消选中某些复选框时,所有复选框都未选中。
看看:https://codepen.io/nuno360/pen/gOwXpXP
<table x-data="{ allChecked: [] }"
x-init="
$watch('allChecked', value => {
if (value.length === 0) {
$refs.all.indeterminate = false;
$refs.all.checked = false
} else if (value.length == all.length) {
$refs.all.indeterminate = false;
$refs.all.checked = true
} else {
$refs.all.indeterminate = true
}
})
" class="bg-white min-w-full divide-y divide-gray-200">
<thead>
<tr>
<th scope="col" class="h-14 leading-none" width="60"><input id="all" x-ref="all" @change="allChecked = $event.target.checked ? all : []; console.log(allChecked)" type="checkbox" class="form-checkbox border-gray-300 h-5 w-5 cursor-pointer rounded"></th>
<th scope="col" class="h-14 text-left text-xs text-gray-800 uppercase">Name</th>
<th scope="col" class="h-14 relative px-6"><span class="sr-only">Editar</span></th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
<tr class="hover:bg-gray-100">
<td class="h-14 leading-none text-center" width="60">
<input name="delete[]" x-model="allChecked" value="1" type="checkbox" class="form-checkbox border-gray-300 h-5 w-5 cursor-pointer rounded">
</td>
<td class="py-4 whitespace-nowrap text-sm text-gray-500">
Regional Paradigm Technician
</td>
<td class="px-5 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="text-blue-600 hover:text-blue-900">Editar</a>
</td>
</tr>
<tr class="hover:bg-gray-100">
<td class="h-14 leading-none text-center" width="60">
<input name="delete[]" x-model="allChecked" value="2" type="checkbox" class="form-checkbox border-gray-300 h-5 w-5 cursor-pointer rounded">
</td>
<td class="py-4 whitespace-nowrap text-sm text-gray-500">
Inter Paradigm
</td>
<td class="px-5 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="#" class="text-blue-600 hover:text-blue-900">Editar</a>
</td>
</tr>
</tbody>
</table>
感谢您的帮助。
无法取消选择单个选项的原因是因为下面一行中的 all
给出了 ref,它是 watch
函数的 dom 元素。
<input id="all" x-ref="all" @change="allChecked = $event.target.checked ? all : []; console.log(allChecked)" type="checkbox" class="...">
如果选中 all
复选框,我们需要将 allChecked
数组设置为 ['1','2']
所以要解决这个问题,
<input id="all" x-ref="all" @change="allChecked = $event.target.checked ? ['1','2'] : []; console.log(allChecked)" type="checkbox" class="...">
更新
要自动传递值,我们可以使用一个函数将复选框的所有值收集到一个数组中,如下所示。
<table x-data="{ allChecked: [], getValues(){
var array = []
var checkboxes = document.querySelectorAll('input[x-model=allChecked]')
for (var i = 0; i < checkboxes.length; i++) {
array.push(checkboxes[i].value)
}
return array;
} }" x-init="..." >
并使用 getValues
函数而不是如下硬编码。
<input id="all" x-ref="all" @change="allChecked = $event.target.checked ? getValues() : []; console.log(allChecked)" type="checkbox" class="...">