-
Notifications
You must be signed in to change notification settings - Fork 6
/
table_paged.go
157 lines (140 loc) · 4.94 KB
/
table_paged.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
package control
import (
"context"
"github.com/goradd/gengen/pkg/maps"
"github.com/goradd/goradd/pkg/page"
"github.com/goradd/goradd/pkg/page/action"
)
type PagedTableI interface {
TableEmbedder
PagedControlI
}
type PagedTable struct {
Table
PagedControl
}
func NewPagedTable(parent page.ControlI, id string) *PagedTable {
t := &PagedTable{}
t.Self = t
t.Init(parent, id)
return t
}
func (t *PagedTable) Init(parent page.ControlI, id string) {
t.Table.Init(parent, id)
t.PagedControl.SetPageSize(0) // use the application default
}
// MarshalState is an internal function to save the state of the control
func (t *PagedTable) MarshalState(m maps.Setter) {
t.PagedControl.MarshalState(m)
}
// UnmarshalState is an internal function to restore the state of the control
func (t *PagedTable) UnmarshalState(m maps.Loader) {
t.PagedControl.UnmarshalState(m)
}
func (t *PagedTable) Serialize(e page.Encoder) (err error) {
if err = t.Table.Serialize(e); err != nil {
return
}
if err = t.PagedControl.Serialize(e); err != nil {
return
}
return
}
func (t *PagedTable) Deserialize(dec page.Decoder) (err error) {
if err = t.Table.Deserialize(dec); err != nil {
return
}
if err = t.PagedControl.Deserialize(dec); err != nil {
return
}
return
}
// PagedTableCreator creates a table that can be paged
type PagedTableCreator struct {
// ID is the control id
ID string
// Caption is the content of the caption tag, and can either be a string, or a data pager
Caption interface{}
// HideIfEmpty will hide the table completely if it has no data. Otherwise, the table and headers will be shown, but no data rows
HideIfEmpty bool
// HeaderRowCount is the number of header rows. You must set this to at least 1 to show header rows.
HeaderRowCount int
// FooterRowCount is the number of footer rows.
FooterRowCount int
// RowStyler returns the attributes to be used in a cell.
RowStyler TableRowAttributer
// RowStylerID is a control id for the control that will be the RowStyler of the table.
RowStylerID string
// HeaderRowStyler returns the attributes to be used in a header cell.
HeaderRowStyler TableHeaderRowAttributer
// HeaderRowStylerID is a control id for the control that will be the HeaderRowStyler of the table.
HeaderRowStylerID string
// FooterRowStyler returns the attributes to be used in a footer cell. It can be either a control id or a TableFooterRowAttributer.
FooterRowStyler TableFooterRowAttributer
// FooterRowStylerID is a control id for the control that will be the FooterRowStyler of the table.
FooterRowStylerID string
// Columns are the column creators that will add columns to the table
Columns []ColumnCreator
// DataProvider is the data binder for the table. It can be either a control id or a DataBinder
DataProvider DataBinder
// DataProviderID is the control id of the data binder for the table.
DataProviderID string
// Data is the actual data for the table, and should be a slice of objects
Data interface{}
// Sortable will make the table sortable
Sortable bool
// SortHistoryLimit will set how many columns deep we will remember the sorting for multi-level sorts
SortHistoryLimit int
OnCellClick action.CallbackActionI
page.ControlOptions
// PageSize is the number of rows to include in a page
PageSize int
// SaveState will cause the table to remember what page it was on
SaveState bool
}
// Create is called by the framework to create a new control from the Creator. You
// do not normally need to call this.
func (c PagedTableCreator) Create(ctx context.Context, parent page.ControlI) page.ControlI {
ctrl := NewPagedTable(parent, c.ID)
c.Init(ctx, ctrl)
return ctrl
}
// Init is called by implementations of Buttons to initialize a control with the
// creator. You do not normally need to call this.
func (c PagedTableCreator) Init(ctx context.Context, ctrl PagedTableI) {
sub := TableCreator {
ID: c.ID,
Caption: c.Caption,
HideIfEmpty: c.HideIfEmpty,
HeaderRowCount: c.HeaderRowCount,
FooterRowCount: c.FooterRowCount,
RowStyler: c.RowStyler,
RowStylerID: c.RowStylerID,
HeaderRowStyler: c.HeaderRowStyler,
HeaderRowStylerID: c.HeaderRowStylerID,
FooterRowStyler: c.FooterRowStyler,
FooterRowStylerID: c.FooterRowStylerID,
Columns: c.Columns,
DataProvider: c.DataProvider,
DataProviderID: c.DataProviderID,
Data: c.Data,
Sortable: c.Sortable,
SortHistoryLimit: c.SortHistoryLimit,
OnCellClick: c.OnCellClick,
ControlOptions: c.ControlOptions,
}
sub.Init(ctx, ctrl)
if c.PageSize != 0 {
ctrl.SetPageSize(c.PageSize)
}
if c.SaveState {
ctrl.SaveState(ctx, true)
}
}
// GetPagedTable is a convenience method to return the table with the given id from the page.
func GetPagedTable(c page.ControlI, id string) *PagedTable {
return c.Page().GetControl(id).(*PagedTable)
}
func init() {
page.RegisterControl(&PagedTable{})
}