需求合并行,在上一篇的基础上改进。
解决思路
1、获取前排列的rowspan值,存入数组,
2、再循环取出数组,根据rowspan的值合并
3、关键点用到jQuery的$("#xxx tbody td:nth-child(xx)")方法获取指定列,在循环遍历处理
代码:
/** * 依据前面的列内容合并后面的列内容 * @param table_id tableID * @param table_colnum 待合并的列 * @param table_colnum_decide 依据的列 */ function servermergetable_rowspan(table_id, table_colnum, table_colnum_decide) { // 查找依据列是否有相同待合并的 var table_firsttd_decide = ""; var table_currenttd_decide = ""; var table_SpanNum_decide = 0; var array = []; var table_Obj_decide = $(table_id + " tbody td:nth-child(" + table_colnum_decide + ")"); table_Obj_decide.each(function (i) { if (i == 0) { table_firsttd_decide = $(this); table_SpanNum_decide = 1; } else { table_currenttd_decide = $(this); if (table_firsttd_decide.text() == table_currenttd_decide.text()) { table_SpanNum_decide++; } else { table_firsttd_decide = $(this); table_SpanNum_decide = 1; } } //将依据列td的rowspan属性加入数组 array.push(table_SpanNum_decide); }); var table_firsttd = ""; var table_currenttd = ""; var table_SpanNum = 0; var table_Obj = $(table_id + " tbody td:nth-child(" + table_colnum + ")"); table_Obj.each(function (i) { if (i == 0) { table_firsttd = $(this); table_SpanNum = array[i]; } else { table_currenttd = $(this); table_SpanNum = array[i]; if (table_SpanNum == 1) {//rowspan值为1的不合并 table_firsttd = $(this); } else {//否则合并 table_firsttd.attr("rowSpan", table_SpanNum); table_currenttd.remove(); } } }); }