Hyperscript多表格行全选功能异常:无法取消选中求助
多表格场景下Hyperscript全选复选框功能异常问题
我在Hyperscript中实现表格行的全选/单选功能,单表格场景下一切正常,但在同一页面存在多表格的场景中(因使用模板,不想使用差异过大的ID),我添加了in closest <table/>来限定作用域,大部分功能正常。
例如表格上方按钮的以下代码可正常工作:
on selectionChanged if length of <input[id^='select_row_']:checked/> === 1 in closest <table/> remove @disabled from me else add @disabled to me end
行内复选框的代码如下,点击时能正确触发全选框的状态更新:
on click send selectionChanged to previous <button[id^='delete_button']/> send selectionChanged to previous <button[id^='edit_button']/> send selectionChanged to previous <input[id^='select_all_']/> end
但点击全选复选框时,只能一次性将所有行设为选中状态,无法取消选中。全选框的代码如下:
on click set <input[id^='select_row_']/>'s checked to my.checked in closest <table/> send selectionChanged to previous <button[id^='delete_button']/> send selectionChanged to previous <button[id^='edit_button']/> end on selectionChanged set my.checked to length of <input[id^='select_row_']:checked/> === length of <input[id^='select_row_']/> in closest <table/> end
奇怪的是,如果移除这段代码中的in closest <table/>,功能会完全正常,但会出现跨表格选中的问题——点击一个表格的全选框会选中所有表格的行。我怀疑是不是对Hyperscript的语法理解有误,希望能得到解决思路。
更新内容
我尝试修改了全选框的点击逻辑,初始时功能可用,但当手动逐个选中所有行后,全选框无法再取消选中所有行:
on click if my.checked add @checked to <input[id^='select_row_']/> in closest <table/> else remove @checked from <input[id^='select_row_']/> in closest <table/> end send selectionChanged to previous <button[id^='delete_button']/> send selectionChanged to previous <button[id^='edit_button']/> end on selectionChanged set my.checked to length of <input[id^='select_row_']:checked/> === length of <input[id^='select_row_']/> in closest <table/> end
on selectionChanged部分的代码一直正常工作,但我发现修改行选中状态时,直接设置checked为true/false无效,必须通过添加/移除checked属性来实现。于是把:
set <input[id^='select_row_']/>'s checked to my.checked in closest <table/>
替换为:
if my.checked add @checked to <input[id^='select_row_']/> in closest <table/> else remove @checked from <input[id^='select_row_']/> in closest <table/> end
这段代码会将行复选框从:
<input type="checkbox" id="select_row_xxx" ...>
修改为:
<input type="checkbox" id="select_row_xxx" ... checked="undefined">
此时行显示为选中状态;但移除checked属性后,所有行依然保持选中状态——这个问题仅在手动逐个选中所有行的情况下出现。
内容的提问来源于stack exchange,提问作者Erik Bent
相关产品推荐
相关产品推荐

