I have a table with a text box in a column. When a user clicks a row selection checkbox and -OnRowSelection triggers, I’d like to capture the value the user entered into the text box. From there I’ll take other actions like verifying data is there and not more than x number of characters. The problem I’m running into is the $EventData inside the -OnRowSelection block reports back “System.Collections.Hashtable”.
I’m using this to try and pull the data: $EventData.Justification; where Justifciation is the property name. When I use other properties, like VMName or PrimaryOwner, those return proper values.
How can I return the user’s entered data from the text box, when -OnRowSelection is triggered, rather than “System.Collections.Hashtable”?
I have to set an ID on the text boxes, and they need to be unique so I named them after the row number they will be in the table; like table-0, table-1, etc. After that I could use (Get-UDElement textbox-#).Value. Although this worked, I came to realize that I don’t want capture this and validate the values in the -OnRowSelection of the table; instead I want to move this to the button. In the button, when you use (Get-UDElement -Id ‘table id’).selectedRows you get back the rows and you can loop through them, but they do not include the row number. So I went back to the first array of objects I create and added a Row property. Now each object that makes up the table has a Row property and text box Id, with the same number.
After that, the button -OnClick block is able to loop through each selected row, create a new object, and add to an array. During the loop we check if the text box value is within our parameters, and if not we add that row to a separate array.
Once the loop is finished, if we have any text box parameter failures we give a toast and set some text box attributes to call out the ones with problems. If there are not any selected VMs with text box parameter failures, we continue with our VM renewal actions.
$ownerDispName = "User 01"
$VMs = @()
$VMs += [ordered]@{Name = "Server 01";Owner = $ownerDispName; LastITValidationDate = "12/15/2021"; Justification = "Used for app X, Y, Z"}
$VMs += [ordered]@{Name = "Server 02";Owner = $ownerDispName; LastITValidationDate = ""; Justification = "Test"}
$VMs += [ordered]@{Name = "Server 03";Owner = $ownerDispName; LastITValidationDate = "01/23/2020"; Justification = ""}
$filteredVMs = @()
$count = 0
foreach ($VM in $VMs)
{
$filteredVMs += [PSCustomObject][Ordered]@{
Row = [string]$count
VMName = $VM.Name
PrimaryOwner = $VM.Owner
LastITValidationDate = $VM.LastITValidationDate
Justification = New-UDTextbox -Id "textbox-$([string]$count)" -Type Text -Value "$($VM.Justification)" -Placeholder "Enter between 10 and 200 characters!" -FullWidth
}# $filteredVMs
$count ++
}# foreach ($VM in $VMs)
$columns_VMsOwned = @(
New-UDTableColumn -Property Row -Title "Row" -Hidden
New-UDTableColumn -Property VMName -Title 'VM Name'
New-UDTableColumn -Property LastITValidationDate -Title 'Last Audit Date'
New-UDTableColumn -Property 'Justification' -Render {$eventData.Justification}
)# $columns_VMsOwned
$table_VMsOwned = New-UDTable -Id 'table_VMsOwned' -Data $filteredVMs -Columns $columns_VMsOwned -Title "VMs Owned by: $ownerDispName" -ShowSelection -Dense
$button_Renew = New-UDButton -Text "Renew VMs" -Color Primary -OnClick {
# Clear a previous toast, if it exists
Hide-UDToast -Id "toast_Justification"
Hide-UDToast -Id "toast_NoSelections"
# Pull out table to a variable and create a copule of empty arrays
$values = Get-UDElement -Id "table_VMsOwned"
$selectedRows = @()
$failedJustified = @()
# Clear any background color that may be present in the text boxes from a previous button click that may have set one or more to red.
foreach ($v in $values.Data.Row)
{Set-UDElement -Id "textbox-$v" -Attributes @{Placeholder = "Enter between 10 and 200 characters!"; style = @{backgroundColor = 'white'}}}
# If there are not any selected Rows returned, throw toast
If (!$values.selectedRows)
{Show-UDToast -Id "toast_NoSelections" -Message "Please select one or move VMs and try again!" -Duration 10000 -BackgroundColor “Red” -MessageColor “White” -Balloon -TransitionIn fadeInDown}
# Else, continue onwards
else
{
# Loop through each row, and create new objects for each.
foreach ($rowValue in $values.selectedRows)
{
Remove-Variable row -Force -ErrorAction SilentlyContinue
$row = [PSCustomObject][Ordered]@{
RowID = $rowValue.Row
VMName = $rowValue.VMName
LastITValidationDate = $rowValue.LastITValidationDate
Owner = $rowValue.PrimaryOwner
Justification = (Get-UDElement -Id "textbox-$($rowValue.Row)").Value
}# $row
# Show-UDToast -Message $row -Persistent
# Show-UDToast -Message "$($row.Justification.Length | Out-String)" -Persistent
# If the Justification value does not fall within parameters, add it to the $failedJustified array for later
if ($row.Justification.Length -lt 10 -or $row.Justification.Length -gt 200)
{$failedJustified += $row}
# Add the row object to the $selectedRows array for later.
$selectedRows += $row
}# foreach ($v in $values)
# If we have any Justifications outside the parameters stop here, throw a UDToast, then call out the text boxes with issues.
if ($failedJustified)
{
Show-UDToast -Id "toast_Justification" -Message "One or more selected VMs need their Justifciation value updated to between 10 and 200 characters!" -Duration 10000 -BackgroundColor “Red” -MessageColor “White” -Balloon -TransitionIn fadeInDown
foreach ($failed in $failedJustified)
{
# Get the current text box value, set it so it calls out a problem with this textbox, while keeping the same value. This is done so we have the Placeholder shows up as a footer.
$textboxValue = (Get-UDElement -Id "textbox-$($failed.RowID)").Value
Set-UDElement -Id "textbox-$($failed.RowID)" -Attributes @{Placeholder = "Enter between 10 and 200 characters!"; style = @{backgroundColor = '#F89728'}; Value = $textboxValue}
}# foreach ($failed in $failedJustified)
}# if ($failedJustified)
else
{
Show-UDModal -Content {
New-UDTypography -Variant h3 -Text "Continue with VM Renewal Actions!" -Style @{color='green'}
}
}# else
}# else
}# button_Renew
$table_VMsOwned
$button_Renew