MultiSelectDropdown : How to pre-select objects on load depending on DataTable column? #218
-
This might be a trivial question, but I am failing to find a solution to the following: I have a datatable with people. Each row has a boolean value, which determines if they are active or not. I have tried the following: def populate_active_people(self):
people = anvil.server.call('get_people', ("active", False))
data = []
for o in people:
# Add person to dropdown
data.append({
"key": o['title'],
"value": o,
"subtext": "Active" if bool(o['active']) else "Inactive",
"selected": bool(o['active'])
})
self.active_people_msdd.selected = [o['value'] for o in data if o['selected']]
self.active_people_msdd.items = data This does not work. The dropdown gets populated as expected, but when the dropdown loads, nothing is selected. Am I missing something? I've tried all kinds of different things, but can't get it to work. Let me know, if I should post this on the Anvil Forums instead. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The source of truth for the selected property is the component's state since it changes as the user interacts with it. When you set the selected property it sets options from the dropdown to selected. In your code snippet you set the selected property but the component has no options yet. TL;DR
It's possible that we could do something fancy where we treat this code as an edge case. |
Beta Was this translation helpful? Give feedback.
The source of truth for the selected property is the component's state since it changes as the user interacts with it.
When you set the selected property it sets options from the dropdown to selected.
A user might interact with the dropdown which would change the value of that property.
i.e. the selected property doesn't remember its state from code.
It always checks the components state, which is the source of truth here.
In your code snippet you set the selected property but the component has no options yet.
TL;DR
It's possible that we could do something fancy where we treat …