Add button to table when using existing array/datarow?

I think I finally got something which works! I don’t like that I have to do all of the button logic in the Columns declaration but it’s working now!

Does anyone know if it will ever be supported to do something like

$mydata = invoke-sqlcommand -getsomedata

$tabledata = $mydata | % { [pscustomobject]@{
    topic = $_.topic
    type = $_.type
    deletebutton = New-udbutton -text 'Delete me!'
}
$Columns = @('topic','type','deletebutton') 
New-UDTable -Data $tabledata -Columns $columns

I think something like that used to work but I wasn’t able to get it working here. The below code does seem to work. Any comments/criticism greatly appreciated as I’m really bad at this :frowning:

$data = Invoke-SqlCmd -ServerInstance $serverinstance -Database $databasename -Query "SELECT DISTINCT topic, type FROM topics"
$tabledata = @()
Foreach ($d in $data) {
    $tableData += @{topic = $d.topic; type = $d.type}
}
$Columns = @(
    New-UDTableColumn -Property type -Title Type
    New-UDTableColumn -Property topic -Title Topic    
    New-UDTableColumn -Property edit -Title Edit -Render {
        New-UDButton -text 'Edit' -OnClick  {
            Show-UDToast -Message "Someday I'll be able to edit"}
    }
    New-UDTableColumn -Property delete -Title Delete -Render {
        New-UDButton -text 'Delete' -OnClick  { 
            Show-UDModal -Content {
                $topic = $eventdata.topic
                $type = $eventdata.type
                New-UDForm -Content {
                    New-UDCheckBox -Label "Check box and press submit to confirm deletion of $topic, click outside of this box to cancel."  -Id 'Confirmation'
                } -OnSubmit {
                    If ((Get-UDElement -Id 'Confirmation').checked -eq $true) {
                        Show-UDToast -Message "Deleting $topic"
                        Invoke-SqlCmd -ServerInstance $serverinstance -Database $databasename -Query "DELETE FROM topics WHERE topic=$topic AND type=$type"
                        Start-sleep -Seconds 3
                        Hide-UDModal
                    }
                    Else {
                        Show-UDToast -Title 'Checkbox' -Message "NOT deleting $topic."
                        Start-sleep -Seconds 3
                        Hide-UDModal
                    }

                }
            }-FullWidth -MaxWidth 'md'
        }
    }
    )
New-UDTable -Data $tableData -Columns $Columns -sort -export -ShowFilter
}
                     
1 Like