Modifying or changing the default ItemTemplate

What I am trying to do

I have an app that collects information from members of my fraternity group. The app has 2 forms:

Form1: collects personal data of each member
Form2: displays personal data extracted from a table in the server

After searching the table (say, keyword: gender = ‘female’), the data display in Form2. However, the data in the ItemTemplate mirrors that in Form1, i.e, all data items are in the ItemTemplate, but only the valid ones are filled (for textbox) or checked (checkbox). The gender data, however, are segregated in separate column panels as in:‘column_panel_female’; column_panel_male’.

What I want to do is display a cleaner Form2 through either of 2 methods:

  1. Display an ItemTemplate in Form2 based on gender condition, or;

  2. Use the default ItemTemplate but delete the column_panel (containers of male data) that is not needed since they are blank.

What I have tried but not working:

Method A:

Based on a test condition (gender), I tried to change the default ItemTemplate to one that I created for each gender as in:

if gender == "female":
	self.repeating_panel_1.item_template = FemaleTemplate

Result:

It seems like the ItemTemplate created when instantiating a repeating panel cannot be changed at runtime. So, the above test failed.

It’s probably a dumb thing to do but I also tried to drop 2 repeating panels in Form 2, one for each gender, but it did not work either, again data display defaults to the ItemTemplate.

Method B:

I tried to apply the method that I found in this forum https://anvil.works/forum/t/manually-creating-and-adding-components/1299/5;solution by @David-Wylie as below:

myform = MyContentForm()
self.column_panel_contentpage.clear()

Replicating the above codes, I tried to delete the column_panel for male in the default (ItemTemplate1) since it componentis held there and can be clicked although without an event handler.

myform = ItemTemplate1()
self.column_panel_male.clear()

Result: I get error as: name ‘Itemtemplate1’ is not defined.

FWIW, I also tried putting the codes directly in Form2 but I get this error instead: AttributeError: ‘Form2’ object has no attribute ‘column_panel_male’ (more expected than not).

If Method 2 would would work, I prefer it over having 2 different ItemTemplates since I’d have less forms to create. All that I need to do is delete the unwanted column panels.

I have been experimenting hard to resolve it but just about out of wits to try.

How could I get around this problem?

Sorry for this long post.
Thank you very much.

Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 

Clone link:
share a copy of your app

that might be a bug

if you do self.repeating_panel_1.items = self.repeating_panel_1.items
just after changing the template it should work

1 Like

Thanks. I will check it out.

Is this FemaleTemplate a class? Or an instance of a class? I think you need an instance, in this case.

The template is how to create instances of the template, so should be a component class, a function that returns a component or a string like the one you would pass to open_form

1 Like

It is a class just like the ItemTemplate. I will take a look at this angle, per you suggestion if I can’t make it work based from the suggestion @stucork. If the column_panel deletion can be make to work, I’d still prefer it, however.

Thank you.

Actually, the real codes are a bit more elaborate than I presented but the code below worked as per @stucork:

if gender == "female":
	self.repeating_panel_1.item_template = FemaleTemplate

Howeverm, the current template then becomes the default. I could switch to an alternative ItemTemplate only by explicitly testing for the opposite condition as:

elif gender =="male":
        self.repeating_panel_1.item_template = MaleTemplate
        break

I also finally used ‘break’ as I only need one occurence of the key to activate the proper template instead of going thru the entire loop.

I am sure my steos could be shortened further by searching the item among keys of dicts instead of extracting the keys and creating a list of keys… oh well, time to learn more python.

Thanks a lot for everyone’s inputs.

1 Like