Many thanks to @david.wylie and @aldo.ercolani for all the work!
I needed a version of this to manage one-to-many relationships and was able to create one that’s working well (shared below with custom components and example).
The structure remains the same, but with slightly edited component names (RawItemBox, ItemBox, and MultiSelectItemDropDown). I’m not sure about the use case for setting anything directly in the ItemBox.
Usage/Properties
items: In essence the same as Anvil stock dropdowns, items must be set to a list of tuples with the first half of the tuple being the text to display, and the second half being the actual row to be used in the “many” part of the relationship (as in https://anvil.works/docs/how-to/dropdowns-data-tables). This needs to be the complete list of items available for selection, typically the result of a search()
on the data table on the “many” side of the relationship.
selected_items: same as items, this must be formatted as a list of tuples built from the “many” side of the relationship, with the first half of the tuple being the text to display, and the second half being the actual row, but only those items that are actually currently related, can be an empty list, or can be set to None
, if empty. Always returns current selected items or None
.
placeholder: changed to a new default of ‘Select to add…’, I really don’t think going without a placeholder makes sense is this scenario, but I haven’t tested it with a blank one.
display_column: this must be set to the name of the data table column used in the first half of the tuple for items and selected_items.
edit_mode: boolean that enables use of the same custom component for both displaying the selected items and editing the relationship, can be toggled live to switch modes, selected_items works regardless.
show_none: boolean that, if true, will show a token with “None” when there are no existing relationships on the “many” side.
Notes
- This probably won’t scale well to a huge number of rows available on the “many” side, if I get into such a usage case, and work through both whatever performance issues arise, as well as displaying a search option at some configurable number of records, I will come back and share my updated components.
Enjoy!
https://anvil.works/build#clone:HSPPKNFRC3IZQPDX=KAH4ZOBRPFTBBRW7UHZ5GQS3
Updates
- 5/12/2020
- Changed selected_items to return
None
when there are no items selected (was returning an empty array.
- Fix issue with setting selected_items to [] after use
- 5/13/2020
- Support setting selected_items to
None
Plans
- Add support for a
change
event (has to not be triggered when initial selected_items are removed from the drop down and added to the item box)
- Add support for an additional role for tokens to enable rounded corners for a more tag-like appearance.
- Change items and selected_items to accept and return actual data tables rows and internally use the display_column property to build the options in the dropdown.
- Support formatted display like the data bindings support in anvil so the dropdown can show something like
"{}: {}".format(column1, column2)"