//datagrid調用列子 onLoadSuccess: function (data) { $(".datagrid-header-row").css("text-align", "center"); if (data.rows.length > 0) { //調用mergeCellsByField()合並單元格 mergeCellsByField("grid", "c0003_stname,c0070_enterprise_name,c0007_name_pk"); } } //參數 tableID 要合並table的id //參數 colList 要合並的列,用逗號分隔(例如:"name,department,office"); ,這邊寫死為 ENT_NAME function mergeCellsByField(tableID, colList) { var ColArray = colList.split(","); var tTable = $("#" + tableID); var TableRowCnts = tTable.datagrid("getRows").length; var tmpA; var tmpB; var PerTxt = ""; var CurTxt = ""; var alertStr = ""; for (j = ColArray.length - 1; j >= 0; j--) { PerTxt = ""; tmpA = 1; tmpB = 0; for (i = 0; i <= TableRowCnts; i++) { if (i == TableRowCnts) { CurTxt = ""; } else { CurTxt = tTable.datagrid("getRows")[i][ColArray[j]]; } if (PerTxt == CurTxt) { tmpA += 1; } else { tmpB += tmpA; tTable.datagrid("mergeCells", { //根據ColArray[j]進行合並 index: i - tmpA, field: ColArray[j], rowspan: tmpA, colspan: null }); tmpA = 1; } PerTxt = CurTxt; } } };
例子: