Creating a form using an alert, with radio buttons

What I’m trying to do:
I’m a complete beginner to Anvil so excuse my ignorance if this is something very basic!

I have a stage in my app in which I’m aiming to have this flow:

  1. User clicks a button
  2. External API is queried to get a list of values
  3. An alert is initiated, with displaying a radio group with the list of values
  4. User chooses an item and clicks ‘Save’
  5. The chosen value is added to a data table

What I’ve tried and what’s not working:

Steps 1 and 2 are working fine, but it’s the rest I’m struggling with. I’ve completed the basic 3 tutorials, so I chose an approach using alerts (similar to the ArticleEdit alert in the news app tutorial), which contains a repeater panel. Each repeater panel part contains a single radio button, and I give this a name and a value from the item - this works correctly and when I open the alert I can see a radio list with all the items and values. The place where I’m stuck is saving this capturing the chosen value - so far I’ve tried assigning the selected value to a variable named selected in the parent, so that I can use it when “Save” is clicked, however the value does not seem to make it to the parent:

Used in the code of the repeater panel template:


  def account_radio_clicked(self, **event_args):
    """This method is called when this radio button is selected"""
    if self.account_radio.selected:
      self.parent.parent.selected = self.item

Used in the form that the alert is generated from.

  def choose_account_button_click(self, **event_args):
    accessible_accounts = anvil.server.call('get_accessible_accounts')
    selected_account = {}
    link_clicked = alert(
      content=GASelectAccount(accounts=accessible_accounts, selected=selected_account),
      title="Choose Project",
      large=True,
      buttons=[("Link", True), ("Cancel", False)],
    )
    
    if link_clicked:
      print(selected_account)

I’m unsure if this is the correct to approach to go down, to create a radio input on a form, and if so where I’m going wrong in the code.

Any help or guidance on this would be much appreciated!

Your general approach seems okay. It’d take some debugging to work out where the disconnect is.

I created a very simple test app to try it out, and it seems to be working fine, so maybe take a look at it and see where your approach differs: https://anvil.works/build#clone:5ZHGEJTYJ2ZO26BA=XWGSZZ72ONLS66QR67ICLEG2

1 Like

Thanks a lot, that was really helpful and I was able to solve the issue by following your code!

This was where the difference was:

Doesn’t work

selected = {}
...
GASelectAccount(accounts=accessible_accounts, selected=selected)
...
print(selected)

Does work

...
form = GASelectAccount(accounts=accessible_accounts)
...
print(form.selected)
1 Like

Hi,
This technique is very elegant. I am just experiencing one issue:
The dialogbox that pops up is of such a height that I cannot see the content at the top and only the OK button at the bottom.
I use a XY-Layout container for my four fields.
Any tips?

You are unlikely to get an answer to a 3 year old post with a reply marked as solution.

Please ask a new question: How to ask a good question

2 Likes