-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
134 lines (104 loc) · 2.61 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
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
package dhtml
// simple <table> element
type TableElement struct {
tag *Tag // <table>
header *TableRowElement
tbody *Tag
emptyLabel string
}
// force interfaces implementation
var _ ElementI = (*TableElement)(nil)
func NewTable() *TableElement {
return &TableElement{
tag: NewTag("table"),
header: NewTableRow(),
tbody: NewTag("tbody"),
}
}
func (e *TableElement) Class(v ...any) *TableElement {
e.tag.Class(v...)
return e
}
func (e *TableElement) EmptyLabel(label string) *TableElement {
e.emptyLabel = label
return e
}
// Adds header value (<tr><th>v</th></tr>) to the table
func (e *TableElement) Header(v any) *TableElement {
th := NewTag("th").Append(v)
e.header.tag.Append(th)
return e
}
// Adds <TR> to the table data
func (e *TableElement) AppendRow(row *TableRowElement) *TableElement {
e.tbody.Append(row)
return e
}
// Creates new row, appends it to table and returns back
func (e *TableElement) NewRow() (row *TableRowElement) {
row = NewTableRow()
e.AppendRow(row)
return row
}
// Returns added rows count.
func (e *TableElement) RowCount() int {
return e.tbody.ChildrenCount()
}
func (e *TableElement) GetTags() TagList {
//empty label set and no rows added - just show label
if e.emptyLabel != "" && !e.tbody.HasChildren() {
return EmptyLabel(e.emptyLabel).GetTags()
}
if e.header.tag.HasChildren() {
e.tag.Append(NewTag("thead").Append(e.header.tag))
}
if e.tbody.HasChildren() {
e.tag.Append(e.tbody)
}
return e.tag.GetTags()
}
//========== <TR> ===============
type TableRowElement struct {
tag *Tag // <tr>
}
var _ ElementI = (*TableRowElement)(nil)
func NewTableRow() *TableRowElement {
return &TableRowElement{tag: NewTag("tr")}
}
// Add <TD> to the row
func (e *TableRowElement) AppendCell(cell *TableCellElement) *TableRowElement {
e.tag.Append(cell)
return e
}
// Add new <TD> with given content to the row
func (e *TableRowElement) Cell(v any) *TableCellElement {
cell := NewTableCell().Append(v)
e.AppendCell(cell)
return cell
}
func (e *TableRowElement) Class(v ...any) *TableRowElement {
e.tag.Class(v...)
return e
}
func (e *TableRowElement) GetTags() TagList {
return e.tag.GetTags()
}
//========== <TD> ===============
type TableCellElement struct {
tag *Tag // <td>
}
var _ ElementI = (*TableCellElement)(nil)
func NewTableCell() *TableCellElement {
return &TableCellElement{tag: NewTag("td")}
}
func (e *TableCellElement) Class(v ...any) *TableCellElement {
e.tag.Class(v...)
return e
}
func (e *TableCellElement) Append(v any) *TableCellElement {
e.tag.Append(v)
return e
}
func (e *TableCellElement) GetTags() TagList {
return e.tag.GetTags()
}