CSS&JS/⚡Thinkers

Table Builder

arancia_ 2022. 7. 8. 15:56

 https://ohikmyeong.github.io/tableBuilder/

 

table builder

 

ohikmyeong.github.io

 

데이터 형식

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
{
    "data" : [
        {"th-1" : "th-1"},
        {"th-1" : "th-1"},
        {"th-2" : "th-2"},
        {"th-1-1" : "th-1-1"},
        {"th-1-2" : "th-1-2"},
        {"th-2-1" : "th-2-1"},
        {"th-1-1" : "(1-1)value"},
        {"th-1-2" : "(1-2)value"},
        {"th-2-1" : "(2-1)value"},
        {"th-1-1" : "(1-1)value"},
        {"th-1-2" : "(1-2)value"},
        {"th-2-1" : "(2-1)value"},
        {"th-1-1" : "(1-1)value"},
        {"th-1-2" : "(1-2)value"},
        {"th-2-1" : "(2-1)value"},
        {"th-1-1" : "(1-1)value"},
        {"th-1-2" : "(1-2)value"},
        {"th-2-1" : "(2-1)value"},
        {"th-1-1" : "(1-1)value"},
        {"th-1-2" : "(1-2)value"},
        {"th-2-1" : "(2-1)value"}
    ]
}
cs

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!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</title>
    <link rel="stylesheet" type="text/css" href="./css/style.css"/>
    <script src="./js/main.js" type="module"></script>
</head>
<body>
    <h1>Table Builder 1.0</h1>
    <div id="wrapper">
        <div id="wrap-1"></div><!-- wrap-1 -->
 
        <div id="wrap-2"></div><!-- wrap-2 -->
 
        <div id="wrap-3"></div><!-- wrap-3 -->
    </div><!-- wrapper -->
</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
import { TableBuilderSetting } from "./TableBuilder.js";
 
const wrap1 = document.getElementById('wrap-1');
const wrap2 = document.getElementById('wrap-2');
const wrap3 = document.getElementById('wrap-3');
 
//th가 세로로 배열 (데이터 방향 >>>)
new TableBuilderSetting()
.setCaption("테이블-1-가로")
.setWrapper(wrap1)
.getData('./../data/tbl-1.json')
.setColGroup(["15%",null,null,null])
.setTHtbody([{
    row : true,
    col : [0]
}])
.init();
 
new TableBuilderSetting()
.setCaption("테이블-1-가로(2)")
.setWrapper(wrap2)
.getData('./../data/tbl-1.json')
.setColGroup(4)
.setTHtbody([{
    row : [1,3],
    col : [0,2]
}])
.init();
 
new TableBuilderSetting()
.setCaption("테이블-2-세로")
.setWrapper(wrap3)
.getData('./../data/tbl-2.json')
.setColGroup(4)
.isThead(true)
.init();
 
//얘는 상속으로 rowspan...colspan...class 달기...
new TableBuilderSetting()
.setCaption("테이블-2-세로(3)")
.getData('./../data/tbl-3.json')
.setColGroup(["200px","200px",null])
.isThead(true,2)
.init();
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
export class TableBuilderSetting{
    /* 테이블 넣을 wrapper */
    setWrapper($dom){
        this.$dom = $dom;
        return this;
    }
    /* data fetch 할 url 받아오기 */
    getData(url){
        this.url = url;
        return this;}
 
    /* set caption */
    setCaption(str){
        this.caption = str;
        return this;
    }
 
    /* set colgroup */
    setColGroup(colgroup){
        this.colgroup = colgroup;
        return this;
    }//setColGroup
 
    /* thead가 있는지 없는지 */
    isThead(bool,num = 1){
        this.hasThead = bool;
        this.theadRowNum = bool && num;
        return this;
    }
 
    /* tbody에서 th가 될 부분 */
    setTHtbody(arr){
        this.bodyTH = arr; 
        return this;
    }//setTHtbody
        
    /* 실행 */
    init(){ return new TableBuilder(this);}
}//TableBuilderSetting
 
