UDElement not forwarded

Product: PowerShell Universal
Version: 4.0.5

Hi,

I have made a table based on values from AD, that our HD can use to change the office location for a user.
The UI is working as expected, but im having trouble relaying the value.

I found this example on how to retrieve values from a textbox, which is working as expected.
But when i add it to my code the Show-UDToast is just reporting that the message is empty = the Get-UDElement did not pull any of the data.
Textbox - PowerShell Universal

New-UDPage -Url "/app/hd/page/changeoffice"  -Name "Change office" -Content {
    Import-Module -Name ActiveDirectory -ErrorAction stop -WarningAction stop
    New-UDGrid -Container -Spacing 3 -Content {
        New-UDGrid -Item -ExtraSmallSize 6 -Content {
            $Cache:ADUsers = Get-ADUser -Filter * -Properties Office 
            $Columns = @(
                New-UDTableColumn -Property SamAccountName -Hidden
                New-UDTableColumn -Property Name -Title "Name" -DefaultSortColumn -ShowFilter -ShowSort
                New-UDTableColumn -Property Office -Title "Current Office" -ShowFilter -ShowSort
                New-UDTableColumn -Property NewOfficeNumber -Title "New Office Number" -Render {
                    New-UDTextbox -Id 'txtExample' -Placeholder "Enter new office location"
                }
                New-UDTableColumn -Property ActivateChangeOfficeNumber -Title "Change Office" -Render {
                    New-UDButton -Text "Change Office" -OnClick {
                        $Value = (Get-UDElement -Id 'txtExample').value 
                        Show-UDToast -Message $Value
                    }
                }
            )
            New-UDTable -Data $Cache:ADUsers -Columns $Columns -Title "Users" -ShowSort -ShowFilter -PageSize 15 -ShowPagination -Dense
        }
    }
} -Title "Change office location"

My question is:
I want to use the SamAccountName value combined with the txtExample value, so that i can use it for setting the updated AD user value - how do i accomplish that?

I’m getting the same result as you when using a button within a table.
Used to behave as expected (and how you describe) in earlier V3 versions.

Not sure what’s going wrong here…

We have an issue open for that here: Get-UDElement doesn't work on components rendered in tables · Issue #2485 · ironmansoftware/issues · GitHub

Awesome!
Just discovered that if i try to change it on a user, while the entire list / table is shown, then i get the exception message.
But if i search for a user and therefor narrow it down, so that only one entry is present, then the Show-UDToast is working as expected.

Hope thats usefull to you :slight_smile:

Perhaps related;

When creating a table with a column that uses -Render, within that rendered content I have:

New-UDTableColumn -Property 'Actions' -Title 'Actions' -Id 'Actions'-Align center -Render {
	if ($null -ne $EventData.autotask.id) {
		New-UDTooltip -Id 'tooltip_serviceDetails' -Content {
			New-UDButton -Icon (New-UDIcon -Icon 'info') -Variant text -OnClick {
				Show-ServiceMappingModal -subscription $EventData
			}
		} -TooltipContent {
			'Show Service Details'
		}
	}
	else {
		New-UDTooltip -Id 'tooltip_serviceDetails' -Content {
			New-UDButton -Icon (New-UDIcon -Icon 'edit') -Variant text -OnClick {
				Show-ServiceMappingModal -subscription $EventData
			}
		} -TooltipContent {
			'Edit Service Details'
		}
	}
	New-UDTooltip -Id 'tooltip_subscriptionId' -Content {
		New-UDButton -Icon (New-UDIcon -Icon 'copy') -Variant text -OnClick {
			Set-UDClipboard -Data $EventData.subscriptionId
			Show-UDToast -Message 'Subscription Id has been copied to the clipboard!' -BackgroundColor 'LightGreen' -Position bottomRight
		}
	} -TooltipContent {
		$EventData.SubscriptionId
	}
}

The first 2 New-UDTooltip with the static TooltipContent show no issues and the New-UDButtons behave as expected, forwarding the $EventData variable as designed, for the specific table row.

The 3rd New-UDTooltip, however, uses the $EventData.SubscriptionId variable for it’s TooltipContent, but does not seem to consider the Table Row for which it should do so.
All rows shows the $EventData.subscriptionId of the first row in the table.

After playing around with this more, this seems to be working as expected. The problem is that the element IDs need to be unique. I made them unique by including the SamAccountName with the ID.

New-UDPage -Url "/changeoffice"  -Name "Change office" -Content {
    New-UDGrid -Container -Spacing 3 -Content {
        New-UDGrid -Item -ExtraSmallSize 6 -Content {
            $Cache:ADUsers = @(
                @{ SamAccountName = "Adam"; Name = "Adam"; Office = "Office1"  }
                @{ SamAccountName = "Ted"; Name = "Ted"; Office = "Office1"  }
                @{ SamAccountName = "Bill"; Name = "Bill"; Office = "Office1"  }
            )
            $Columns = @(
                New-UDTableColumn -Property SamAccountName -Hidden
                New-UDTableColumn -Property Name -Title "Name" -DefaultSortColumn -ShowFilter -ShowSort
                New-UDTableColumn -Property Office -Title "Current Office" -ShowFilter -ShowSort
                New-UDTableColumn -Property NewOfficeNumber -Title "New Office Number" -Render {
                    New-UDTextbox -Id ('txtExample' + $EventData.SamAccountName) -Placeholder "Enter new office location"
                }
                New-UDTableColumn -Property ActivateChangeOfficeNumber -Title "Change Office" -Render {
                    New-UDButton -Text "Change Office" -OnClick {
                        $Value = (Get-UDElement -Id ('txtExample' + $EventData.SamAccountName)).value 
                        Show-UDToast -Message $Value
                    }
                }
            )
            New-UDTable -Data $Cache:ADUsers -Columns $Columns -Title "Users" -ShowSort -ShowFilter -PageSize 15 -ShowPagination -Dense
        }
    }
} -Title "Change office location"

For the tooltip, it was the same trick.

                    New-UDTooltip -Id ('tooltip_subscriptionId' + $EventData.SamAccountName) -Content {
                        New-UDButton -Icon (New-UDIcon -Icon 'copy') -Variant text -OnClick {
                            Set-UDClipboard -Data $EventData.subscriptionId
                            Show-UDToast -Message 'Subscription Id has been copied to the clipboard!' -BackgroundColor 'LightGreen' -Position bottomRight
                        }
                    } -TooltipContent {
                        $EventData.SamAccountName
                    }

The reason this is happening is because we are creating multiple buttons with the same ID when the ID isn’t unique and the last button (or tooltip) to be created is the one that ends up being registered with that ID.

1 Like

That actually makes perfect sense, and when i use the old code to go to the last page in the table, the button actually works, because its the last one to get generated.

Considering a github issue was created due to this, and we were several people having the same issue / misunderstanding of this, it would be a good idea to include it in the documentation - perhaps even in the psu-demo template that we can download. I often use the examples in there, to solve issues and get inspiration - so its a win-win for everyone :slight_smile:

“Dôh!”

Is all I can say right now…
Thanks for clarifying!