-
I'm currently grabbing some information from an endpoint and each bit of information represents an action that can be toggled. I'm curious if there's a way to do this so I can keep track of which item was toggled using the I have created a minimal example of something like I'm trying to accomplish. I apologize if I'm trying to tackle the issue in an entirely incorrect manner. https://gist.github.com/baileyn/8d70ed67b91f36beaddcbac8736ff90a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For your given example, you could simply store the index of the toggled item in the message. enum Message {
ItemToggled(/*index*/usize, /*value*/bool),
} The only thing that then needs to change in the view is to use let col = self
.items
.iter()
.enumerate()
.fold(Column::new().spacing(10), |column, (index, item)| {
column.push(Checkbox::new(
item.checked,
&item.name,
move |b| Message::ItemToggled(index, b),
))
}); And then the index can be used in the update to get the item that was toggled. Message::ItemToggled(index, selected) => {
let toggled_item = &mut self.items[index];
toggled_item.checked = selected;
println!("Item {} was toggled to {}", toggled_item.name, selected);
} Here is the full modified example: |
Beta Was this translation helpful? Give feedback.
For your given example, you could simply store the index of the toggled item in the message.
The only thing that then needs to change in the view is to use
enumerate()
to extract the index from the iterator and store it in the message.And then the index can be used in the upd…