作者fr730149 ()
看板Ajax
标题Re: [问题] JQuery合并td
时间Fri Sep 23 09:44:08 2011
的确是蛮利害的解法,依小弟的程度还写不出来,需要好好消化一下
: http://jsbin.com/oveger/edit#source
: // 从第一个row逆向走访每一个cell
: $($('tr:first td').get().reverse()).each(function(index){
: // $start为比对目标,指向每一组的首项cell
: var $start = $current = $(this);
: var $next;
: var need_remove = $([]);
: // 逆向所以index要算一下
: index = $start.parent().children().length - index - 1;
: // 取得同一个column的下一个cell
: while(($next=$current.parent().next().children().eq(index)).length){
: if($start.html() == $next.html()){
: var rs = (parseInt($start.attr('rowspan'), 10) || 1) + 1;
: $start.attr('rowspan', rs);
: need_remove.push($next);
: }
: else{
: $start = $next;
: }
: $current = $next;
: }
: // 每跑完一个column才移除不要的cell
: need_remove.each(function(){
: this.remove();
: });
: });
: 设rowspan和移除多余的cell的部分
: 或许可以有更好的写法
附带一提,将以上的语法套用以下的html,编辑栏位下的值就无法完全合并…
<table border class="tbspan">
<thead>
<tr>
<th scope="col">帐号</th>
<th scope="col">姓名</th>
<th scope="col">编辑</th>
</tr>
</thead>
<tbody>
<tr class="tbspan">
<td class="Style1">A001</td>
<td class="Style1">素还真</td>
<td class="Style1"><a href="/Home/Edit/A001?target=_blank">编辑</a></td>
</tr>
<tr>
<td class="Style1">A001</td>
<td class="Style1">叶小钗</td>
<td class="Style1"><a href="/Home/Edit/A001?target=_blank">编辑</a></td>
</tr>
<tr class="tbspan">
<td class="Style1">A003</td>
<td class="Style1">剑子仙迹</td>
<td class="Style1"><a href="/Home/Edit/A003?target=_blank">编辑</a></td>
</tr>
<tr>
<td class="Style1">A005</td><td class="Style1">佛剑分说</td>
<td class="Style1"><a href="/Home/Edit/A005?target=_blank">编辑</a></td>
</tr>
<tr class="tbspan">
<td class="Style1">A005</td>
<td class="Style1">佛剑分说</td>
<td class="Style1"><a href="/Home/Edit/A005?target=_blank">编辑</a></td>
</tr>
</tbody>
</table>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.125.235.184
1F:推 No:可能中间多个tbody吧~我写的没去处理tbody那一层 09/23 09:56
2F:推 No:噢~还有thead 09/23 10:08
3F:推 No:另外第一层tr里是th的话,程式码也需要修改为 $('tr:first th') 09/23 10:13
4F:推 No:啊~ 以这个html来看的话,可以直接改 $('tr:eq(1) td') 09/23 10:18
5F:→ No:thead的部分不需要合并的话,直接从tbody内容开始run就可以 09/23 10:18
6F:→ fr730149:我用$($('tbody tr:first td').开始跑,其结果未完全合并 09/23 10:57
7F:→ fr730149:将if($start.html() == $next.html())中的html()改成 09/23 10:58
8F:→ fr730149:text() 就可以了,目前尚未发现bug。原因尚待查… 09/23 10:59
9F:推 No:你编辑的超连结路径不一样,用text()来判断合并不会有问题吗? 09/23 11:04
10F:→ fr730149:喔~对哦 我忽略了连结不同 难怪它不给合 谢谢解答 09/23 11:10