-
Notifications
You must be signed in to change notification settings - Fork 6
/
column_button.go
120 lines (105 loc) · 3.42 KB
/
column_button.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
package column
import (
"context"
"github.com/goradd/goradd/pkg/page/event"
"github.com/goradd/goradd/pkg/javascript"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/control"
"github.com/goradd/html5tag"
)
// ButtonColumnClick returns an event that detects a click on the icon in the column.
// The EventValue will be the row value clicked on.
// If you need to also know the column clicked on, you can set the Action's action value to:
//
// javascript.NewJsCode(g$(event.goradd.match).columnId())
//
// and then get the value from the ActionValue.
func ButtonColumnClick() *event.Event {
e := event.Click().
Selector("[data-gr-btn-col]").
ActionValue(javascript.JsCode(
`g$(event.goradd.match).closest("tr").data("value")`,
))
return e
}
// ButtonColumn is a column that draws a button that fires the ButtonColumnClick event.
type ButtonColumn struct {
control.ColumnBase
buttonHtml string
buttonAttributes html5tag.Attributes
}
// NewButtonColumn creates a new button column.
func NewButtonColumn() *ButtonColumn {
i := ButtonColumn{}
i.Self = &i
i.Init()
return &i
}
func (c *ButtonColumn) Init() {
c.ColumnBase.Init(c)
c.buttonHtml = `✎` // default to a standard pencil icon
c.buttonAttributes = html5tag.NewAttributes().
AddClass("gr-transparent-btn") // style so button part does not show, only the icon
c.SetIsHtml(true)
}
// ButtonAttributes returns the attributes of the button. You can directly manipulate those.
func (c *ButtonColumn) ButtonAttributes() html5tag.Attributes {
return c.buttonAttributes
}
func (c *ButtonColumn) CellData(ctx context.Context, row int, col int, data interface{}) interface{} {
c.buttonAttributes.SetData("grBtnCol", "1") // make sure we are tagged so event will fire
return html5tag.RenderTag("button", c.buttonAttributes, c.buttonHtml)
}
func (c *ButtonColumn) Serialize(e page.Encoder) {
c.ColumnBase.Serialize(e)
if err := e.Encode(c.buttonHtml); err != nil {
panic(err)
}
if err := e.Encode(c.buttonAttributes); err != nil {
panic(err)
}
}
// SetButtonHtml sets the html to use inside the button. By default a pencil is drawn.
// Use `<i class="fas fa-edit" aria-hidden="true"></i>` for a font awesome edit icon
func (c *ButtonColumn) SetButtonHtml(h string) {
c.buttonHtml = h
}
func (c *ButtonColumn) Deserialize(dec page.Decoder) {
c.ColumnBase.Deserialize(dec)
if err := dec.Decode(&c.buttonHtml); err != nil {
panic(err)
}
if err := dec.Decode(&c.buttonAttributes); err != nil {
panic(err)
}
}
// ButtonColumnCreator creates a column that displays a clickable icon.
type ButtonColumnCreator struct {
// ID will assign the given id to the column. If you do not specify it, an id will be given it by the framework.
ID string
// Title is the static title string to use in the header row
Title string
// IconHtml specifies the html to put inside the button
ButtonHtml string
// ButtonAttributes lets you set the button attributes how you want.
ButtonAttributes html5tag.Attributes
control.ColumnOptions
}
func (c ButtonColumnCreator) Create(ctx context.Context, parent control.TableI) control.ColumnI {
col := NewButtonColumn()
if c.ID != "" {
col.SetID(c.ID)
}
col.SetTitle(c.Title)
if c.ButtonHtml != "" {
col.SetButtonHtml(c.ButtonHtml)
}
if c.ButtonAttributes != nil {
col.buttonAttributes = c.ButtonAttributes
}
col.ApplyOptions(ctx, parent, c.ColumnOptions)
return col
}
func init() {
control.RegisterColumn(ButtonColumn{})
}