我有一个表,可以通过按钮或单击该行进行精细切换。问题是,当第一列跨越两行或更多行时,所有内容都不同步。还希望底部边框在切换区域下方移动,并且希望悬停区域的背景颜色与其他行对齐(由于行跨度,当前仅与第一行一起使用)。希望这是有道理的,有人可以为此提供帮助。
$(document).ready(function() {
$("#hide").click(function() {
$(".toggle").hide();
});
$("#show").click(function() {
$(".toggle").show();
});
$("#data tr td").click(function() {
$(this).parents(".pointer").next().toggle();
});
});
table {
border-collapse: collapse;
margin-top: 20px;
}
table,
th {
border: solid 1px;
padding: 10px;
}
tr:hover td {
background: #e5e5e5;
}
.toggle {
display: none;
}
.toggle.active {
display: table-row;
}
.pointer {
cursor: pointer;
border: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="hide">Hide All</button>
<button id="show">Show All</button>
<table>
<tr>
<th>header</th>
<th>header</th>
<th>header</th>
<th>header</th>
<th>header</th>
</tr>
<tbody id="data">
<tr class="pointer">
<td style="border-bottom:1px solid;" >DATA1</td>
<td style="border-bottom:1px solid;">data1</td>
<td style="border-bottom:1px solid;">data1</td>
<td style="border-bottom:1px solid;">data1</td>
<td style="border-bottom:1px solid;">data1</td>
</tr>
<tr class="toggle">
<td></td>
<td colspan="4">toggled area 1a</td>
</tr>
<tr class="pointer">
<td style="border-bottom:1px solid;" rowspan="2">DATA2</td>
<td>data2</td>
<td>data2</td>
<td>data2</td>
<td>data2</td>
</tr>
<tr class="toggle">
<td></td>
<td colspan="4">toggled area 2a</td>
</tr>
<tr class="pointer">
<td style="border-bottom:1px solid;">data2</td>
<td style="border-bottom:1px solid;">data2</td>
<td style="border-bottom:1px solid;">data2</td>
<td style="border-bottom:1px solid;">data2</td>
</tr>
<tr class="toggle">
<td></td>
<td colspan="4">toggled area 2b</td>
</tr>
<tr class="pointer">
<td rowspan="3">DATA3</td>
<td>data3</td>
<td>data3</td>
<td>data3</td>
<td>data3</td>
</tr>
<tr class="toggle">
<td></td>
<td colspan="4">toggled area 3a</td>
</tr>
<tr class="pointer">
<td>data3</td>
<td>data3</td>
<td>data3</td>
<td>data3</td>
</tr>
<tr class="toggle">
<td></td>
<td colspan="4">toggled area 3b</td>
</tr>
<tr class="pointer">
<td>data3</td>
<td>data3</td>
<td>data3</td>
<td>data3</td>
</tr>
<tr class="toggle">
<td></td>
<td colspan="4">toggled area 3c</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
<tbody>
中。 tr.toggle
应该只有一个<td>
,并且应该只有colspan='4'
<td>
中只有第一个<tbody>
具有类.pointer
和rowspan
,其类别等于所述<tbody>
的初始可见行数。 例如。第一个<tbody>
最初有一行,因此td.pointer
中的<tbody>
有一个rowspan='1'
。<td>
中的<td>
以外的tr.toggle
并使其可见时,相应的.pointer
rowspan
增加1。 / li>
.pointer
rowspan
在看不见tr.toggle
时减少1。<tr>
具有不可点击的<td colspan='5'>
,其中包含一个<hr>
。
$("#all").on('click', function(e) {
$(this).toggleClass('show hide');
toggleRow(e);
});
$('.pointer').on('click', toggleRow);
$("td").not('.toggle > td').on('click', toggleRow);
function toggleRow(e) {
if (e.target.matches('td')) {
var toggle = $(this).parent().next('.toggle');
var data = $(this).closest('tbody');
var pointer = data.find('.pointer');
var rowspan = Number(pointer.attr('rowspan'));
toggle.toggle();
if (toggle.is(':visible')) {
pointer.prop('rowspan', `${rowspan+=1}`);
if (e.target.matches('.borderA')) {
data.find('.borderA').css('border-width', '0');
}
} else {
pointer.prop('rowspan', `${rowspan-=1}`);
if (e.target.matches('.borderA')) {
data.find('.borderA').css('border-width', '1px');
}
}
} else if (e.target.matches('#all')) {
$('tbody').each(function() {
var pointer = $(this).find('.pointer');
var rows = $(this).children().length;
var rowspan = Number(pointer.attr('rowspan'));
if ($('#all').hasClass('hide')) {
$('.toggle').show();
pointer.attr('rowspan', rows);
$(this).find('.borderA').css('border-width', '0');
} else {
$('.toggle').hide();
pointer.attr('rowspan', rowspan);
$(this).find('.borderA').css('border-width', '1px');
}
});
}
return false;
};
table {
margin-top: 20px;
border-spacing: 0;
}
table,
th {
border: solid 1px #000;
}
tr:hover td,
tbody:hover .pointer {
background: #e5e5e5;
}
.toggle {
display: none;
text-align: center
}
.pointer {
border: 0;
border-bottom: 1px solid #000;
}
.border,
.borderA {
border-bottom: 1px solid #000;
}
#all {
width: 10ch;
}
#all.hide::before {
content: 'Hide ';
}
#all.show::before {
content: 'Show ';
}
<button id="all" class='show'>All</button>
<table>
<thead>
<tr>
<th>header</th>
<th>header</th>
<th>header</th>
<th>header</th>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<th class="pointer show" rowspan='1'>DATA1</th>
<td class='borderA'>data1</td>
<td class='borderA'>data1</td>
<td class='borderA'>data1</td>
<td class='borderA'>data1</td>
</tr>
<tr class="toggle">
<td class='border' colspan="4">1A</td>
</tr>
</tbody>
<tbody>
<tr>
<th class='pointer show' rowspan="2">DATA2</th>
<td>data2</td>
<td>data2</td>
<td>data2</td>
<td>data2</td>
</tr>
<tr class="toggle">
<td colspan="4">2A</td>
</tr>
<tr>
<td class='borderA'>data2</td>
<td class='borderA'>data2</td>
<td class='borderA'>data2</td>
<td class='borderA'>data2</td>
</tr>
<tr class="toggle">
<td class='border' colspan="4">2B</td>
</tr>
</tbody>
<tbody>
<tr>
<th class="pointer show" rowspan="3">DATA3</th>
<td>data3</td>
<td>data3</td>
<td>data3</td>
<td>data3</td>
</tr>
<tr class="toggle">
<td colspan="4">3A</td>
</tr>
<tr>
<td>data3</td>
<td>data3</td>
<td>data3</td>
<td>data3</td>
</tr>
<tr class="toggle">
<td colspan="4">3B</td>
</tr>
<tr>
<td class='borderA'>data3</td>
<td class='borderA'>data3</td>
<td class='borderA'>data3</td>
<td class='borderA'>data3</td>
</tr>
<tr class="toggle">
<td class='border' colspan="4">3C</td>
</tr>
</tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>