Data binding with radio buttons

I have 4 phone numbers (home, work, cell and other) and would like to use a radio button to select which is primary. My data table has a “PrimaryPhone” column with data already populated with Home, Work, Cell Other and Null. So, I have placed 4 radio buttons in a repeater and given them the same group name. I’ve used print statements to ensure that my code is pulling the appropriate values but it seems the UI will not change to selected.

I’ve tried various changes with the properties. I’ve also tried to add a delay timer thinking the Ui might need time to catch up.

class ItemTemplate3(ItemTemplate3Template):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    
  # Set the radio_home based on the PrimaryPhone value
    self.set_radio_home_selection()
    print(f"Current item's PrimaryPhone value: {self.item['PrimaryPhone']}")

  def set_radio_home_selection(self):
    """Selects radio_home if PrimaryPhone is 'Home'."""
    if self.item['PrimaryPhone'] == 'Home':
      self.radio_home.selected = True
      print("Setting radio_home to selected.")
    else:
      self.radio_home.selected = False
      print("Setting radio_home to not selected.")

Clone link:
share a copy of your app

Welcome to the Forum!

Unfortunately, we can’t see what Data Bindings you may have used, so it’s hard for us to read what’s going on in your actual App.

Is this something you could share via a Clone link?

1 Like

I’d like to share a clone link but I have concerns over my data being public. Maybe this will help:

image

Sorry to be cryptic but i don’t own the data

Hi @jzgaskin,

I can’t spot anything obvious in the code snippet.
If you’re not able to provide a clone link of the original app, then it’s often best to create a minimal working example that demonstrates the issue.
In fact, it’s almost always better to do this anyway since the original app will have a lot of code that is unrelated to the issue you’re trying to solve.

1 Like

Thanks @stucork , here is a minimal working example.

So there seemed to be 2 issues, either or both of which might be actual bugs on anvil’s side:

  1. When each row in the repeating panel’s init function runs, there seems to be no group_name assigned to the radio button groups. I say this because I was able to get only the first row’s radio button selected by running the primary select function in the show event, but none of the other rows.
  2. It seems that the reason that during the show event that only the first row’s radio button was selected was because all the rows in the repeating panel’s radio buttons had the same radio button group name.

I worked through both issues by adding a unique group_name key to each row’s item property and assigning that as each radio buttons in that row’s group_name like so:

self.radio_home.group_name = self.radio_work.group_name = self.radio_other.group_name = self.radio_cell.group_name = self.item["group_name"]

Here’s the clone link:

2 Likes

Not a bug, but a bit of a hard edge

radio buttons are grouped based on their group name
If all your radio buttons on the page have the same name, you have one group

so creating a unique group name based on the item is the right approach here

3 Likes

@duncan_richards12 & @stucork ,

Thanks for your reply. I recognized this while running the app. One of the radio buttons would be selected and it was correct for the given value.

Since all my records have a customer number, I use this to make unique group names.

 # Dynamically set the RadioButton group name to ensure uniqueness per Repeater item
    unique_id = str(self.item['CustomerNumber'])  
    self.radio_home.group_name = f"phoneGroup_{unique_id}"
    self.radio_work.group_name = f"phoneGroup_{unique_id}"
    self.radio_cell.group_name = f"phoneGroup_{unique_id}"
    self.radio_other.group_name = f"phoneGroup_{unique_id}"

The result:

Thanks for helping me think through this.

3 Likes