-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.ts
49 lines (40 loc) · 1.38 KB
/
table.ts
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
import { Div } from "./div";
class Table extends Div {
props :any;
constructor(props:any) {
if (props) {
super(props);
this.props = props;
if (props.rows) {
this.getHTMLfromJSON(props.rows);
this.createNode();
}
if (props.parent) {
this.appendTo(props.parent);
}
}
}
getHTMLfromJSON(lines:any) {
var tableHeader: string = "";
var tableBody: string = "";
Object.entries(lines[0]).forEach(([key, value]) => {
tableHeader = tableHeader.concat(`<th>${key}</th>`);
});
var headNode = `<thead>${tableHeader}</thead>\n`;
for (let line of lines) {
var lineString = "";
Object.entries(line).forEach(([key, value]) => {
lineString = lineString.concat(`<td>${value}</td>`);
});
tableBody = tableBody.concat(`<tr>${lineString}</tr>\n`);
}
var bodyNode = `<tbody>${tableBody}</tbody>`;
if (this.props.header) {
this.htmlString = `<table class="primary">${headNode}\n${bodyNode}</table>`;
} else {
this.htmlString = `<table class="primary">${bodyNode}</table>`;
}
//console.log(this.htmlString);
}
}
export { Table };