class TableBuilder{
    constructor(Setting){
        this.$dom = Setting.$dom ? Setting.$dom : document.body;
        this.data = undefined;
        if(Setting.url) this.url = Setting.url;
        if(Setting.caption) this.caption = Setting.caption;
        if(Setting.colgroup) this.colgroup = Setting.colgroup;
        if(Setting.hasThead) this.hasThead = Setting.hasThead;
        if(Setting.theadRowNum) this.theadRowNum = Setting.theadRowNum;
        if(Setting.bodyTH) this.bodyTH = Setting.bodyTH;
        
        this.$tbl = document.createElement('TABLE');
 
        this.init();
    }//constructor
 
    async init(){
        console.log(this);
 
        await this.set_data();
        //caption 세팅
        const $caption = domMaker('CAPTION'this.caption);
        
        //colgroup 세팅
        const $colgroup = this.draw_colgroup();
 
        //thead가 있는지 판별하고 그림
        const $thead = this.draw_thead();
        
        //tbody 그리기
        const $tbody = this.draw_tbody();
 
        //요소들 tbl에추가
        this.$tbl.appendChild($caption);
        $colgroup && this.$tbl.appendChild($colgroup);
        $thead && this.$tbl.appendChild($thead);
        $tbody && this.$tbl.appendChild($tbody);
 
        //최종 추가
        this.$dom.appendChild(this.$tbl);
    }//init
 
    async set_data(){
        this.data = await this.fetch_data(this.url);
    }//set_data
 
    fetch_data(url){
        return fetch(url).then(res=>res.json()).then(json=>json.data);
    }//fetch_data
 
    
    /* colgroup 만들기 */
    draw_colgroup(){
        const $cg = domMaker('COLGROUP');
        this.set_per();
        
        for(let i=0; i< this.per; i++){
            const $col = domMaker('COL');
            if(typeof this.colgroup == "object"){
                if(this.colgroup[i]) $col.style.width = this.colgroup[i];
            }
            $cg.appendChild($col);
        }//for
 
        return $cg;
    }//draw_colgroup
 
    set_per(){
        const isObject = typeof this.colgroup == "object"
        this.per =  isObject ? this.colgroup.length : this.colgroup;
    }//set_per
 
    /* THEAD 만들기 */
    draw_thead(){
        if(!this.hasThead){return;}
        const $thead = domMaker('THEAD'); 
 
        for(let i=0; i<this.theadRowNum; i++){
            const $tr = domMaker('TR');
            const start = i * this.per;
            const end = start + this.per;
            const DATA = this.data.slice(start,end);
            for(const data of DATA){
                const title = Object.keys(data)[0];
                const $th = domMaker('TH',data[title]);
                $tr.appendChild($th);
            }//for-th
            $thead.appendChild($tr);
        }//for-tr
        
        return $thead;
    }//draw_thead
    
    /* TBODY 만들기 */
    draw_tbody(){
        const $tbody = domMaker("TBODY");
        const start = this.hasThead ? this.per * (this.theadRowNum ?? 1) : 0;
        const DATA = this.data.slice(start);
        const rowNum = DATA.length / this.per;
 
        for(let i=0; i<rowNum; i++){
            const $tr = domMaker("TR");
            for(let k=0; k<this.per; k++){
                const $tt = this.th_or_td_tbody(i,k);
                console.log($tt);
                const [key,val] = Object.entries(DATA[i*this.per + k])[0];
                const $span = domMaker('SPAN',val);
                $span.title = key;
                $tt.appendChild($span);
                $tr.appendChild($tt);
            }//for-td,th
            $tbody.appendChild($tr);
        }//for-row
        return $tbody;
    }//draw_tbody
 
    /* tbody에 TH를 넣을건지 TD를 넣을건지 판별 */
    th_or_td_tbody(i,k){
        if(!this.bodyTH){return domMaker('TD');}
 
        const {row,col} = this.bodyTH[0];
        
        if(row == true || row.includes(i)){
            if(col == true || col.includes(k)) return domMaker('TH');
        }
 
        return domMaker('TD');
    }//th_or_td_tbody
}//TableBuilder
 
function domMaker(type,content = null,clssList = []){
    const $dom = document.createElement(type);
    if(content){$dom.textContent = content;}
    if(clssList.length > 0){
        clssList.forEach(clss => $dom.classList.add(clss));
    }
    return $dom;
}//domMaker
cs