Passing variables between components

#1

Hello,

I am pretty new with Universal Dashboards and do understand some of the basics.
My question is about Event-handling and variables.
After reading documentation, experimenting, looking at other code, I have not found the answer.

The dashboard contains a checkbox and a button. When the checkbox has been selected, var $Myvar gets a new value which must be used after clicking the button.
However the button still shows the initial value of the var.

Any help is highly appreciated. Thanks in advance for your help.

Get-UDDashboard | Stop-UDDashboard

$Myvar = "Old message"

$MyDashboard =  New-UDDashboard -Title "Select me" -Content {
                New-UDElement -Id "CheckboxState" -Tag "span" 

                New-UDCheckbox -Id CheckBox -Label "Check me" -OnChange {
                    $Element = Get-UDElement -Id CheckBox
                    Set-UDElement -Id "CheckboxState" -Content $Element.Attributes["checked"]
                    $Myvar = "New message"
                }

                New-UDHeading -Size 5 -Id "ShowOnClick" -Text ""
                New-UDButton -Text "Button" -OnClick {
                    #Set-UDElement -Id "ShowOnClick" -Content { "Button message" }
                    Show-UDToast -Message $Myvar
                }
                        
            
}

Start-UDDashboard -Dashboard $MyDashboard -Port 10000
0 Likes

#2

Hi @Pgrevink,

You will want to use the $Session: scope for your varaibles. This will be updated per user using your dashboard. A session variable isn’t available outside an endpoint.

Get-UDDashboard | Stop-UDDashboard

$Myvar = "Old message"

$MyDashboard =  New-UDDashboard -Title "Select me" -Content {
                New-UDElement -Id "CheckboxState" -Tag "span" 

                New-UDCheckbox -Id CheckBox -Label "Check me" -OnChange {
                    $Element = Get-UDElement -Id CheckBox
                    Set-UDElement -Id "CheckboxState" -Content $Element.Attributes["checked"]
                    $Session:Myvar = "New message"
                }

                New-UDHeading -Size 5 -Id "ShowOnClick" -Text ""
                New-UDButton -Text "Button" -OnClick {
                    #Set-UDElement -Id "ShowOnClick" -Content { "Button message" }
                    Show-UDToast -Message $Session:Myvar
                }
                        
            
}

Start-UDDashboard -Dashboard $MyDashboard -Port 10000
0 Likes

#3

Hello @adam,

Thank you very much for your quick reply. I am getting the idea how this works. Will continue my experiments.

Best regards,

Paul

0 Likes