function loadData(){ $("#tt").datagrid({ url:"memeber/findAllmemberCount.do", fitColumns:true, loadMsg:"數據正在加載,請等待...", rownumbers:true, singleSelect:true, striped:false, queryParams:{custLicence:$("#licenceNo").val(),userName:$("#mName").val(), muuid:$("#muuid").val(),mobile:$("#mobile").val()}, columns:[[ {field:"currCode",hidden:"true"}, {field:"cityCode",hidden:"true"}, {field:"currName",title:"所屬區域",width:80,halign:"center",align:"center",formatter:_setContent,sortable:"true"}, {field:"cityName",title:"所屬地市",width:180,halign:"center",align:"center",formatter:_setContent,sortable:"true"}, {field:"termCount",title:"運行終端數",width:180,halign:"center",align:"center",formatter:_formatCityCount,sortable:"true"}, {field:"allTermCount",title:"合計終端數",width:180,halign:"center",align:"center",formatter:_setContent,sortable:"true"}, {field:"count",title:"會員數",width:180,halign:"center",align:"center",formatter:_formatCount,sortable:"true"}, {field:"allCount",title:"合計會員數",width:180,halign:"center",align:"center",formatter:_setContent,sortable:"true"}, ]], onLoadSuccess:function(data) { if(data.rows.length==0){ var body = $(this).data().datagrid.dc.body2; body.find('table tbody').append('<tr><td width="'+body.width()+'" style="height: 25px; text-align: center;" colspan="10">沒有數據展現</td></tr>'); } else{ mergeCells(data); } }, pageSize:10, pagination:'true', pageList:[10,20,50] }); }
function mergeCells(data){ //合併列的field數組及對應前提條件filed(爲空則直接內容合併)mergeFiled:合併列的field名,和premiseFiled:合併前邊列的前提條件約束列即只有這個約束列相等時再合併mergeFiled列,如premiseFiled:"",則直接按內容合併,而直接按內容相同與否合併就有一個很大的bug,就是相鄰的行即便不該該合併可是內容相同也合併了,這就很容形成表格合併的效果良莠不齊不是咱們想要的,因此增長了一個premiseFiled屬性來約束合併做爲前提條件,如只有projectID字段(可甚至否一個字段hidden="true"來隱藏此列)相同的狀況下才合併projectName。 var arr =[{mergeFiled:"currName",premiseFiled:"currCode"}, {mergeFiled:"allTermCount",premiseFiled:"currCode"}, {mergeFiled:"allCount",premiseFiled:"currCode"} ]; var dg = $("#tt"); // 要合併的datagrid中的表格id var rowCount = dg.datagrid("getRows").length; var cellName; var span; var perValue = ""; var curValue = ""; var perCondition = ""; var curCondition = ""; var flag = true; var condiName = ""; var length = arr.length - 1; for (i =length; i >= 0; i--) { cellName = arr[i].mergeFiled; condiName = arr[i].premiseFiled; if (condiName != null) { flag = false; } perValue = ""; perCondition = ""; span = 1; for (row = 0; row <= rowCount; row++) { if (row == rowCount) { curValue = ""; curCondition = ""; } else { curValue = dg.datagrid("getRows")[row][cellName]; /* if(cellName=="ORGSTARTTIME"){//特殊處理這個時間字段 curValue =formatDate(dg.datagrid("getRows")[row][cellName],""); } */ if(!flag){ curCondition=dg.datagrid("getRows")[row][condiName]; } } if (perValue == curValue&&(flag||perCondition==curCondition)) { span += 1; } else { var index = row - span; dg.datagrid('mergeCells', { index : index, field : cellName, rowspan : span, colspan : null }); span = 1; perValue = curValue; if(!flag){ perCondition=curCondition; } } } } }