-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
88 lines (69 loc) · 1.43 KB
/
table.go
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
package compton
import (
_ "embed"
"github.com/boggydigital/compton/consts/compton_atoms"
"golang.org/x/net/html/atom"
)
type TableElement struct {
*BaseElement
r Registrar
}
func (te *TableElement) AppendHead(columns ...string) *TableElement {
var thead Element
if theads := te.GetElementsByTagName(atom.Thead); len(theads) > 0 {
thead = theads[0]
}
if thead == nil {
thead = Thead()
te.Append(thead)
}
for _, col := range columns {
th := Th()
th.Append(Text(col))
thead.Append(th)
}
return te
}
func (te *TableElement) AppendRow(data ...string) *TableElement {
var tbody Element
if tbodies := te.GetElementsByTagName(atom.Tbody); len(tbodies) > 0 {
tbody = tbodies[0]
}
if tbody == nil {
tbody = Tbody()
te.Append(tbody)
}
tr := Tr()
for _, col := range data {
td := Td()
td.Append(Text(col))
tr.Append(td)
}
tbody.Append(tr)
return te
}
func (te *TableElement) AppendFoot(columns ...string) *TableElement {
var tfoot Element
if tfeet := te.GetElementsByTagName(atom.Tfoot); len(tfeet) > 0 {
tfoot = tfeet[0]
}
if tfoot == nil {
tfoot = Tfoot()
te.Append(tfoot)
}
for _, col := range columns {
td := Td()
td.Append(Text(col))
tfoot.Append(td)
}
return te
}
func Table(r Registrar) *TableElement {
table := &TableElement{
BaseElement: NewElement(tacMarkup(atom.Table)),
r: r,
}
r.RegisterStyles(DefaultStyle,
compton_atoms.StyleName(atom.Table))
return table
}