I am making simple ToDo app and I am adding new task pressing enter or pushing “plus” button. It makes new row with textbox, checkbutton and other fields but with no data (like in Google Tasks in Gmail).
I would then like to get the new textbox focused after refresh, is it possible ?
def btn_add_task(self, **event_args):
"""This method is called when the button is clicked"""
anvil.server.call('add_task')
self.refresh()
I can not figure out how to address that new row to make it focused so if you for example press enter, you could be writing another task right away.
You can call the TextBox’s focus() method to set the focus on it after you create it. You need to do this in the currently-open Form, so you need to get the last element of the Repeating Panel like this:
The most robust way to run the refresh method (or any other method) on the top-level Form is to create an event handler on that Form and raise the event from the template.
class ItemTemplate1(ItemTemplate1Template):
#...
def button_1_click(self, **event_args):
get_open_form().raise_event('x-refresh')
That way, if the ItemTemplate is used by a different Form that doesn’t have a refresh method, the x-refresh event will be raised and ignored rather than causing an exception (it’s the Publish-Subscribe pattern which is meant to achieve loose coupling between components).
I’ve modified my example app to have a ‘refresh’ button on each row that runs the refresh() method of the main Form.
If you’re using get_open_form().raise_event('x-refresh'), the x-refresh event will get raised on the open Form. The open Form needs to have an x-refresh event registered in order for something to happen. Have you got an x-refresh event set on your Todo Form?
yes I have, maybe I am doing something wrong in other part.
I added another form to your sample app and as soon as I set it up as the start form, it doesn’t work either. Only “plus” works because it is on the parent.
Your open form is the default form (Form2) which has no event handler.
You are adding the other form as a component.
So what you are doing is opening the default form (Form2).
The button adds Form1 as a subform to Form2.
Form1 has the event handler.
ItemTemplate1 does get_open_form().raise_event('x-refresh') which attempts to raise an event on the open form (Form2) which has no handler.
If you need to reference a subform on the open form you may need to store it in a property. Eg in Form2 :
def button_1_click(self, **event_args):
"""This method is called when the link is clicked"""
f = Form1()
self.subform = f
self.content_panel.clear()
self.content_panel.add_component(f)
I don’t really need to add it as component, it was my error, I just changed it to:
f = Form1()
open_form(f)
and it works.
thank you
P.S. I have also tried the version with form as component and subform refresh, and it also works, now I understand perfeclty, I have learned a lot with this example, thanks again.