话说表格需要合并单元格,并且进行汇总计算,可是愁屎人了~,并且列还不固定,真是各种操作,严重考验我的脑容量呀
tableID:表格的ID
colList:需要合并的列,如果有多个,可以以,分开,此方法为合并行
*/
function mergeCellsByField(tableID, colList) {
//alert(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", {
index: i - tmpA,
field: ColArray[j],
rowspan: tmpA,
colspan: null
});
tTable.datagrid("mergeCells", { //根据ColArray[j]进行合并
index: i - tmpA,
field: "Ideparture",
rowspan: tmpA,
colspan: null
});
tmpA = 1;
}
PerTxt = CurTxt;
}
}
}
添加一行
$('#grid').datagrid('insertRow',{
index: n, //索引
row: ovar //对象
});
列进行区分合并
/**
* 表格列属性
* @returns {Array}
*/
function gridColumns(type){
var columns = [ [],[] ];//第一个表示最上边的表头,第二个表示这个表头分了几个小表头
for(var i=0;i<type.length;i++){
var infoObj={};
infoObj.field = “dutyNum”+i;
infoObj.title = type[i]+‘值’;
infoObj.colspan = 4; //表示4个小表头,
columns[0].push(infoObj);
var infoCount ={
field : ‘count’+i,
title : ‘次数’,
align : ‘center’,
formatter:function(value,row,index){
if(value){
return value
}else {
return “–”
}
}
};
columns[1].push(infoCount);
}
return columns;
}
function MergeCells(tableID, fldList) {
var Arr = fldList.split(",");
var dg = $(’#’ + tableID);
var fldName;
var RowCount = dg.datagrid(“getRows”).length;
var span;
var PerValue = “”;
var CurValue = “”;
var length = Arr.length - 1;
var rows = dg.datagrid(“getRows”);
for (i = length; i >= 0; i--) {
fldName = Arr[i];
PerValue = "";
span = 1;
for (row = 0; row <= RowCount; row++) {
if (row == RowCount) {
CurValue = "";
}
else {
CurValue = dg.datagrid("getRows")[row][fldName];
}
//同组信息合并单元格,添加限制条件 ------------此处是关键
if (PerValue == CurValue &&rows[row-1]&&rows[row]["id"] == rows[row - 1]["id"])
{
span += 1;
}
else {
var index = row - span;
dg.datagrid('mergeCells', {
index: index,
field: fldName,
rowspan: span,
colspan: null
});
span = 1;
PerValue = CurValue;
}
}
}
}