giu icon indicating copy to clipboard operation
giu copied to clipboard

use a loop in layout (or explain how this can be done)

Open MarcelXia opened this issue 2 years ago • 4 comments

Related problem

g.Layout{ for key, record := range records { g.Selectable(record.Name).OnClick(func() { fmt.Println(key) }), } },

Your request

The widget page only show "hard" ways of doing stuff, but for this, I want to see how this kind of things can be done.

Alternative solution

No response

Additional context

No response

MarcelXia avatar Feb 28 '22 17:02 MarcelXia

@MarcelXia you need to use giu.Custom(func() {/*your for loop*/})

gucio321 avatar Feb 28 '22 17:02 gucio321

@MarcelXia You can use the ... operator to unpack a slice into individual variables.

First create a slice of unpackable giu elements, in your case, []*g.SelectableWidget

var selectables []*g.Selectable{}
for key, record := range records {
   selectables = append(selectables, g.Selectable(record.Name).OnClick(func() { fmt.Println(key) }))
}

Then add the unpacked slice in the giu element like this

g.Layout{
  selectables...
}

dqhieuu avatar Apr 18 '22 06:04 dqhieuu

Yah and one more thing. Be aware of the fact that if the records variable is a map the iteration order will be different each frame!

gucio321 avatar Apr 18 '22 06:04 gucio321

@MarcelXia any updates here?

gucio321 avatar Jul 08 '22 19:07 gucio321

Inactive for longer time & code demo above so closing; feel free to ping me for any details or re-opening.

gucio321 avatar May 08 '23 13:05 gucio321