How to populate RichText box in parent form from repeating panel button

What I’m trying to do:

I have a blank richtext box in my main/parent form called “results_text”. Also on the main form I have a datagrid with repeating panels that gets populated form an outside database. In the repeating panels I have a button.

What I would like to happen is when the button is pressed on a given row, the script “pushes” some of the row’s content to the “results_text” box.

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

I have tried importing the Form into the RowTemplate code (From … Import Form), but when I try to access/find the richtext object, it does not show up.

Code Sample:

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

Clone link:
share a copy of your app

import gets you the class of the Form; that is, a factory for making instances of that class: for making new Forms of the same type.

But you don’t need a new instance. You’ve already got the instance you want: the “parent” form you mentioned. Anvil created it on demand, for displaying it. (Actually, it’s probably several “parents” removed. Call it an “ancestor”.) It’s live, on-screen, at the time.

The question then is, how to get the RowTemplate instances talking to that specific ancestor.

What I usually do, in this case, is create a custom event. This is a way to have the RowTemplate instance “call” upon a function in the ancestor Form, without having to actually go and find that Form. The function can receive whatever data the RowTemplate wishes to send.

Example (row template pushing datum line_item back to parent):

    def btn_contract_edit_click(self, **event_args):
        """This method is called when the button is clicked"""
        self.parent.raise_event('x-edit-contract', line_item=self.item)

Example (parent form preparations to receive and act on that datum):

class Frame_ContractList(Frame_ContractListTemplate):
    def __init__(self, **properties):
        # Set Form properties and Data Bindings.
        self.init_components(**properties)

        # Any code you write here will run when the form opens.
        self.repeating_panel_1.set_event_handler(
            'x-edit-contract', self.on_edit_line_item)
...

    def on_edit_line_item(self, line_item, **event_args):
        cec = contract_editor_context.ContractEditorContext(line_item)
        cec.invoke_editor()

Custom event names should begin with “x-”. Otherwise, name your event and your functions as you see fit, to help explain (to your later self) what’s going on.

1 Like