在Go Fyne中绑定表格数据

huangapple go评论151阅读模式
英文:

Binding Table data in Go Fyne

问题

Fyne初学者在这里。

我正在尝试解决一个简单的用例,在文档中找不到任何解决方案:在Fyne中,如何使用数据源绑定数据到一个表格小部件?

换句话说,文档中有binding.BindStringList,它允许绑定一个字符串列表...

  1. data := binding.BindStringList(
  2. &[]string{"Item 1", "Item 2", "Item 3"},
  3. )

...我正在寻找类似的东西,它可以绑定一个结构体列表而不是字符串。例如,一个待办事项的表格:

  1. type Todo struct {
  2. UserID int `json:"userId,omitempty"`
  3. ID int `json:"id,omitempty"`
  4. Title string `json:"title,omitempty"`
  5. Completed bool `json:"completed,omitempty"`
  6. }

如果不可能,你认为最好的解决方法是什么?


好的,根据Andy的建议,我尝试了这个:

  1. var data []Todo
  2. stringData := `[{"userId":1,"id":1,"title":"delectus aut autem"},{"userId":1,"id":2,"title":"quis ut nam facilis et officia qui"}]`
  3. json.Unmarshal([]byte(stringData), &data)
  4. var bindings []binding.DataMap
  5. for _, todo := range data {
  6. bindings = append(bindings, binding.BindStruct(&todo))
  7. }
  8. list := widget.NewTable(
  9. func() (int, int) {
  10. return len(bindings), 4
  11. },
  12. func() fyne.CanvasObject {
  13. return widget.NewLabel("wide content")
  14. },
  15. func(i widget.TableCellID, o fyne.CanvasObject) {
  16. title, _ := bindings[i.Row].GetItem("Title")
  17. log.Println(title)
  18. o.(*widget.Label).SetText(title)
  19. }
  20. )

我无法访问我的元素的实际值(例如Title)。你能帮忙吗?

英文:

Fyne beginner here.

There is simple use case I'm trying to solve, without finding any solution in the docs: in Fyne, how to have a Table widget, with its data bound to a data source?

In other words, we have the binding.BindStringList in the docs, that allows to bind a list of strings…

  1. data := binding.BindStringList(
  2. &[]string{"Item 1", "Item 2", "Item 3"},
  3. )

…I am looking for something similar that would allow to bind a list of structs instead of string. For example, a table of todos:

  1. type Todo struct {
  2. UserID int `json:"userId,omitempty"`
  3. ID int `json:"id,omitempty"`
  4. Title string `json:"title,omitempty"`
  5. Completed bool `json:"completed,omitempty"`
  6. }

If it is not possible, what would seem like the best workaround for you?


Well, following Andy's suggestion, I tried this:

  1. var data []Todo
  2. stringData := `[{"userId":1,"id":1,"title":"delectus aut autem"},{"userId":1,"id":2,"title":"quis ut nam facilis et officia qui"}]`
  3. json.Unmarshal([]byte(stringData), &data)
  4. var bindings []binding.DataMap
  5. for _, todo := range data {
  6. bindings = append(bindings, binding.BindStruct(&todo))
  7. }
  8. list := widget.NewTable(
  9. func() (int, int) {
  10. return len(bindings), 4
  11. },
  12. func() fyne.CanvasObject {
  13. return widget.NewLabel("wide content")
  14. },
  15. func(i widget.TableCellID, o fyne.CanvasObject) {
  16. title, _ := bindings[i.Row].GetItem("Title")
  17. log.Println(title)
  18. o.(*widget.Label).SetText(title)
  19. }
  20. )

I don't manage to access the actual values of my elements (i.e. Title). Could you help?

答案1

得分: 4

很抱歉,目前还没有一个与数据绑定的Table小部件。
在v2.1中,我们计划添加binding.BindMapList,它将传递给NewTableWithData。在即将发布的版本中。

在那之前,您可以维护一个binding.DataMap项的切片,并访问其中的项,以便在Table更新回调方法中绑定各个项。

英文:

Unfortunately there is not currently a data bound Table widget.
In v2.1 we plan to add binding.BindMapList which would be passed to NewTableWithData. In the upcoming release.

Until then you can maintain a slice of binding.DataMap items and access the items in it to bind the individual items inside the Table update callback methods.

答案2

得分: 0

我处于类似的情况,并尝试在列表中使用你的解决方案。我的所有列表项都显示为最后一项的文本。我修改了range循环如下所示,现在我得到了预期的结果:

  1. for i, _ := range data {
  2. bindings = append(bindings, binding.BindStruct(&data[i]))
  3. }

我的用例与你的有些不同,但希望这能传达概念。

英文:

I am in a similar situation and tried your solution in a list. All of my list items were displaying the text of the last item. I modified the range loop as follows and I now get expected results

  1. for i, _ := range data {
  2. bindings = append(bindings, binding.BindStruct(&data[i]))
  3. }

My use case is a bit different than yours, but hopefully this will convey the concept.

huangapple
  • 本文由 发表于 2021年6月22日 22:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/68085584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定