我有这样的表:
当单元格的内容相同时,我希望在“模型”列中有一个行距。
该表具有过滤器功能,这使“固定” colspan不是解决方案。
我发现了同样的问题:
https://codereview.stackexchange.com/questions/6363/spanning-table-cells-automatically-between-same-value-cells
但可惜对我不起作用。
很好的例子也在这里:
https://stackoverflow.com/a/1294964/1861097,但仅适用于colspan。
对于排序和过滤,我使用http://datatables.net
可以在这里找到表:http://ipt.czechbattlefield.info(切换到附件)
最佳答案
我写了一个关于如何使用jquery行跨任何表的示例。
请注意,该代码仅在以下一种假设下起作用:您希望行跨度的列已排序
You can play with it on Codepen(尝试编辑indexOfColumnToRowSpan)
JS:
$(function(){
//assumption: the column that you wish to rowspan is sorted.
//this is where you put in your settings
var indexOfColumnToRowSpan = 0;
var $table = $('#myTable');
//this is the code to do spanning, should work for any table
var rowSpanMap = {};
$table.find('tr').each(function(){
var valueOfTheSpannableCell = $($(this).children('td')[indexOfColumnToRowSpan]).text();
$($(this).children('td')[indexOfColumnToRowSpan]).attr('data-original-value', valueOfTheSpannableCell);
rowSpanMap[valueOfTheSpannableCell] = true;
});
for(var rowGroup in rowSpanMap){
var $cellsToSpan = $('td[data-original-value="'+rowGroup+'"]');
var numberOfRowsToSpan = $cellsToSpan.length;
$cellsToSpan.each(function(index){
if(index==0){
$(this).attr('rowspan', numberOfRowsToSpan);
}else{
$(this).hide();
}
});
}
})();
的HTML:
<table id="myTable">
<tbody>
<tr>
<th align="left">A</th>
<th align="left">B</th>
<th align="left">C</th>
<th align="left">D</th>
</tr>
<tr>
<td>Apple</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Apple</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Apple</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Apple</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Apple</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Pear</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Pear</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Pear</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Orange</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Orange</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Orange</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Orange</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Orange</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
<tr>
<td>Orange</td>
<td>x</td>
<td>y</td>
<td>z</td>
</tr>
</tbody>
</table>