Skip to content

Instantly share code, notes, and snippets.

@gallypette
Created June 22, 2022 12:27
Show Gist options
  • Save gallypette/0a4c031eeba3853a245f20ee15a5af8b to your computer and use it in GitHub Desktop.
Save gallypette/0a4c031eeba3853a245f20ee15a5af8b to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
"time"
)
type Todo struct {
UserID string `json:"userId,omitempty"`
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Completed string `json:"completed,omitempty"`
}
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Table Widget")
myWindow.Resize(fyne.Size{
Width: 600,
Height: 400,
})
var data []Todo
stringData := `[{"userId":"1","id":"1","title":"mon premier titre", "completed":"this is the completed field"},{"userId":"1","id":"2","title":"mon deuxieme titre", "completed":"this is the completed field"}]`
json.Unmarshal([]byte(stringData), &data)
var bindings []binding.DataMap
for _, todo := range data {
bindings = append(bindings, binding.BindStruct(&todo))
}
list := widget.NewTable(
func() (int, int) {
return len(bindings), 4
},
func() fyne.CanvasObject {
return widget.NewLabel("wide content")
},
func(i widget.TableCellID, o fyne.CanvasObject) {
var tmp binding.DataItem
switch i.Col {
case 0:
tmp, _ = bindings[i.Row].GetItem("UserID")
case 1:
tmp, _ = bindings[i.Row].GetItem("ID")
case 2:
tmp, _ = bindings[i.Row].GetItem("Title")
case 3:
tmp, _ = bindings[i.Row].GetItem("Completed")
}
o.(*widget.Label).Bind(tmp.(binding.String))
},
)
go func() {
time.Sleep(time.Duration(3) * time.Second)
var tmp []Todo
stringData := `[{"userId":"2","id":"3","title":"updating", "completed":"BOOO"}]`
json.Unmarshal([]byte(stringData), &tmp)
for _, todo := range tmp {
bindings = append(bindings, binding.BindStruct(&todo))
}
list.Refresh()
}()
myWindow.SetContent(list)
myWindow.ShowAndRun()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment