-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
322 lines (287 loc) · 6.11 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package objhtml
import (
"fmt"
"strconv"
// "golang.org/x/net/html"
)
//=============================================
// Table //
//=============================================
type (
//Table represents <table>
Table struct {
*Element
Head *TableHead
Body *TableBody
Footer *TableFooter
Colgroup *TableColgroup
Caption *Element
Rows []*TableRow
}
//TableColgroup represents a <colgroup>
TableColgroup struct {
*Element
Span int
Cols []*TableCol
}
//TableCol represents a <col>
TableCol struct {
*Element
Span int
Style string
}
//TableHead
TableHead struct {
*Element
Rows []*TableRow
}
//TableBody represents
TableBody struct {
*Element
Rows []*TableRow
}
//TableFooter
TableFooter struct {
*Element
Rows []*TableRow
}
//TableRow represents a <tr>
TableRow struct {
*Element
Cells []*TableCell
}
//TableCell represents a <td>
TableCell struct {
*Element
Header bool
Colspan int
Rowspan int
Content *Element
}
)
//NewTable creates a new table with type
func NewTable(rows ...*TableRow) *Table {
t := new(Table)
t.Element = NewElement("table")
t.AddRows(rows...)
return t
}
//AddColgroup adds a head
func (t *Table) AddColgroup(c *TableColgroup) {
if c != nil {
t.Colgroup = c
t.AddElement(c.Element)
}
}
//AddCaption adds a caption
func (t *Table) AddCaption(c *Element) {
if c != nil {
t.Caption = c
t.AddElement(c)
}
}
//AddHead adds a head
func (t *Table) AddHead(h *TableHead) {
if h != nil {
t.Head = h
t.AddElement(h.Element)
}
}
//AddBody adds a body
func (t *Table) AddBody(b *TableBody) {
if b != nil {
t.Body = b
t.AddElement(b.Element)
}
}
//AddFooter adds a footer
func (t *Table) AddFooter(f *TableFooter) {
if f != nil {
t.Footer = f
t.AddElement(f.Element)
}
}
//AddRow adds a row
func (t *Table) AddRow(row *TableRow) {
if row != nil {
t.Rows = append(t.Rows, row)
t.AddElement(row.Element)
}
}
//AddRows adds a rows
func (t *Table) AddRows(rows ...*TableRow) {
if rows != nil {
for _, row := range rows {
t.AddRow(row)
}
}
}
//NewTableColgroup adds an colgroup row
func NewTableColgroup(span int, cols ...*TableCol) *TableColgroup {
c := new(TableColgroup)
c.Element = NewElement("colgroup")
if span > 0 {
c.Span = span
c.SetAttribute("span", strconv.Itoa(int(c.Span)))
}
c.AddColgroupCols(cols...)
return c
}
//AddColgroupCol adds a col
func (c *TableColgroup) AddColgroupCol(col *TableCol) {
if col != nil {
c.Cols = append(c.Cols, col)
c.AddElement(col.Element)
}
}
//AddColgroupCols adds a cols
func (c *TableColgroup) AddColgroupCols(cols ...*TableCol) {
if cols != nil {
for _, col := range cols {
c.AddColgroupCol(col)
}
}
}
//NewTableCol creates a new table colgroup col
func NewTableCol(span int, style string) *TableCol {
c := new(TableCol)
c.Element = NewElement("col")
if span > 0 {
c.Span = span
c.SetAttribute("span", strconv.Itoa(int(c.Span)))
}
if style > "" {
c.Style = style
c.SetAttribute("style", style)
}
return c
}
//NewTableCaption adds an caption
func NewTableCaption(text string) *Element {
c := NewElement("caption")
c.SetText(text)
return c
}
//NewTableHead adds an header row
func NewTableHead(rows ...*TableRow) *TableHead {
thead := new(TableHead)
thead.Element = NewElement("thead")
thead.AddHeadRows(rows...)
return thead
}
//AddHeadRow adds a row
func (h *TableHead) AddHeadRow(row *TableRow) {
if row != nil {
h.Rows = append(h.Rows, row)
h.AddElement(row.Element)
}
}
//AddHeadRows adds a rows
func (h *TableHead) AddHeadRows(rows ...*TableRow) {
if rows != nil {
for _, row := range rows {
h.AddHeadRow(row)
}
}
}
//NewTableBody adds body
func NewTableBody(rows ...*TableRow) *TableBody {
tbody := new(TableBody)
tbody.Element = NewElement("tbody")
tbody.AddBodyRows(rows...)
return tbody
}
//AddBodyRow adds a row
func (b *TableBody) AddBodyRow(row *TableRow) {
if row != nil {
b.Rows = append(b.Rows, row)
b.AddElement(row.Element)
}
}
//AddBodyRows adds a rows
func (b *TableBody) AddBodyRows(rows ...*TableRow) {
if rows != nil {
for _, row := range rows {
b.AddBodyRow(row)
}
}
}
//NewTableFooter adds footer
func NewTableFooter(rows ...*TableRow) *TableFooter {
tfoot := new(TableFooter)
tfoot.Element = NewElement("tfoot")
tfoot.AddFooterRows(rows...)
return tfoot
}
//AddFooterRow adds a row
func (f *TableFooter) AddFooterRow(row *TableRow) {
if row != nil {
f.Rows = append(f.Rows, row)
f.AddElement(row.Element)
}
}
//AddFooterRows adds a rows
func (f *TableFooter) AddFooterRows(rows ...*TableRow) {
if rows != nil {
for _, row := range rows {
f.AddFooterRow(row)
}
}
}
//NewTableRow creates a new table row
func NewTableRow(cells ...*TableCell) *TableRow {
tr := new(TableRow)
tr.Element = NewElement("tr")
tr.AddCells(cells...)
return tr
}
//AddCells adds cell to a table row
func (tr *TableRow) AddCell(cell *TableCell) {
if cell != nil {
tr.Cells = append(tr.Cells, cell)
tr.AddElement(cell.Element)
}
}
//AddCells adds cells to a table row
func (tr *TableRow) AddCells(cells ...*TableCell) {
if cells != nil {
for _, cell := range cells {
tr.AddCell(cell)
}
}
}
//NewTableCell creates and adds new cell
func NewTableCell(header bool, colspan, rowspan int, content *Element) *TableCell {
td := new(TableCell)
//td := NewElement("td")
if header {
td.Element = NewElement("th")
} else {
td.Element = NewElement("td")
}
if colspan > 0 {
td.Colspan = colspan
td.SetAttribute("colspan", strconv.Itoa(int(td.Colspan)))
}
if rowspan > 0 {
td.Rowspan = rowspan
td.SetAttribute("rowspan", strconv.Itoa(int(td.Rowspan)))
}
td.Content = content
td.AddElement(content)
return td
}
//=============================================
// Table utils //
//=============================================
//QuickTable creates a table from a given map with key-value pairs
func QuickTable(data map[string]interface{}) *Table {
t := NewTable()
for key, value := range data {
cell1 := NewTableCell(false, 0, 0, NewText(key))
cell2 := NewTableCell(false, 0, 0, NewText(fmt.Sprintf("%v", value)))
row := NewTableRow([]*TableCell{cell1, cell2}...)
t.AddRow(row)
}
return t
}