-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_column_base.go
733 lines (647 loc) · 21.9 KB
/
table_column_base.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package control
import (
"bytes"
"context"
"fmt"
"github.com/goradd/gengen/pkg/maps"
"github.com/goradd/goradd/pkg/base"
"github.com/goradd/goradd/pkg/config"
"github.com/goradd/goradd/pkg/datetime"
"github.com/goradd/goradd/pkg/html"
"github.com/goradd/goradd/pkg/page"
html2 "html"
"strconv"
"time"
)
type SortDirection int
const (
NotSortable = SortDirection(0)
SortAscending = SortDirection(1)
SortDescending = SortDirection(-1)
NotSorted = SortDirection(-100)
)
// SortButtonHtmlGetter is the injected function for getting the html for sort buttons in the column header.
var SortButtonHtmlGetter func(SortDirection) string
// ColumnI defines the interface that all columns must support. Most of these functions are provided by the
// default behavior of the ColumnBase class.
type ColumnI interface {
ID() string
init(self ColumnI)
SetID(string) ColumnI
setParentTable(TableI)
ParentTable() TableI
Title() string
SetTitle(string) ColumnI
Span() int
SetSpan(int) ColumnI
IsHidden() bool
AsHeader() bool
SetHidden(bool) ColumnI
DrawColumnTag(ctx context.Context, buf *bytes.Buffer)
DrawFooterCell(ctx context.Context, row int, col int, count int, buf *bytes.Buffer)
DrawCell(ctx context.Context, row int, col int, data interface{}, buf *bytes.Buffer)
CellText(ctx context.Context, row int, col int, data interface{}) string
CellData(ctx context.Context, row int, col int, data interface{}) interface{}
HeaderCellHtml(ctx context.Context, row int, col int) string
FooterCellHtml(ctx context.Context, row int, col int) string
HeaderAttributes(ctx context.Context, row int, col int) html.Attributes
FooterAttributes(ctx context.Context, row int, col int) html.Attributes
ColTagAttributes() html.Attributes
UpdateFormValues(ctx context.Context)
AddActions(ctrl page.ControlI)
Action(ctx context.Context, params page.ActionParams)
SetCellTexter(s CellTexter) ColumnI
SetHeaderTexter(s CellTexter) ColumnI
SetFooterTexter(s CellTexter) ColumnI
SetCellStyler(s CellStyler)
IsSortable() bool
SortDirection() SortDirection
SetSortDirection(SortDirection) ColumnI
SetSortable() ColumnI
RenderSortButton(labelHtml string) string
SetIsHtml(columnIsHtml bool) ColumnI
PreRender()
MarshalState(m maps.Setter)
UnmarshalState(m maps.Loader)
Serialize(e page.Encoder) (err error)
Deserialize(dec page.Decoder) (err error)
Restore(parentTable TableI)
}
// CellTextInfo is provided to the cell texter so the cell texter knows how to draw.
// Its a struct here so that the info can grow without the CellTexter signature having to change.
type CellInfo struct {
RowNum int
ColNum int
Data interface{}
isHeaderCell bool
isFooterCell bool
}
// CellTexter defines the interface for a structure that provides the content of a table cell.
// If your CellTexter is not a control, you should register it with gob.
type CellTexter interface {
CellText(ctx context.Context, col ColumnI, info CellInfo) string
}
type CellStyler interface {
CellAttributes(ctx context.Context, col ColumnI, info CellInfo) html.Attributes
}
// ColumnBase is the base implementation of all table columns
type ColumnBase struct {
base.Base
id string
parentTable TableI
parentTableID string // for deserializing
title string
html.Attributes // These are static attributes that will appear on each cell
headerAttributes []html.Attributes // static attributes per header row
footerAttributes []html.Attributes
colTagAttributes html.Attributes
span int
asHeader bool
isHtml bool
cellTexter CellTexter
cellTexterID string // for deserialization
headerTexter CellTexter
headerTexterID string // for deserialization
footerTexter CellTexter
footerTexterID string // for deserialization
cellStyler CellStyler // for dynamically styling cells
cellStylerID string // for deserialization
isHidden bool
sortDirection SortDirection
// Format is a format string. It will be applied using fmt.Sprintf. If you don't provide a Format string, standard
// string conversion operations will be used.
format string
// TimeFormat is applied to the data using time.Format. You can have both a Format and TimeFormat, and the Format
// will be applied using fmt.Sprintf after the TimeFormat is applied using time.Format.
timeFormat string
}
func (c *ColumnBase) Init(self ColumnI) {
c.Base.Init(self)
c.Attributes = html.NewAttributes()
}
func (c *ColumnBase) init(self ColumnI) {
c.Init(self)
}
func (c *ColumnBase) this() ColumnI {
return c.Self.(ColumnI)
}
// ID returns the id of the column
func (c *ColumnBase) ID() string {
return c.id
}
// SetID sets the id of the column. If you are going to provide your own id, do this as the first thing after you create
// a table, or the new id might not propagate through the system correctly. Note that the id in html will have the table
// id prepended to it. This is required so that actions can be routed to a column.
func (c *ColumnBase) SetID(id string) ColumnI {
c.id = id
return c.this()
}
func (c *ColumnBase) setParentTable(t TableI) {
c.parentTable = t
}
// ParentTable returns the table that is the parent of the column
func (c *ColumnBase) ParentTable() TableI {
return c.parentTable
}
// Title returns the title text that will appear in the header of the column
func (c *ColumnBase) Title() string {
return c.title
}
// SetTitle sets the title of the column. It returns a column reference for chaining.
func (c *ColumnBase) SetTitle(title string) ColumnI {
c.title = title
return c.this()
}
// Span returns the number of columns this column will span. If the span is not set, it will return 1.
func (c *ColumnBase) Span() int {
if c.span < 2 {
return 1
} else {
return c.span
}
}
// SetSpan sets the span indicated in the column tag of the column. This is used to create colgroup tags.
func (c *ColumnBase) SetSpan(span int) ColumnI {
c.span = span
return c.this()
}
// SetAsHeader will cause the entire column to be output with th instead of td cells.
func (c *ColumnBase) SetAsHeader(r bool) {
c.asHeader = r
}
func (c *ColumnBase) AsHeader() bool {
return c.asHeader
}
func (c *ColumnBase) SetFormat(format string) ColumnI {
c.format = format
return c.this()
}
func (c *ColumnBase) SetTimeFormat(timeFormat string) ColumnI {
c.timeFormat = timeFormat
return c.this()
}
// SetIsHtml will cause the cell to treat the text it receives as html rather than raw text it should escape.
// Use this with extreme caution. Do not display unescaped text that might come from user input, as it could
// open you up to XSS attacks.
func (c *ColumnBase) SetIsHtml(columnIsHtml bool) ColumnI {
c.isHtml = columnIsHtml
return c.this()
}
// SetCellStyler sets the CellStyler for the body cells.
func (c *ColumnBase) SetCellStyler(s CellStyler) {
c.cellStyler = s
}
// SetCellTexter sets the CellTexter for getting the content of each body cell.
func (c *ColumnBase) SetCellTexter(s CellTexter) ColumnI {
c.cellTexter = s
return c.this()
}
// CellTexter returns the cell texter.
func (c *ColumnBase) CellTexter() CellTexter {
return c.cellTexter
}
// SetHeaderTexter sets the CellTexter that gets the text for header cells.
func (c *ColumnBase) SetHeaderTexter(s CellTexter) ColumnI {
c.headerTexter = s
return c.this()
}
// SetFooterTexter sets the CellTexter that gets the text for footer cells.
func (c *ColumnBase) SetFooterTexter(s CellTexter) ColumnI {
c.footerTexter = s
return c.this()
}
// IsHidden returns true if the column is hidden.
func (c *ColumnBase) IsHidden() bool {
return c.isHidden
}
// SetHidden hides the column without removing it completely from the table.
func (c *ColumnBase) SetHidden(h bool) ColumnI {
c.isHidden = h
return c.this()
}
// HeaderAttributes returns the attributes to use on the header cell.
// The default version will return an attribute structure which you can use to directly
// manipulate the attributes. If you want something more customized, create your own column and
// implement this function. row and col are zero based.
func (c *ColumnBase) HeaderAttributes(ctx context.Context, row int, col int) html.Attributes {
if len(c.headerAttributes) < row + 1 {
// extend the attributes
c.headerAttributes = append(c.headerAttributes, make([]html.Attributes, row-len(c.headerAttributes)+1)...)
}
if c.headerAttributes[row] == nil {
c.headerAttributes[row] = html.NewAttributes()
}
if row == 0 {
// for screen readers
c.headerAttributes[0].Set("scope", "col")
if c.IsSortable() {
switch c.SortDirection() {
case SortAscending:
c.headerAttributes[0].Set("aria-sort", "ascending")
case SortDescending:
c.headerAttributes[0].Set("aria-sort", "descending")
default:
c.headerAttributes[0].RemoveAttribute("aria-sort")
}
}
}
return c.headerAttributes[row]
}
// FooterAttributes returns the attributes to use for the footer cell.
func (c *ColumnBase) FooterAttributes(ctx context.Context, row int, col int) html.Attributes {
if len(c.footerAttributes) < row + 1 {
// extend the attributes
c.footerAttributes = append(c.footerAttributes, make([]html.Attributes, row-len(c.footerAttributes)+1)...)
}
if c.footerAttributes[row] == nil {
c.footerAttributes[row] = html.NewAttributes()
}
return c.footerAttributes[row]
}
// ColTagAttributes specifies attributes that will appear in the column tag. Note that you have to turn on column
// tags in the table object as well for these to appear.
func (c *ColumnBase) ColTagAttributes() html.Attributes {
if c.colTagAttributes == nil {
c.colTagAttributes = html.NewAttributes()
}
return c.colTagAttributes
}
// DrawColumnTag draws the column tag if one was requested.
func (c *ColumnBase) DrawColumnTag(ctx context.Context, buf *bytes.Buffer) {
if c.isHidden {
return
}
a := c.this().ColTagAttributes()
if c.span > 1 {
a.Set("span", strconv.Itoa(c.span))
}
buf.WriteString(html.RenderTag("col", a, ""))
}
// HeaderCellHtml returns the text of the indicated header cell. The default will call into the headerTexter if it
// is provided, or just return the Label value. This function can also be overridden by embedding the ColumnBase object
// into another object.
func (c *ColumnBase) HeaderCellHtml(ctx context.Context, row int, col int) (h string) {
if c.headerTexter != nil {
info := CellInfo{RowNum: row, ColNum: col, isHeaderCell:true}
h = c.headerTexter.CellText(ctx, c.this(), info)
} else if row == 0 {
h = html2.EscapeString(c.title)
if c.IsSortable() {
h = c.this().RenderSortButton(h)
}
}
return
}
// DrawFooterCell will draw the footer cells html into the given buffer.
func (c *ColumnBase) DrawFooterCell(ctx context.Context, row int, col int, count int, buf *bytes.Buffer) {
if c.isHidden {
return
}
cellHtml := c.this().FooterCellHtml(ctx, row, col)
a := c.this().FooterAttributes(ctx, row, col)
tag := "td"
if c.asHeader {
tag = "th"
}
buf.WriteString(html.RenderTag(tag, a, cellHtml))
}
// FooterCellHtml returns the html to use in the given footer cell.
func (c *ColumnBase) FooterCellHtml(ctx context.Context, row int, col int) string {
if c.footerTexter != nil {
info := CellInfo{RowNum: row, ColNum: col, isFooterCell:true}
return c.footerTexter.CellText(ctx, c.this(), info) // careful, this does not get escaped
}
return ""
}
// DrawCell is the default cell drawing function.
func (c *ColumnBase) DrawCell(ctx context.Context, row int, col int, data interface{}, buf *bytes.Buffer) {
if c.isHidden {
return
}
cellHtml := c.this().CellText(ctx, row, col, data)
if !c.isHtml {
cellHtml = html2.EscapeString(cellHtml)
}
a := c.CellAttributes(ctx, row, col, data)
tag := "td"
if c.asHeader {
tag = "th"
}
buf.WriteString(html.RenderTag(tag, a, cellHtml))
}
// CellText returns the text in the cell. It will use the CellTexter if one was provided.
func (c *ColumnBase) CellText(ctx context.Context, row int, col int, data interface{}) string {
if c.cellTexter != nil {
info := CellInfo{RowNum: row, ColNum: col, Data:data}
return c.cellTexter.CellText(ctx, c.this(), info)
}
d := c.this().CellData(ctx, row, col, data)
return c.ApplyFormat(d)
}
func (c *ColumnBase) CellData(ctx context.Context, row int, col int, data interface{}) interface{} {
return ""
}
// CellAttributes returns the attributes of the cell. Column implementations should call this base version first before
// customizing more. It will use the CellStyler if one was provided.
func (c *ColumnBase) CellAttributes(ctx context.Context, row int, col int, data interface{}) html.Attributes {
if c.Attributes == nil && c.cellStyler == nil {
return nil
}
a := c.Attributes.Copy()
if c.cellStyler != nil {
info := CellInfo{RowNum: row, ColNum: col, Data:data}
a2 := c.cellStyler.CellAttributes(ctx, c.this(), info)
a.Merge(a2)
}
return a
}
// SetSortable indicates that the column should be drawn with sort indicators.
func (c *ColumnBase) SetSortable() ColumnI {
c.sortDirection = NotSorted
return c.this()
}
// IsSortable indicates whether the column is sortable, and has a sort indicator in the head.
func (c *ColumnBase) IsSortable() bool {
return c.sortDirection != NotSortable
}
// UpdateFormValues is called by the system whenever values are sent by client controls.
// This default version does nothing. Columns that need to record information (checkbox columns for example), should
// implement this.
func (c *ColumnBase) UpdateFormValues(ctx context.Context) {}
func (c *ColumnBase) AddActions(ctrl page.ControlI) {}
// Do a table action that is directed at this table
// Column implementations can implement this method to receive private actions that they have added using AddActions
func (c *ColumnBase) Action(ctx context.Context, params page.ActionParams) {}
func (c *ColumnBase) RenderSortButton(labelHtml string) string {
labelHtml += ` ` + c.ParentTable().SortIconHtml(c)
return fmt.Sprintf(`<button class="gr-transparent-btn" onclick="g$('%s').trigger('grsort', '%s'); return false;">%s</button>`, c.parentTable.ID(), c.ID(), labelHtml)
}
// SortDirection returns the current sort direction.
func (c *ColumnBase) SortDirection() SortDirection {
return c.sortDirection
}
// SetSortDirection is used internally to set the sort direction indicator.
func (c *ColumnBase) SetSortDirection(d SortDirection) ColumnI {
c.sortDirection = d
return c.this()
}
// PreRender is called just before the table is redrawn.
func (c *ColumnBase) PreRender() {}
// MarshalState is an internal function to save the state of the control.
func (c *ColumnBase) MarshalState(m maps.Setter) {}
// UnmarshalState is an internal function to restore the state of the control.
func (c *ColumnBase) UnmarshalState(m maps.Loader) {}
type columnBaseEncoded struct {
ID string
Title string
Attributes html.Attributes
HeaderAttributes []html.Attributes
FooterAttributes []html.Attributes
ColTagAttributes html.Attributes
Span int
AsHeader bool
IsHtml bool
IsHidden bool
SortDirection SortDirection
CellTexter interface{}
HeaderTexter interface{}
FooterTexter interface{}
CellStyler interface{}
}
func (c *ColumnBase) Serialize(e page.Encoder) (err error) {
s := columnBaseEncoded{
ID: c.id,
Title: c.title,
Attributes: c.Attributes,
HeaderAttributes: c.headerAttributes,
FooterAttributes: c.footerAttributes,
ColTagAttributes: c.colTagAttributes,
Span: c.span,
AsHeader: c.asHeader,
IsHtml: c.isHtml,
IsHidden: c.isHidden,
SortDirection: c.sortDirection,
CellTexter: c.cellTexter,
HeaderTexter: c.headerTexter,
FooterTexter: c.footerTexter,
CellStyler: c.cellStyler,
}
if ctrl,ok := c.cellTexter.(page.ControlI); ok {
s.CellTexter = ctrl.ID()
}
if ctrl,ok := c.headerTexter.(page.ControlI); ok {
s.HeaderTexter = ctrl.ID()
}
if ctrl,ok := c.footerTexter.(page.ControlI); ok {
s.FooterTexter = ctrl.ID()
}
if ctrl,ok := c.cellStyler.(page.ControlI); ok {
s.CellStyler = ctrl.ID()
}
if err = e.Encode(s); err != nil {
return err
}
return
}
func (c *ColumnBase) Deserialize(dec page.Decoder) (err error) {
var s columnBaseEncoded
if err = dec.Decode(&s); err != nil {
panic(err)
}
c.id = s.ID
c.title = s.Title
c.Attributes = s.Attributes
c.headerAttributes = s.HeaderAttributes
c.footerAttributes = s.FooterAttributes
c.colTagAttributes = s.ColTagAttributes
c.span = s.Span
c.asHeader = s.AsHeader
c.isHtml = s.IsHtml
c.isHidden = s.IsHidden
c.sortDirection = s.SortDirection
if s.CellTexter != nil {
if v,ok := s.CellTexter.(string); ok {
c.cellTexterID = v
} else {
c.cellTexter = s.CellTexter.(CellTexter)
}
}
if s.HeaderTexter != nil {
if v,ok := s.HeaderTexter.(string); ok {
c.headerTexterID = v
} else {
c.headerTexter = s.HeaderTexter.(CellTexter)
}
}
if s.FooterTexter != nil {
if v,ok := s.FooterTexter.(string); ok {
c.footerTexterID = v
} else {
c.footerTexter = s.FooterTexter.(CellTexter)
}
}
if s.CellStyler != nil {
if v,ok := s.CellStyler.(string); ok {
c.cellStylerID = v
} else {
c.cellStyler = s.CellStyler.(CellStyler)
}
}
return
}
func (c *ColumnBase) Restore(parentTable TableI) {
if c.cellTexterID != "" {
c.cellTexter = parentTable.Page().GetControl(c.cellTexterID).(CellTexter)
}
if c.headerTexterID != "" {
c.headerTexter = parentTable.Page().GetControl(c.headerTexterID).(CellTexter)
}
if c.footerTexterID != "" {
c.footerTexter = parentTable.Page().GetControl(c.footerTexterID).(CellTexter)
}
if c.cellStylerID != "" {
c.cellStyler = parentTable.Page().GetControl(c.cellStylerID).(CellStyler)
}
return
}
type ColumnCreator interface {
Create(context.Context, TableI) ColumnI
}
// Columns is a helper to return a group of columns
func Columns(cols ...ColumnCreator) []ColumnCreator {
return cols
}
// ColumnOptions are settings you can apply to all types of table columns
type ColumnOptions struct {
// CellAttributes is a static map of attributes to apply to every cell in the column
CellAttributes html.Attributes
// HeaderAttributes is a slice of attributes to apply to each row of the header cells in the column.
// Each item in the slice corresponds to a row of the header.
HeaderAttributes []html.Attributes
// FooterAttributes is a slice of attributes to apply to each row of the footer cells in the column.
// Each item in the slice corresponds to a row of the footer.
FooterAttributes []html.Attributes
// ColTagAttributes applies attributes to the col tag if col tags are on in the table. There are limited uses for
// this, but in particular, you can style a column and give it an id. Use Span to set the span attribute.
ColTagAttributes html.Attributes
// Span is specifically for col tags to specify the width of the styling in the col tag.
Span int
// AsHeader will cause the entire column to output header tags (th) instead of standard cell tags (td).
// This is useful for columns on the left or right that contain labels for the rows.
AsHeader bool
// IsHtml will cause the text of the cells to NOT be escaped
IsHtml bool
// HeaderTexter is an object that will provide the text of the header cells. This can be either an
// object that you have set up prior, or a string id of a control
HeaderTexter interface{}
// FooterTexter is an object that will provide the text of the footer cells. This can be either an
// object that you have set up prior, or a string id of a control
FooterTexter interface{}
// IsHidden will start the column out in a hidden state so that it will not initially be drawn
IsHidden bool
// Format is a format string applied to the data using fmt.Sprintf
Format string
// TimeFormat is a format string applied specifically to time data using time.Format
TimeFormat string
}
func (c *ColumnBase) ApplyOptions(ctx context.Context, parent TableI, opt ColumnOptions) {
c.Attributes.Merge(opt.CellAttributes)
if opt.HeaderAttributes != nil {
for i,row := range opt.HeaderAttributes {
attr := c.HeaderAttributes(ctx, i, 0)
attr.Merge(row)
}
}
if opt.FooterAttributes != nil {
for i,row := range opt.FooterAttributes {
attr := c.FooterAttributes(ctx, i, 0)
attr.Merge(row)
}
}
if opt.ColTagAttributes != nil {
if c.colTagAttributes == nil {
c.colTagAttributes = html.NewAttributes()
}
c.colTagAttributes.Merge(opt.ColTagAttributes)
}
c.isHidden = opt.IsHidden
if opt.Span != 0 {
c.SetSpan(opt.Span)
}
c.asHeader = opt.AsHeader
if opt.IsHtml {
c.isHtml = true
}
if opt.HeaderTexter != nil {
if s,ok := opt.HeaderTexter.(string); ok {
c.SetHeaderTexter(parent.Page().GetControl(s).(CellTexter))
} else {
c.SetHeaderTexter(opt.HeaderTexter.(CellTexter))
}
}
if opt.FooterTexter != nil {
if s,ok := opt.FooterTexter.(string); ok {
c.SetFooterTexter(parent.Page().GetControl(s).(CellTexter))
} else {
c.SetFooterTexter(opt.FooterTexter.(CellTexter))
}
}
if opt.Format != "" {
c.SetFormat(opt.Format)
}
if opt.TimeFormat != "" {
c.SetTimeFormat(opt.TimeFormat)
}
}
// ApplyFormat is used by table columns to apply the given fmt.Sprintf and time.Format strings to the data.
// It is exported to allow custom cell texters to use it.
func (c *ColumnBase) ApplyFormat(data interface{}) string {
var out string
switch d := data.(type) {
case int:
if c.format == "" {
out = fmt.Sprintf("%d", d)
} else {
out = fmt.Sprintf(c.format, d)
}
case float64:
if c.format == "" {
out = fmt.Sprintf("%f", d)
} else {
out = fmt.Sprintf(c.format, d)
}
case float32:
if c.format == "" {
out = fmt.Sprintf("%f", d)
} else {
out = fmt.Sprintf(c.format, d)
}
case time.Time:
timeFormat := c.timeFormat
if timeFormat == "" {
timeFormat = config.DefaultDateTimeFormat
}
out = d.Format(timeFormat)
if c.format != "" {
out = fmt.Sprintf(c.format)
}
case datetime.DateTime:
timeFormat := c.timeFormat
if timeFormat == "" {
timeFormat = config.DefaultDateTimeFormat
}
out = d.Format(timeFormat)
if c.format != "" {
out = fmt.Sprintf(c.format)
}
case nil:
return ""
default:
if c.format == "" {
out = fmt.Sprint(d)
} else {
out = fmt.Sprintf(c.format, d)
}
}
return out
}