jQuery 展开/折叠分层表格行
我正在寻找一种使用 jQuery 展开/折叠分层表行的有效方法。问题是,展开和折叠功能不同。
- 最初,仅显示类
level_0
的行,所有其他行均隐藏。 - 展开应该只显示下一个级别,因此单击行
id=10
应该只会生成具有id=11
和id= 的行14 可见。
- 另一方面,折叠应该折叠比当前行更深的所有连续行。例如,单击行
id=10
上的折叠应该隐藏 ID 为11, 12, 13, 14
的行(如果它们可见)。
表数据如下所示:
<table id="mytable">
<tr class="level_0" id="10">...</td>
<tr class="level_1 parent_10" id="11">...</td>
<tr class="level_2 parent_11" id="12">...</td>
<tr class="level_2 parent_11" id="13">...</td>
<tr class="level_1 parent_10" id="14">...</td>
<tr class="level_0" id="15">...</td>
</table>
我的非工作解决方案:
$('#mytable tr').live('click', function() {
var toggleClass = 'parent_' + $(this).attr('id');
$(this).nextAll('tr').each(function() {
if ($(this).is('.'+toggleClass)) {
$(this).toggleClass("showme");
}
});
});
问题是,它只折叠下一级行。仍显示单击的行下方的可见和更深层次的行。
谁能给我一些关于如何有效地做到这一点的提示?如果需要,可以调整 HTML 代码。
感谢您的任何提示。
I am looking for an efficient way to expand/collapse hierarchical table rows using jQuery. The problem is, that the expand and collapse functionality differs.
- Initially, only rows with class
level_0
are show, all other rows are hidden. - expand should only show the next level, so clicking on row
id=10
should only make rows withid=11
andid=14
visible. - collapse on the other hand, should collapse all consecutive rows with a deeper level than the current one. For example, clicking collapse on row
id=10
should hide rows with ids11, 12, 13, 14
, if they are visible.
The table data looks as follows:
<table id="mytable">
<tr class="level_0" id="10">...</td>
<tr class="level_1 parent_10" id="11">...</td>
<tr class="level_2 parent_11" id="12">...</td>
<tr class="level_2 parent_11" id="13">...</td>
<tr class="level_1 parent_10" id="14">...</td>
<tr class="level_0" id="15">...</td>
</table>
My non-working solution:
$('#mytable tr').live('click', function() {
var toggleClass = 'parent_' + $(this).attr('id');
$(this).nextAll('tr').each(function() {
if ($(this).is('.'+toggleClass)) {
$(this).toggleClass("showme");
}
});
});
The problem is, that it only collapses the next level rows. Visible and deeper level rows beneath the clicked row are still shown.
Can anyone give me some hints on how I could do this in an efficient way? The HTML code can be adjusted if needed.
Thanks for any hints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建了一个多层次层次结构的版本作为另一个问题的答案。
您的表的 jQuery 将是:
请参阅此 JSFiddle 来了解它的工作原理。
优化
我的表结构略有不同,其中涉及指定父级和子级,以便搜索更加高效。
然后我还制作了一个 jQuery 分层表行切换 Gist 并将其转换为对象化带有优化的 javascript。优化来自 http://24ways.org/2011/your -jquery-now-with-less-suck/。
具体来说:
表上而不是行上的事件委托。
缓存子级选择。
在 .childClass 上使用较快的元素选择器,然后使用较慢的过滤器< /强>
I've created a version for multiple levels of hierarchy as an answer to another question.
The jQuery for your table would be:
See this JSFiddle for it working.
Optimisations
I have a slightly different table structure, which involves specifying parents as well as children so that the searching can be more efficient.
I've then also made a jQuery hierarchical table row toggling Gist from it and converted it into objectified javascript with optimisations. The optimisations come from http://24ways.org/2011/your-jquery-now-with-less-suck/.
Specifically:
Event delegation on the table rather than on the rows.
Cache the children selection.
Use the faster element selector followed by a slower filter on the .childClass
我认为您需要更多代码,以便可以处理关闭和打开状态下所有行的点击。
我添加了一个类开关来指示行何时打开,并根据其状态以不同方式处理点击。
collapse
和expand
都有 while 循环遍历行,从紧邻单击的行之后的行开始,当到达同一级别的行时停止。这个逻辑应该适用于任何级别,而不仅仅是 0。此外,使用nextUntil
逻辑和一个花哨的选择器,while
循环可能会更干净 - 我对此并不熟悉jQuery 方法,直到看到 Jules 的回答 - 非常狡猾!另外,为了使示例代码更简单,我将关卡类命名系统视为 HTML 数据属性,因此给定行如下所示:
< /代码>。您可以将对
.data
的调用替换为解析类名的代码。(这还没有经过测试 - 抱歉,我得解雇了!)
I think you're gonna need a little some more code so you can handle clicks on all the rows in both closed and open states.
I added a class switch to indicate when a row is open, and treat clicks differently based on it's state. Both
collapse
andexpand
have while loops that walk through rows, starting with the one immediately after the clicked row, and they stop when they get to rows of the same level. This logic should work for any level, not just 0. Also, thewhile
loop could probably be cleaner usingnextUntil
logic with a fancy selector - I wasn't familiar with that jQuery method until seeing Jules answer - very slick!ALso, for keeping this example code simpler, I treated your level class naming system as HTML data attributes, so a given row looks like this:
<tr data-level="0" id="10">
. You could replace calls to.data
with code to parse your class names.(This isn't tested - sorry gotta hit the sack!)
您帖子中的表格 html 无效(tr 包含在 td 中)。在结构正确的表上,此代码可以工作。
The table html in your post is not valid (tr enclosed by td). On the properly structured table, this code works.