HTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table Builder v2.0</title>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
<link rel="stylesheet" type="text/css" href="./css/tbl.css"/>
<script src="./js/main.js" type="module"></script>
</head>
<body>
<h1>Table Builder v2.0</h1>
<div class="wrapper"></div>
<div class="wrapper"></div>
</body>
</html>
|
cs |
main.js
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
|
import { append_all, fetch_json } from "./fn.js";
import { TableMakerBuilder } from "./tableBuilder.js";
const urlList = [
"./data/tbl-1.json",
"./data/tbl-2.json"
];
init();
async function init(){
const $$wrap = document.getElementsByClassName('wrapper');
const data1 = await fetch_json(urlList[0]).then(json=>json.main);
const data2 = await fetch_json(urlList[1]).then(json=>json.main);
const $tbl_1 = new TableMakerBuilder()
.get_data(data1)
.set_table_class('tbl-read')
.set_colgroup(["15%","15%","15%",null])
.set_class_thtd([
{row:true, col:[0,1,2], clss:["f_center"]}
])
.set_class_tr([{
row:0, clss:["on"]
}])
.set_thead_row(1)
.has_total(true)
.init();
const $tbl_2 = new TableMakerBuilder()
.get_data(data2)
.set_table_class('tbl-read')
.set_colgroup(["24%",null,"24%",null])
.set_tbody_th([{
row: true,
col : [0,2]
}])
.set_class_thtd([
{row:[4], col:[1], clss:["data-unit-m"]},
{row:[4,10,12,14,15,16], col:[3], clss:["data-unit-m2"]},
{row:[5], col:[1,3], clss:["data-unit-m2"]},
{row:[7], col:[1], clss:["data-unit-m2"]},
{row:[6], col:[1,3], clss:["data-unit-percent"]},
{row:[14,15,16], col:[1], clss:["data-unit-dasu"]},
])
.init();
append_all($$wrap[0], $tbl_1);
append_all($$wrap[1], $tbl_2);
}//init
|
cs |
fn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/* [dom을 만든다] */
export function dom_maker(type, clssList = []){
const $el = document.createElement(type);
for(let clss of clssList){$el.classList.add(clss);}
return $el;
}//dom_maker
/* [json을 fetch해서 return 한다] */
export function fetch_json(url){
return fetch(url).then(res=>res.json());
}//fetch_json
/* [부모 요소(첫 parameter)에 자식들(rest)을 추가한다] */
export function append_all($parent,...rest){
for(const item of rest){
$parent.appendChild(item);
}
return $parent;
}//append_all
|
cs |
TableBuilder.js
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
|
import { dom_maker } from "./fn.js";
/* Table Builder */
export class TableMakerBuilder{
get_data(data){
this.data= data;
return this;}
set_table_class(tableClass){
this.tableClass = tableClass;
return this;}
set_colgroup(colG){
this.colG = colG;
return this;}
set_thead_row(theadRowNum){
this.theadRowNum = theadRowNum;
return this;}
set_tbody_th(tbodyTh){
this.tbodyTh = tbodyTh;
return this;}
set_class_thtd(clssThTd){
this.clssThTd = clssThTd;
return this;}
set_class_tr(clssTr){
this.clssTr = clssTr;
return this;
}//set_class_tr
has_total(bool = false){
this.hasTotal = bool;
return this;}
/* 실행 */
init(){
const $result = new TableMaker(this);
return $result.init();
}
}//class-TableMakerBuilder
/* ---------------------------------------- */
class TableMaker{
constructor(builder){
this.data = builder.data ?? null;
this.tableClass = builder.tableClass ?? null;
this.colG = builder.colG ?? null;
this.theadRowNum = builder.theadRowNum ?? null;
this.tbodyTh = builder.tbodyTh ?? null;
this.clssThTd = builder.clssThTd ?? null;
this.clssTr = builder.clssTr ?? null;
this.hasTotal = builder.hasTotal ?? false;
}//constructor
/* 실행 */
init(){
if(!this.data){return;}
const $frag = document.createDocumentFragment();
const $tbl = this.tableClass ? dom_maker("TABLE",[this.tableClass]) : dom_maker("TABLE");
//전체 00건 추가
const $total = this.make_total();
//caption
const $caption = dom_maker("CAPTION");
$caption.textContent = this.data.title;
//colgroup
const $colgroup = dom_maker("COLGROUP");
for(let col of this.colG){
const $col = dom_maker("COL");
if(col){$col.style.width = col;}
$colgroup.appendChild($col);
}//for
//thead
const $thead = this.make_thead();
//tbody
const $tbody = this.make_tbody();
//최종
$tbl.appendChild($caption);
$tbl.appendChild($colgroup);
$thead && $tbl.appendChild($thead);
$tbl.appendChild($tbody);
$total && $frag.appendChild($total);
$frag.appendChild($tbl);
return $frag;
}//init
/* 전체 00건 */
make_total(){
if(!this.hasTotal) return;
const $div = dom_maker("DIV",['tbl-total']);
const len = this.data.list.length - ((this.theadRowNum || 0) * this.colG.length);
$div.innerHTML = `전체 <strong>${len}</strong>건`;
return $div;
}//make_total
make_thead(){
if(!this.theadRowNum){return;}
const $thead = dom_maker("THEAD");
const $$data = this.data.list.slice(0, this.theadRowNum * this.colG.length);
const per = Math.floor(this.colG.length / 2);
for(let r=0; r<this.theadRowNum; r++){
const $tr = dom_maker("TR");
for(let c=0; c<this.colG.length; c++){
const $th = dom_maker("TH");
$th.classList.add('f_center');
const $span = dom_maker("SPAN");
const dataIdx = (this.theadRowNum * r) + c + (r * per);
$span.title = $$data[dataIdx].name;
$span.textContent = $$data[dataIdx].value;
$th.appendChild($span);
$tr.appendChild($th);
}//for-c
$thead.appendChild($tr);
}//for-r
return $thead;
}//make_thead
make_tbody(){
const $tbody = dom_maker("TBODY");
let $$data;
if(this.theadRowNum){
$$data = this.data.list.slice(this.theadRowNum * this.colG.length);
}else{
$$data = this.data.list.slice();
}
const rowLen = Math.ceil($$data.length / this.colG.length);
for(let r=0; r<rowLen; r++){
const $tr = dom_maker("TR");
//tr에 클래스 추가
if(this.clssTr){
for(let obj of this.clssTr){
if(obj.row === r){
obj.clss.forEach(clss => $tr.classList.add(clss));
}
}//for
}//if
//th,td 추가
for(let c=0; c<this.colG.length; c++){
//th인지 td인지 판별
const $thtd = this.is_th_td(r,c);
const $span = dom_maker("SPAN");
//클래스 있으면 클래스 추가
this.add_class($thtd,r,c);
const dataIdx = (r*this.colG.length) + c;
if($$data[dataIdx]){
//내용 추가
const {name,value,id} = $$data[dataIdx];
$span.title = name ?? "";
$span.textContent = value ?? "";
//id가 있으면 id를 달고
if(id) $thtd.setAttribute('dataset-data-id',id);
}
$thtd.appendChild($span);
$tr.appendChild($thtd);
}//for-c
$tbody.appendChild($tr);
}//for-r
return $tbody;
}//make_tbody
/* [th인지 td인지 판별] */
is_th_td(r,c){
if(!this.tbodyTh) return dom_maker("TD");
for(let obj of this.tbodyTh){
if(obj.row === true || obj.row === r){
if(obj.col === true || obj.col.includes(c)){
return dom_maker("TH");
}else{
return dom_maker("TD");
}
}
return dom_maker("TD")
}//for
}//is_th_td
/* [클래스 있으면 추가] */
add_class($thtd,r,c){
if(!this.clssThTd) return;
for(let obj of this.clssThTd){
const isR = obj.row === true || obj.row.includes(r);
const isC = obj.col === true || obj.col.includes(c);
const {clss} = obj;
if(isR && isC && clss.length > 0){
clss.forEach(clssname => $thtd.classList.add(clssname));
}
}//for
}//add_class
}//class-TableMaker
|
cs |
json 예제
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
|
{
"main" : {
"title" : "건축물대장 목록",
"list" : [
{"name" : "산구분", "value" : "산구분"},
{"name" : "본번", "value" : "본번"},
{"name" : "부번", "value" : "부번"},
{"name" : "건물명칭 및 번호", "value" : "건물명칭 및 번호"},
{"name" : "산구분", "value" : "대지", "id":"test-01"},
{"name" : "본번", "value" : "0319", "id":"test-01"},
{"name" : "부번", "value" : "0000", "id":"test-01"},
{"name" : "건물명칭 및 번호", "value" : "땡땡시청사 1동", "id":"test-01"},
{"name" : "산구분", "value" : "대지", "id":"test-02"},
{"name" : "본번", "value" : "0319", "id":"test-02"},
{"name" : "부번", "value" : "0000", "id":"test-02"},
{"name" : "건물명칭 및 번호", "value" : "땡땡시청사 1동", "id":"test-02"},
{"name" : "산구분", "value" : "대지", "id":"test-03"},
{"name" : "본번", "value" : "0319", "id":"test-03"},
{"name" : "부번", "value" : "0000", "id":"test-03"},
{"name" : "건물명칭 및 번호", "value" : "땡땡시청사 1동", "id":"test-03" },
{"name" : "산구분", "value" : "대지", "id":"test-01"},
{"name" : "본번", "value" : "0319", "id":"test-01"},
{"name" : "부번", "value" : "0000", "id":"test-01"},
{"name" : "건물명칭 및 번호", "value" : "땡땡시청사 1동", "id":"test-01"},
{"name" : "산구분", "value" : "대지", "id":"test-02"},
{"name" : "본번", "value" : "0319", "id":"test-02"},
{"name" : "부번", "value" : "0000", "id":"test-02"},
{"name" : "건물명칭 및 번호", "value" : "땡땡시청사 1동", "id":"test-02"},
{"name" : "산구분", "value" : "대지", "id":"test-03"},
{"name" : "본번", "value" : "0319", "id":"test-03"},
{"name" : "부번", "value" : "0000", "id":"test-03"},
{"name" : "건물명칭 및 번호", "value" : "땡땡시청사 1동", "id":"test-03" }
]
}
}
|
cs |
'CSS&JS > ⚡Thinkers' 카테고리의 다른 글
[JS] vanilla JavaScript로 jQuery ui의 Draggable과 Resizable 구현하기 (0) | 2022.09.07 |
---|---|
clipText (0) | 2022.07.25 |
Table Builder (0) | 2022.07.08 |
[JS]바닐라 자바스크립트로 24시간 쿠키 모달 끄기/켜기 설정하기 (0) | 2022.04.22 |
[JS]vanilla JS - class 문법으로 모달창 구현하기 (draggable) (0) | 2022.04.06 |