-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
243 lines (212 loc) · 6.83 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> MergeCells By Che </title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<link href="miniui.css" rel="stylesheet" type="text/css" />
<style>
i { color:red; }
small { color:blue; }
</style>
<body>
<div id="container" class="mini-splitter" style="width:100%;height:100%;">
<div size="50%" showCollapseButton="true">
<div id="datagrid" class="mini-datagrid" style="width:100%;height:100%;"
url="data1.txt" resultAsData="true" showPager="false">
<div property="columns">
<div field="field_0" name="field_0" headerAlign="center">A</div>
<div field="field_1" width="100" headerAlign="center">B</div>
<div field="field_2" width="100" headerAlign="center">C</div>
<div field="field_3" width="100" headerAlign="center">D</div>
</div>
</div>
</div>
<div showCollapseButton="true">
<div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;"
url="data1.txt" resultAsData="true" showPager="false">
<div property="columns">
<div field="field_0" name="field_0" headerAlign="center" >A</div>
<div field="field_1" width="100" headerAlign="center">B</div>
<div field="field_2" width="100" headerAlign="center">C</div>
<div field="field_3" width="100" headerAlign="center">D</div>
</div>
</div>
</div>
</div>
<script src="jq1.6.2.js" type="text/javascript"></script>
<script src="miniui3.6.js" type="text/javascript"></script>
<script type="text/javascript">
/**
* MergeCells base on MINIUI
* Author is Chen Hongen
**/
mini.parse();
var grid = mini.get("datagrid1");
grid.load();
var grid2 = mini.get("datagrid");
grid2.load();
grid.on("load", function(e){
mini.get("container").setHeight($(document).height());
mergeCells(e);
// 滚动条同步
$('.mini-grid-rows-view').scroll( function() {
$('.mini-grid-rows-view').scrollTop($(this).scrollTop());
$('.mini-grid-rows-view').scrollLeft($(this).scrollLeft());
});
});
// -------------------------------------------------- 带“##”方法,挪用请重写 -------------------------------------
// --------------------------------------------------- 单元格合并方法 begin -------------------------------------
var mergeCells = (function(){
var g, // ## 表格对象
g_col, // 列数
g_row; // 行数
// ## 初始值
var setGrid = function(e) {
g = e.sender;
// 列数
g_col = g.getColumns().length||g.getColumns()[0].columns.length; //
// 行数
g_row = e.data.length;
};
// ## 根据坐标(x,y)获取对应值,从0开始
var getCellVal = function(y, x) {
return g.getRow(y)["field_"+(x)];
};
// ## 根据json数组合并单元格
var mergeCell = function(arr) {
g.mergeCells(arr);
}
// 对角线方向[初始列标,初始行标,截止列标,截止行标,合并行数,合并列数]
var oppositeVal = function(colIndex, rowIndex, i, j, rowSpan, colSpan) {
var merge = {
colSpan: colSpan,
rowSpan: rowSpan,
columnIndex: colIndex,
rowIndex: rowIndex
};
// 行的各列值相等,则行扩展
var isX = true;
// 行范围控制
if(j+1 > g_row-1) {
isX = false;
} else {
for(var m=colIndex; m < i; m++) {
if(getCellVal(j+1, m) != getCellVal(j, i)) {
isX = false;
break;
}
}
}
if(isX) {
merge.rowSpan = rowSpan+1;
}
// 列的各行值相等,则列扩展
var isY = true;
// 列范围控制
if(i+1 > g_col-1) {
isY = false;
} else {
for(var n=rowIndex; n < j; n++) {
if(getCellVal(n, i+1) != getCellVal(j, i)) {
//return merge;
isY = false;
break;
}
}
}
if(isY) {
merge.colSpan = colSpan+1;
}
// 行列值存在不等情况 or 对角线值不等 返回
if(!(isX && isY) || getCellVal(j+1, i+1) != getCellVal(j, i)) {
return merge;
}
// 满足条件则:递归
return oppositeVal(colIndex, rowIndex, i+1, j+1, merge.rowSpan, merge.colSpan);
}
// 行方向(即y轴)
function rowVal(i, j, rowSpan) {
var merge = {
colSpan: 1,
rowSpan: rowSpan,
columnIndex: i
};
// 范围控制
if(j+1>g_row-1) {
return merge;
}
// 行值判断
if(getCellVal(j+1, i) != getCellVal(j, i)) {
return merge;
}
// 满足2者:递归
return rowVal(i, j+1, merge.rowSpan+1);
}
// 列方向(即x轴)
function colVal(i, j, colSpan) {
var merge = {
colSpan: colSpan,
rowSpan: 1,
rowIndex: j
};
// 范围控制
if(i+1>g_col-1) {
return merge;
}
// 列值判断
if(getCellVal(j, i+1) != getCellVal(j, i)) {
return merge;
}
// 满足2者:递归
return colVal(i+1, j, merge.colSpan+1);
}
return function() {
var merges = [],
mergesOppo = [];
setGrid([].shift.call(arguments));
for(var i=0; i<g_col; i++) { // 列循环
for(var j=0; j<g_row; j++) { // 行循环
var temp = getCellVal(j, i), // curr_val
xVal = i+1<g_col?getCellVal(j, i+1):-1, // next_col
yVal = j+1<g_row?getCellVal(j+1, i):-1, // next_row
oVal = i+1<g_col && j+1<g_row?getCellVal(j+1, i+1):-1; // next_opp
//loVal = i-1<0||j+1>=g_col?-1:getCellVal(j+1, i-1); // last_opp
//console.log(temp +"-"+ xVal +"-"+ yVal +"-"+ oVal);
// 对角线模式
if(temp == yVal && temp == xVal && temp == oVal) {
var merge = oppositeVal(i, j, i+1, j+1, 2, 2);
// x,y方向同时推进
//i = i+merge.colSpan-1;
j = j+merge.rowSpan-1;
// 最新的插入到最前
mergesOppo.splice(0, 0, merge);
// 行模式(┗ 阶梯模式:列合并行不合)
} else if(temp == yVal) {
//alert(1);
var tVal = j-1<0?-1:getCellVal(j-1, i); // last_row
if(tVal != temp) {
var merge = rowVal(i, j+1, 2);
merge.rowIndex = j;
merges.splice(0, 0, merge);
}
// 列模式( ┓阶梯模式:行合并列不合)
} else if(temp == xVal) {
//alert(2);
var lVal = i-1<0?-1:getCellVal(j, i-1); // last_col
if(temp != oVal && temp != yVal && temp != lVal) {
var merge = colVal(i+1, j, 2);
merge.columnIndex = i;
merges.splice(0, 0, merge);
}
}
}
}
//console.log(mini.encode(merges.concat(mergesOppo)));
mergeCell(merges.concat(mergesOppo));
};
})();
// --------------------------------------------------- 单元格合并方法 end-------------------------------------
</script>
</body>
</html>