Edit: This post actually helped me improve the deletion by having it be buttons and include a prompt to ensure users want to delete the object). Still trying to figure out how to add a row as well as edit an existing row within the page.
I am trying to build a dashboard to allow staff to view/modify the contents of a CSV file. In this case, it’s printer files that define the printers available to a virtual desktop.
The CSV is one or more lines containing the hostname, printer IP, printer name, and printer driver to use.
A sanitized example:
Hostname,PrinterIP,PrinterName,PrinterDriver
PC001,10.0.0.63,Printer01,HP LaserJet 4
PC001,10.0.0.83,Printer02,Xerox VersaLink C400 PCL6
PC001,10.0.0.58,Printer03,Xerox Phaser 6600DN PCL6
Of note, the first printer in the file becomes the default in Windows for the user. So I have the following main goals:
- Add a new row
- Allow selecting a default (or re-ordering the list dynamically to place a new item in the first row)
- Change a value within a row (e.g. change the “Printer Name” or “Printer IP” on a row)
- Delete an existing row
I’ve had some success with the updating the default printer or deleting a row using the below code, however, users have to reclick on the “Submit” button to modify/update the table. More importantly, I’m stumped how to set the table to allow for adding a row or to modify an existing value in one of the existing rows. I am not particularly proud of the below code at all, so if there is a better approach, I am completely open.
(Code modified with updated ‘Delete’ handling)
New-UDPage -Name 'Printer Info' -Content {
New-UDTypography -Text 'Enter endpoint name'
New-UDForm -Content {
New-UDTextbox -Id 'Endpoint' -Placeholder 'Endpoint Name'
} -OnSubmit {
$CsvRootPath = 'D:\Files\PrinterFiles'
Try{
$CsvFile = Join-Path $CsvRootPath "$($EventData.Endpoint).csv"
[Array]$RawData = Import-Csv $CsvFile -ErrorAction Stop
$DefaultPrinter = ($RawData | Select-Object -First 1).PrinterName
$FormattedData = $RawData.ForEach({
$Obj = [PSCustomObject]@{
DefaultPrinter = $null
Hostname = $_.Hostname
PrinterName = $_.PrinterName
PrinterIP = $_.PrinterIP
PrinterDriver = $_.PrinterDriver
}
If($_.PrinterName -eq $DefaultPrinter){
$Obj.DefaultPrinter = 'True'
}
$Obj #Returns object to $FormattedData parent array
})
$Columns = @(
New-UDTableColumn -Property Hostname -Title 'Endpoint Name'
New-UDTableColumn -Property PrinterName -Title 'Printer Name'
New-UDTableColumn -Property PrinterIP -Title 'Printer IP'
New-UDTableColumn -Property PrinterDriver -Title 'Printer Driver'
New-UDTableColumn -Property DefaultPrinter -Title 'Default Printer'
New-UDTableColumn -Property Delete -Title Delete -Render {
New-UDButton -Id 'btnDeletePrinter' -Text 'Delete' -OnClick {
Show-UDModal -Content {
$PrinterName = $eventdata.PrinterName
New-UDForm -Content {
New-UDCheckBox -Label "Check box and press submit to confirm deletion of [$PrinterName], click outside of this box to cancel." -Id 'Confirmation'
} -OnSubmit {
If ((Get-UDElement -Id 'Confirmation').checked -eq $true) {
Show-UDToast -Message "Deleting $PrinterName"
$Output = $RawData.Where({$_.PrinterName -notin $PrinterName})
$Output | Export-Csv $CsvFile -NoTypeInformation -Force
Start-sleep -Seconds 2
Hide-UDModal
}
Else {
Show-UDToast -Title 'Checkbox' -Message "NOT deleting $topic."
Start-sleep -Seconds 2
Hide-UDModal
}
}
} -FullWidth -MaxWidth 'md'
}
}
)
Set-UDElement -Id 'ElementJobOutput' -Content {
New-UDCard -Id 'CardJobOutput' -Title "Printer Info [$($EventData.Endpoint.ToUpper())]" -Content {
New-UDTable -Id 'TablePrinterInfo' -Data $FormattedData -Columns $Columns -ShowSelection
}
Set-UDElement -Id 'SubmitButton' -Content {
New-UDButton -Id "btnUpdateDefaultPrinter" -Text "Set Default Printer" -OnClick {
$Rows = Get-UDElement -Id 'TablePrinterInfo'
If($Rows.SelectedRows.Count -eq 0){
Show-UDToast -Message "Must select one printer" -CloseOnClick -Position center -Duration 10000
} Elseif($Rows.SelectedRows.Count -gt 1){
Show-UDToast -Message "Cannot set more than one default printer" -CloseOnClick -Position center -Duration 10000
} Elseif($Rows.SelectedRows.Count -eq 1) {
Show-UDToast -Message "Updating [$($EventData.Endpoint.ToUpper())] default printer to [$($Rows.SelectedRows.PrinterName)]" -Duration 2500
$NewDefaultPrinter = $Rows.SelectedRows.PrinterName
$NewDefaultArray = $RawData.ForEach({
$Obj = [PSCustomObject]@{
DefaultPrinter = $null
Hostname = $_.Hostname
PrinterName = $_.PrinterName
PrinterIP = $_.PrinterIP
PrinterDriver = $_.PrinterDriver
}
If($_.PrinterName -eq $NewDefaultPrinter){
$Obj.DefaultPrinter = 'True'
}
$Obj
})
$Sorted = $NewDefaultArray | Group-Object DefaultPrinter | Sort-Object Name -Descending
# Create sorted Array for export
$ExportArray = Foreach ($Item in $Sorted){
$Item.Group
}
# Export to CSV file
$ExportArray | Export-Csv $CsvFile -NoTypeInformation
}
}
}
}
} Catch {
Set-UDElement -Id 'ElementJobOutput' -Content {
"$($Error[0].exception.message)"
}
}
}
New-UDElement -Id 'ElementJobOutput' -Tag 'div' # New empty element to be updated/displayed after user clicks 'Submit'
New-UDElement -Id 'SubmitButton' -Tag 'div2'
}