/
table.go
64 lines (54 loc) · 1.38 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
package main
import (
"fyne.io/fyne"
"fyne.io/fyne/container"
"fyne.io/fyne/widget"
)
type Table struct {
columnsRows [][]*widget.Label
columns []*fyne.Container
container *fyne.Container
}
func NewTable() *Table {
return &Table{columnsRows: [][]*widget.Label{}, columns: []*fyne.Container{}}
}
func (t *Table) Update(headings []string, rows [][]string) {
columnsData := rowsToColumns(headings, rows)
for i, col := range columnsData {
if len(t.columnsRows) <= i {
t.columnsRows = append(t.columnsRows, []*widget.Label{})
}
row := t.columnsRows[i]
columnData := append([]string{headings[i]}, col...)
for j, elem := range columnData {
if len(row) <= j {
row = append(row, widget.NewLabel(elem))
}
row[j].Text = elem
}
t.columnsRows[i] = row
if len(t.columns) <= i {
t.columns = append(t.columns, container.NewGridWithColumns(1, []fyne.CanvasObject{}...))
}
objects := []fyne.CanvasObject{}
for _, o := range t.columnsRows[i] {
objects = append(objects, o)
}
column := t.columns[i]
column.Objects = objects
column.Refresh()
}
objects := []fyne.CanvasObject{}
for _, o := range t.columns {
objects = append(objects, o)
}
if t.container == nil {
t.container = container.NewHBox(objects...)
} else {
t.container.Objects = objects
t.container.Refresh()
}
}
func (t *Table) GetContainer() *fyne.Container {
return t.container
}