在Go Fyne中绑定表格数据

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

Binding Table data in Go Fyne

问题

Fyne初学者在这里。

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

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

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

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

type Todo struct {
  UserID    int    `json:"userId,omitempty"`
  ID        int    `json:"id,omitempty"`
  Title     string `json:"title,omitempty"`
  Completed bool   `json:"completed,omitempty"`
}

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


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

var data []Todo

stringData := `[{"userId":1,"id":1,"title":"delectus aut autem"},{"userId":1,"id":2,"title":"quis ut nam facilis et officia qui"}]`
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) {
    title, _ := bindings[i.Row].GetItem("Title")
    log.Println(title)
    o.(*widget.Label).SetText(title)
  }
)

我无法访问我的元素的实际值(例如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…

data := binding.BindStringList(
	&[]string{"Item 1", "Item 2", "Item 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:

type Todo struct {
  UserID    int    `json:"userId,omitempty"`
  ID        int    `json:"id,omitempty"`
  Title     string `json:"title,omitempty"`
  Completed bool   `json:"completed,omitempty"`
}

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


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

var data []Todo

stringData := `[{"userId":1,"id":1,"title":"delectus aut autem"},{"userId":1,"id":2,"title":"quis ut nam facilis et officia qui"}]`
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) {
    title, _ := bindings[i.Row].GetItem("Title")
    log.Println(title)
    o.(*widget.Label).SetText(title)
  }
)

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循环如下所示,现在我得到了预期的结果:

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

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

英文:

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

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

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:

确定