Is there a "form loaded" event or function to add to Form component

What I’m trying to do:
I’m trying to scroll a repeating panel to bottom after component is rendered in DOM

What I’ve tried and what’s not working:
I’ve tried to run a function in init after init_components is called,
that set an elements scrollTop to it’s scrollHeight via e.g

def scroll_to_bottom(self):    
    el = anvil.js.get_dom_node(self.repeating_messages)
    print('el scrollTop was', el.scrollTop)
    el.scrollTop = el.scrollHeight
    print('el scrollTop is now', el.scrollTop)

This prints ‘el scrolltop is now 0’ and self.repeating_messages component is not scrolled to bottom

If I instead, run this function on the push of a button, the components gets scroll to the bottom as expected.

This must be because my component is not ready in the DOM after init_components has run.
Is there a component life cycle function i can put my scroll_to_bottom method in, to ensure it runs after component is ready in the DOM?

I’ve seen mentions of a function called form_show that should be what I’m looking for, but seems deprecated as I can’t find it in the docs:

Thanks in advance!

Form show is most certainly not deprecated, so that’s what you’d want to use!

1 Like

Using the underscore when searching the documentation doesn’t work, which is a crippling limitation. Googling site:anvil.works/docs form_show usually gives better results than searching from the documentation itself. See here: Anvil Documentation Suggestions [ON-GOING] - #135 by stefano.menci

2 Likes

Do note that the form’s show event fires any time the visibility changes. So if you have one-time initialization in there, you’ll want to protect it from executing more than once, with something similar to:

if self.form_show_run:
    return

self.form_show_run = True

# do one-time init stuff here

And initialize self.form_show_run to False in your init.

3 Likes

Ahh, ok, got it.
I thought that the form_show just firing when you add it to the Form class.
But I see now in the editor that there’s a show event on every component that can attached to a class method.
Thanks for the help! And thx for the search tip @stefano.menci . definitely useful.

1 Like