-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
575 lines (498 loc) · 20.7 KB
/
index.html
File metadata and controls
575 lines (498 loc) · 20.7 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table {
border-collapse: collapse;
}
th,
td {
border: 1px solid black;
}
</style>
</head>
<body>
<script>
//깊이 구하기
function calculateDepth(jsonArray) {
let maxDepth = 1; // 최소한의 깊이는 1로 초기화
for (let i = 0; i < jsonArray.length; i++) {
if (Array.isArray(jsonArray[i].child)) {
// 현재 요소가 배열인 경우 재귀 호출을 통해 깊이 계산
const depth = 1 + calculateDepth(jsonArray[i].child);
maxDepth = Math.max(maxDepth, depth);
}
}
return maxDepth;
}
//전체 배열 갯수 구하기
function calculateTotalElements(jsonArray) {
let totalElements = 0;
for (let i = 0; i < jsonArray.length; i++) {
if (Array.isArray(jsonArray[i])) {
// 현재 요소가 배열인 경우 재귀 호출을 통해 요소 갯수 계산
totalElements += calculateTotalElements(jsonArray[i]);
} else {
// 현재 요소가 배열이 아닌 경우 (단일 값인 경우)
totalElements++;
}
}
return totalElements;
}
//다시 작성(배열로 return 해야함)
// function iterateArray(jsonArray) {
// for (let i = 0; i < jsonArray.length; i++) {
// if (Array.isArray(jsonArray[i])) {
// // 현재 요소가 배열인 경우 재귀 호출을 통해 순회
// iterateArray(jsonArray[i]);
// } else {
// // 현재 요소가 배열이 아닌 경우 (단일 값인 경우)
// console.log('배열의 값:', jsonArray[i]);
// }
// }
// }
function iterateArray(jsonArray) {
const result = [];
for (const item of jsonArray) {
if (Array.isArray(item.child)) {
const childArr = iterateArray(item.child);
result.push(...childArr);
} else {
result.push(item);
}
}
return result;
}
//현재 뎁스의 배열 정보 구하기
function getElementsAtDepth(jsonArray, targetDepth, currentDepth = 0) {
// 결과를 저장할 배열
const result = [];
for (const item of jsonArray) {
if (currentDepth === targetDepth) {
// 현재 깊이가 목표 깊이와 일치하면 요소를 결과 배열에 추가
result.push(item);
} else if (Array.isArray(item.child)) {
// 현재 요소가 배열이면 재귀적으로 깊이 우선 탐색 수행
const deeperElements = getElementsAtDepth(item.child, targetDepth, currentDepth + 1);
result.push(...deeperElements);
}
// 기타 경우에는 무시하고 계속 진행
}
return result;
}
//필요한 colspan 계산
function calculateColspan(childArray) {
let colspan = 0;
for (const childColumn of childArray) {
colspan++;
if (childColumn.child && childColumn.child.length > 0) {
// 하위 항목이 있는 경우 재귀적으로 호출하여 colspan 계산
colspan += calculateColspan(childColumn.child) - 1;
}
}
return colspan;
}
//변수 명칭 같은거 좀 정리 해야함.
function make(json) {
let column = json.column;
let table = document.createElement("table");
let thead = document.createElement("thead");
table.appendChild(thead);
//body에도 colspan, rowspan 입력 가능하도록 하려고 하는데.. 이것도 만만치 않다.
//colspan의 경우 tr마다 해당 열의 모든 컬럼의 colspan의 합계를 구해서 어디까지 td를 생성할 것인지 판단해야 함.
//rowspan의 경우 최대 tr의 갯수까지 늘릴 수 있고, 더 넘어가면 최대 tr의 갯수 고정
//type가 object인 경우 현재 키 값에 맞춰서 매핑이 되기 때문에, colspan 또는 rowspan을 사용하는 경우 매핑 되어야 할 부분에 매핑이 되지 못하는 현상이 있을 수 있음...
let depth = calculateDepth(column);
for (let i = 0; i < depth; i++) {
let json = column[i];
let tr = document.createElement("tr");
let curdepth = getElementsAtDepth(column, i);
for (let j = 0; j < curdepth.length; j++) {
let th = document.createElement("th");
//console.log(curdepth[j]);
//rowspan (depth)
let rowspan = 1;
let colspan = 1;
if (curdepth[j].child) {
// console.log(json);
// console.log(calculateDepth(curdepth[j].child));
// console.log(calculateDepth(curdepth[j].child));
// console.log(curdepth[j]);
//console.log(getElementsAtDepth(curdepth[j].child, calculateDepth(curdepth[j].child)-1).length);
//console.log(curdepth[j].child);
//colspan = calculateDepth(curdepth[j].child) + 1;
//colspan = getElementsAtDepth(curdepth[j].child, calculateDepth(curdepth[j].child) - 1).length;
//console.log(curdepth[j].name, "colspan : " + calculateColspan(curdepth[j].child));
colspan = calculateColspan(curdepth[j].child);
} else {
//child가 없는 경우에는 전체 tr 갯수에서 i 뺀 갯수
//rowspan = depth;
rowspan = depth - i;
}
//console.log("\n");
if (rowspan > 1) {
th.rowSpan = rowspan;
}
if (colspan > 1) {
th.colSpan = colspan;
}
//나중에 css라던가, align, width 같은거는 option으로 하나 만들려고 함
if (curdepth[j].width) {
//th.width = curdepth[j].width;
th.style.width = curdepth[j].width + "px";
}
th.appendChild(document.createTextNode(curdepth[j].name));
tr.appendChild(th);
}
thead.appendChild(tr);
}
table.appendChild(thead);
let body = json.data;
if (body) {
let tbody = document.createElement("tbody");
//전체 body의 rowspan 합계
//같은 열의 rowspan의 합계는 전체 body의 row를 넘어갈 수 없음
//컬럼의 갯수만큼 배열 크기 생성해야 함.
let rowspanSum = 0;
for (let i = 0; i < body.length; i++) {
let tr = document.createElement("tr");
let it = iterateArray(column);
if (Array.isArray(body[i])) {
//항목이 array인 경우
let content = body[i];
//남아있는 colspan
let remainColspan = 0;
for (let j = 0; j < it.length; j++) {
//colspan이 적용되는 부분이 남았다면
if(remainColspan > 1) {
remainColspan--;
//해당 부분은 건너뛰기
continue;
}
let td = document.createElement("td");
let curColumn = content[j];
let columnValue = "";
let columnJson = {};
//해당 데이터 부분이 object인지 string인지 판단
if(typeof curColumn === "object") {
console.log(curColumn);
//columnValue = curColumn.value;
columnJson = curColumn;
} else if(typeof curColumn == "string") {
columnValue = curColumn;
}
let rowspan = 1;
let colspan = 1;
if(columnJson) {
if(columnJson.value) {
columnValue = curColumn.value;
}
if(columnJson.colSpan) {
colspan = curColumn.colSpan;
//colspanSum += colspan;
remainColspan = colspan;
console.log(colspan);
}
if(columnJson.rowSpan) {
rowspan = curColumn.rowSpan;
}
if (rowspan > 1) {
td.rowSpan = rowspan;
}
if (colspan > 1) {
let maxColspan = it.length - j;
td.colSpan = (colspan > maxColspan) ? maxColspan : colspan;
}
}
td.appendChild(document.createTextNode(columnValue ? columnValue : ""));
tr.appendChild(td);
}
} else if (typeof body[i] === "object") {
//항목이 json인 경우
//head의 컬럼명이 body와 key값으로 비교하는데.. 실 헤더 데이터와, 이걸 키값과 비교해야하기에 이 부분에 대해 조금 변경해야하지 않나 싶음.
//head 선언 시에 key값 변수 하나 추가 후에 이거와 비교하는게 맞을듯..
//let keyarray = Object.keys(body[i]);
//해당 row의 colspan 합계
//let colspanSum = 0;
//남아있는 colspan
let remainColspan = 0;
for (let j = 0; j < it.length; j++) {
//colspan이 적용되는 부분이 남았다면
if(remainColspan > 1) {
remainColspan--;
//해당 부분은 건너뛰기
continue;
}
let td = document.createElement("td");
let curColumn = body[i][it[j].name];
let columnValue = "";
let columnJson = {};
//해당 데이터 부분이 object인지 string인지 판단
if(typeof curColumn === "object") {
//columnValue = curColumn.value;
columnJson = curColumn;
} else if(typeof curColumn == "string") {
columnValue = curColumn;
}
let rowspan = 1;
let colspan = 1;
if(columnJson) {
if(columnJson.value) {
columnValue = curColumn.value;
}
if(columnJson.colSpan) {
colspan = curColumn.colSpan;
//colspanSum += colspan;
remainColspan = colspan;
}
if(columnJson.rowSpan) {
rowspan = curColumn.rowSpan;
}
if (rowspan > 1) {
td.rowSpan = rowspan;
}
if (colspan > 1) {
let maxColspan = it.length - j;
td.colSpan = (colspan > maxColspan) ? maxColspan : colspan;
}
}
//colspanSum이 length보다 넘어간다면
//if()
td.appendChild(document.createTextNode(columnValue ? columnValue : ""));
tr.appendChild(td);
}
}
tbody.appendChild(tr);
}
table.appendChild(tbody);
}
return table;
}
let json = {
column: [
{
name: "1",
width: 60
},
{
name: "2",
width: 120
},
{
name: "3",
width: 120,
child: [
{
name: "3-1"
},
{
name: "3-2"
}
]
},
{
name: "4",
width: 120,
child: [
{
name: "4-1"
},
{
name: "4-2"
},
{
name: "4-3"
}
]
},
{
name: "5",
width: 120,
child: [
{
name: "5-1",
child: [
{
name: "5-1-1"
},
{
name: "5-1-2"
},
{
name: "5-1-3"
},
{
name: "5-1-4",
child: [
{
name: "5-1-4-1",
child: [
{
name: "5-1-4-1-1"
}
]
},
{
name: "5-1-4-2"
}
]
}
]
},
{
name: "5-2",
child: [
{
name: "5-2-1",
width: 100
},
{
name: "5-2-2"
}
]
},
]
},
{
name: "6",
width: 70
},
{
name: "7"
},
{
name: "8"
},
{
name: "9"
},
{
name: "10"
},
{
name: "11"
},
{
name: "12"
},
{
name: "13"
},
{
name: "14"
},
{
name: "15"
},
{
name: "16"
},
{
name: "17"
},
{
name: "18"
},
{
name: "19"
},
{
name: "20"
},
{
name: "21"
},
],
data: [
{
"1": "1번째",
"2": "2번째",
"8": "8번째",
"3-1": "3-1번째",
"3-2": "3-2번째",
"4-1": "4-1번째",
"4-2": "4-2번째",
"5-1-1": "5-1-1번째",
"5-1-2": "5-1-2번째",
"6-1": "6-1번째",
"7-1": "7-1번째",
"5-2": "5-2번째",
"5-1-4-1": "5-1-4-1번째",
"5-2-2": "5-2-2번째",
"16": "16번째"
},
["1", "2", { value : "3-1", colSpan: 32 }, "3-2", "4-1", "4-2", "5-1-1", "5-1-2", "5-2", "6", "7", "8", "9", "10", "11", "12"],
{
"0": {
value: "0번째"
},
"1": "1번째",
"2": "2번째",
"3": "3번째",
"4": "4번째",
"5": "5번째",
"6": "6번째",
"7": "7번째",
"8": "8번째",
"9": "9번째",
"10": {
value: "10번째",
colSpan: 5
},
"11": "11번째",
"12": {
value: "12번째",
colSpan: 15
},
"13": "13번째",
"14": "14번째",
"15": "15번째",
"16": "16번째",
"17": "17번째",
"18": "18번째",
"19": "19번째",
"20": "20번째",
"21": "21번째",
"22": "22번째",
"23": "23번째",
"24": "24번째",
"25": "25번째",
"26": "26번째",
"27": "27번째",
"28": "28번째",
"29": "29번째",
"30": "30번째",
"31": "31번째",
"32": "32번째",
"33": "33번째",
"34": "34번째",
"35": "35번째",
"36": "36번째",
"37": "37번째",
"38": "38번째",
"39": "39번째",
"40": "40번째",
"41": "41번째",
"42": "42번째",
"43": "43번째",
"44": "44번째",
"45": "45번째",
"46": "46번째",
"47": "47번째",
"48": "48번째",
"49": "49번째",
"50": "50번째"
}
]
}
//document.write(make(json));
let result = make(json);
//html 필요하면 이렇게 사용할것.
console.log(result.outerHTML);
document.body.appendChild(result);
</script>
</body>
</html>