Im building an ordering system for a local event
there will be 3 people going around ordering drinks with phones
I have 3 pages 1 for selecting drinks one for tabels and one to confirm and show the total price and if it was succes full
the problem im running into is that if you go to the next page i need to give some data to that next page like an order id or just the order itself
how can best do this in anvil
When I want to pass something from one form to another I like to do it like this:
using the Material design, clear the content panel
add the new form to the content panel, passing the variables along. For example:
from my_new_form import my_new_form
self.content_panel.clear()
new_form_instance=my_new_form(some_variable='foo')
self.content_panel.add_component(new_form_instance)
Then, in your new form you can get 'foo' out from the properties in the init method:
some_variable=properties['some_variable']
If you are using the Material design you can also “stick” your variable to the nav bar or header components (or components contained there). For example:
my_home_page_icon_in_nav_bar.tag='foo'
'foo' will then be available to all your nested forms. That is, if you open a new form by clearing and adding to the main content panel, you can access 'foo' since the main for is technically still open.
Lastly, you could save your variable to a data table, but that is probably overkill if you’re looking just to pass temporary things.
There may be other ways, but hopefully this gets you started. Good luck!
@alcampopiano made a good suggestion regarding passing in the variable to the Form you are opening. As well as getting the variable out of the **properties, you can also add your own parameters to __init__, like so:
class Form2(Form2Template):
def __init__(self, your_own_parameter, **properties):
# Set Form properties and Data Bindings.
self.init_components(**properties)
# This is the bit you need to write
some_variable = your_own_parameter
Which you would use by doing this
from my_new_form import my_new_form
self.content_panel.clear()
new_form_instance=my_new_form('foo')
self.content_panel.add_component(new_form_instance)
Global variables
You can also store things in global variables that you can import from any Form. Create a Module named Global, then import that from both of your Forms:
from Global import my_variable
When you modify this variable in one Form, the modified version will be available in another Form (make sure you use a global my_variable statement in any methods that modify it).
def button_1_click(self, **event_args):
"""This method is called when the button is clicked"""
global my_variable
my_variable = self.label_1.text
Here’s an example that uses a global variable to store search filters between two Forms: