1. 程式人生 > >JS動態根據前面的列合併後面的列

JS動態根據前面的列合併後面的列

需求合併行,在上一篇的基礎上改進。


解決思路

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(); } } }); }