【发布时间】:2021-10-11 11:59:23
【问题描述】:
我正在尝试创建一个具有多级行组/折叠的 HTML 表格,加载页面时默认折叠为 +Parnts.Group1。
并在打开+Parnts.Group1时默认折叠+ SubGroup1。
多级行分组/折叠示例
| A header | Another header |
|---|---|
| +Parnts.Group1 | +Parnts.Group1 |
| + SubGroup1. | + SubGroup1 |
| - text | - text |
我发现的都是只有一层的表。
我搜索了很多解决方案,但代码每次都不一样,所以我无法弄清楚,因为我不是编码专家。
另外,出于同样的原因,我无法将 + - 添加到 JS 中的组中。
这是我的代码
JS - CSS - HTML
var toggle_rows = function (element, parent) {
if ($(element).closest("tr").attr("state") == "opened") {
$("tr[parent~='" + parent + "']").each(function (i, obj) {
toggle_child_rows(obj, false);
});
$("tr[parent~='" + parent + "']").hide();
$(element).closest("tr").attr("state", "collapsed")
} else {
$("tr[parent~='" + parent + "']").each(function (i, obj) {
toggle_child_rows(obj, true);
});
$("tr[parent~='" + parent + "']").show();
$(element).closest("tr").attr("state", "opened");
}
};
var toggle_child_rows = function (obj, willShow) {
var name = null;
if ($(obj).attr("state") === "leaf") {
return;
} else {
name = $(obj).attr("rowname");
if ($(obj).attr("state") === "opened" && !willShow) {
$("tr[parent~='" + name + "']").each(function (i, elem) {
toggle_child_rows(elem, willShow);
$(elem).hide();
});
} else if ($(obj).attr("state") === "opened" && willShow) {
$("tr[parent~='" + name + "']").each(function (i, elem) {
toggle_child_rows(elem, willShow);
$(elem).show();
});
}
}
};
<CSS>
table {
border-collapse:collapse;
margin: 150px auto;
table-layout: fixed;
width: 750px;
word-wrap: break-word;
cursor: pointer;
}
.name {
text-align: center;
}
th, td {
border:1px solid #DDD;
}
th {
cursor: pointer;
background-color: #006098;
color:white;
position:center;
}
.level1 {
cursor: pointer;
background-color: #00C1C1;
}
.level2 {
cursor: pointer;
background-color: gray;
}
.level3 {
cursor: pointer;
background-color: #eaeaea;
}
.level4 {
cursor: pointer;
background-color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class='table table-hover table-bordered table-condensed cashflow_report'>
<thead>
<tr>
<th width="150px" class='name'></th>
<th width="300px" >Header1</th>
<th width="300px" >Header2</th>
</tr>
</thead>
<tbody>
<tr class='level1' rowname='inflow' state='opened'>
<td class='name'> <span onclick="toggle_rows(this, 'inflow');">
+ Parnts.Group1 </span> </td>
<td class='name'> <span onclick="toggle_rows(this, 'inflow');">
test </span> </td>
<td class='name'> <span onclick="toggle_rows(this, 'inflow');">
test </span> </td>
</tbody>
<tbody >
<tr class='level2' parent='inflow' rowname='in-ct-1' state='opened'>
<td class='name'> <span onclick="toggle_rows(this, 'in-ct-1');">
+ Group 1 </span></td>
<td>0</td>
<td>0</td>
</tr>
<tr class='level3' parent='in-ct-1' rowname='in-tt-1' state='opened'>
<td class='name'> <span onclick="toggle_rows(this, 'in-tt-1');">
+ Subgroup 1 </span></td>
<td>0</td>
<td>0</td>
</tr>
<tr class='level4' parent='in-tt-1' state='leaf'>
<td class='name'> Operation 1</td>
<td>0</td>
<td>0</td>
</tr>
<tr class='level3' parent='in-ct-1' rowname='in-tt-2' state='opened'>
<td class='name'> <span onclick="toggle_rows(this, 'in-tt-2');">
Subgroup 2</span></td>
<td>0</td>
<td>0</td>
</tr>
<tr parent='in-tt-2' state='leaf'>
<td class='name'> Operation 2</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: javascript html-table row expand