-
Notifications
You must be signed in to change notification settings - Fork 0
/
btableoptions.go
79 lines (70 loc) · 2.08 KB
/
btableoptions.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
package htmllookup
import "fmt"
// Hover adds the "hover" attribute to the btable
func (hp *htmlLookup) Hover() {
hp.AddBTableAttribute("hover")
}
// Bordered adds the "bordered" attribute to the btable
func (hp *htmlLookup) Bordered() {
hp.AddBTableAttribute("bordered")
}
// Striped adds the "striped" attribute to the btable
func (hp *htmlLookup) Striped() {
hp.AddBTableAttribute("striped")
}
// AddBTableAttribute adds an attribute to the bTable
func (hp *htmlLookup) AddBTableAttribute(attribute string) {
hp.bTableAttributes = append(hp.bTableAttributes, attribute)
}
// RenderInRawHtml adds template to bTable to render the column values in raw html
// column can either be an int, string or a []byte
func (hp *htmlLookup) RenderInRawHtml(column interface{}) (err error) {
var cName string
var cIndex int
switch t := column.(type) {
case int:
cName, err = hp.columnName(t)
if err != nil {
return err
}
cIndex = t
case string:
cName = t
cIndex, err = hp.columnIndex(cName)
if err != nil {
return err
}
case []byte:
cName = string(t)
cIndex, err = hp.columnIndex(cName)
if err != nil {
return err
}
default:
return fmt.Errorf("column argument has to be either int, string or []byte")
}
hp.AddBTableTemplate(fmt.Sprintf("<template #cell(%s)=\"data\"><span v-html=\"data.value\"></span></template>", cName))
hp.notHtmlEscapedColumns = append(hp.notHtmlEscapedColumns, cIndex)
return
}
// AddBTableTemplate adds template to the bTable
// I do not know any other application than to render in raw html
func (hp *htmlLookup) AddBTableTemplate(template string) {
hp.bTableTemplates = append(hp.bTableTemplates, template)
}
// columnName returns the column name
func (hp *htmlLookup) columnName(col int) (string, error) {
if col > len(hp.header)-1 {
return "", fmt.Errorf("the column with index %d does not exist", col)
}
return hp.header[col], nil
}
// columnName returns the column name
func (hp *htmlLookup) columnIndex(col string) (int, error) {
for k, c := range hp.header {
if col == c {
return k, nil
}
}
return -1, fmt.Errorf("column not found")
}