使用 jquery 实现报表的代码折叠
我正在尝试使用 jquery 折叠或展开第一列上显示的 + 和 - 符号的表行。
<script type="text/javascript">
$(document).ready(function() {
$("tr.header").click(function () {
$("tr.child", $(this).parent()).slideToggle("fast");
});
});
我正在尝试使用这段代码。但我希望单独点击的父母的孩子能够被切换。关于如何做到这一点有什么想法吗?
示例 Html
<table>
<tr class="header" >
<td>1 </td>
<td>line1</td>
</tr>
<tr class="child">
<td>2</td>
<td>line2</td>
</tr>
<tr class="child">
<td>3</td>
<td>line3</td>
</tr>
<tr class="header" >
<td>4 </td>
<td>line4</td>
</tr>
<tr class="child">
<td>5</td>
<td>line5</td>
</tr>
<tr class="child">
<td>6</td>
<td>line6</td>
</tr>
</table>
编辑:我试图将 + 或 - 的图像放在与第 1 行、第 4 行相同的行中,并尝试在 java 脚本中获取它的句柄以在两个图像之间切换。有人可以帮我吗?
编辑1:我可以在加载文档时折叠整个树吗?我是使用 XQuery 的初学者。
I'm trying to collapse or expand table rows with + and - sign displayed on the first column, using jquery.
<script type="text/javascript">
$(document).ready(function() {
$("tr.header").click(function () {
$("tr.child", $(this).parent()).slideToggle("fast");
});
});
I'm trying to use this code. But I want the child of the parent I'm clicking on alone to be toggled. Any ideas on how to do it?
Sample Html
<table>
<tr class="header" >
<td>1 </td>
<td>line1</td>
</tr>
<tr class="child">
<td>2</td>
<td>line2</td>
</tr>
<tr class="child">
<td>3</td>
<td>line3</td>
</tr>
<tr class="header" >
<td>4 </td>
<td>line4</td>
</tr>
<tr class="child">
<td>5</td>
<td>line5</td>
</tr>
<tr class="child">
<td>6</td>
<td>line6</td>
</tr>
</table>
Edit: I'm trying to put an image of + or - in the same row as line 1, line4 and trying to get a handle of it in the java script to switch between the two images. Can some help me on that?
Edit 1: Can I have the whole tree collapsed when the document is loaded. I'm beginner with using XQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(针对更改后的代码)
有两种方法:要么更改代码以仅切换单击的标题后面的行:
要么使用原始代码并将这些部分包装在 TBODY 中:
(In responce to the changed code)
There are two ways: Either change you code to toggle only the rows following the clicked header:
or use the original code and wrap the sections in TBODYs:
看一下这个以合并图像切换:
使用 JQuery 查找下一个表行
我将其合并到 Umang 共享的代码中,这似乎可以切换
图像以及下面的行。
$(document).ready(function() {
//这将在初始页面加载时滚动行
$("tr.header").nextUntil(".header").slideToggle("快", function () {
}); //end SlideToggle
//这将展开折叠行并切换 + 和 - 图像
$("tr.header").click(function () {
}); //结束点击
}); //end document.ready
这是关联的 HTML,但您必须删除 img 标签周围的 HTML 注释
因为这只是为了防止 stackoverflow 认为我正在发布真实的图像。
1
line1
2
line2
3
line3
4
line4
5
line5
6
line6
Take a look at this for incorporating the image toggle:
Using JQuery to find the next table row
I incorporated it into the code Umang shared and this appears to work on toggling the
image as well as the rows underneath.
$(document).ready(function() {
//This will roll up rows on initial page load
$("tr.header").nextUntil(".header").slideToggle("fast", function () {
}); //end slideToggle
//This will expand collapse row and toggle + and - images
$("tr.header").click(function () {
}); //end click
}); //end document.ready
Here is the associated HTML but you'll have to remove the HTML comments around the img tag
because that is just to prevent stackoverflow from thinking I'm posting an actual image.
1
line1
2
line2
3
line3
4
line4
5
line5
6
line6
您当前的代码确实会展开/折叠父行的子行。在您的示例中,它折叠了两个子行(data1、data2 和 data3、data4)
您是否想在这里实现不同的目标?
乌芒
Your current code does expand/collapses the child of the parent row. In your example, it collapses both child rows (data1, data2 & data3,data4)
Are you trying to achieve something different here?
Umang