Add button to table when using existing array/datarow?

I have a simple table with two columns topic and type which are built on data from a sql query. I want to present the data in a New-UDTable but add a 3rd column for a button. I am struggling with how to either either add the button to each row of the datarow/array object or dynamically add it in with the columns flag. I’ve tried a few things but am really rusty on my PS and didn’t have luck getting things to work.

Here’s the code I currently have:

$datarows = Invoke-SqlCmd -ServerInstance "localhost" -Database "optimizer" -Query "SELECT DISTINCT topic, type FROM topics"
$columns = @(New-UDTableColumn -Property "Edit" -Title "Edit" -Render { 
    New-UDButton -Text "Click to do something!!" 
    }
    New-UDTableColumn -Property "topic" -Title "topic"
    New-UDTableColumn -Property "type" -Title "type"
)
    New-UDTable -Data $datarows -columns $columns -Search -PageSize 10 

If I simply use $datarows in New-UDTable I get the 2 column table fine but I’m uncertain how to get that button to also show up :frowning:

Any help would be greatly appreciated! Love the new features in UD, it’s been awhile since I took a look at it!

Hello @theabraxas long time no see, hope you keeping ok buddy? Well I remember I pulled this off once, I cannot remember the original ironman webpage I got to, to do this. So as I used it in SQL as well, here is a complete (almost) of one of my pages, that shows information in a table, and sums the total that customer spent, then you got an invoice button, which will then show the breakdown of each order in a new modal window…here is the code, hopefully from this you can figure it out for yourself:-

New-UDPage -Name "Goods_Only_21_days" -Title "Goods Only 21 Days Page" -Content {
    New-UDRow -Columns {
   New-UDLayout -Columns 1 {
   New-UDTable -Title "Goods Only Customers Last 21 days" -Headers @("Name","Customer","Gross","Ranking","Invoice") -Style bordered -Endpoint {
    $GoodsOnly21 = @"
    exec dbo.SPROC_GOODS_ONLY 21
"@
    $GoodsO21 = Invoke-Sqlcmd2 -ServerInstance YOUR_SQL_SERVER -Database YOUR_SQL_DB -Query $GoodsOnly21 -Username USER -Password PASSWORD -QueryTimeout 500

    $GoodsO21 | % {
   New-Object -TypeName PSCustomObject -Property @{
   "Customer" = $_.CustomerCode
   "Name" = $_.CustomerName
   "Gross" = $_.Gross
   "Ranking" = $_.Ranking
   "Invoice" = New-UDButton -Text "Invoice" -OnClick  {

             Show-UDModal -Header {New-UDHeading -Size 4 -Text "$($_.CustomerName)" }  -Content {

             New-UDTable -Title "Invoice Information" -Headers @("CustomerCode","TransactionNet","TransactionVat","Quantity","ProductName") -Style bordered -Endpoint {
             $mQ = @"
   WITH GoodsOnly AS (
     SELECT CustomerCode
      FROM [YOUR_SQL_DB]
     WHERE CustomerCode = '$($_.CustomerCode)'
"@
   $QueryModal = Invoke-Sqlcmd2 -ServerInstance YOUR_SQL_SERVER -Database YOUR_SQL_DB -Query $mQ -Username USER -Password PASSWORD -QueryTimeout 50

   $QueryModal | Out-UDTableData -Property @("CustomerCode","TransactionNet","TransactionVat","Quantity","ProductName")
   }
 
               }
             }
           }

   } | Out-UDTableData -Property @("Name","Customer","Gross","Ranking","Invoice")
   } 
   }

   }
   }

I remember this wasn’t the easiest thing to pull off, but the above code does work, so hopefully this will help you out :smiley: You just need to change what is shown inside the modal that appears when the button is clicked and you should be good to go.

1 Like

Thanks so much dude! Will be experimenting tonight that looks like I should be able to pick it apart, glad you’re still around here and building cool things!

1 Like

Sorry I thought this quesiton had been asked before, I still cannot find the original blog, but does seem the ironman site has been some-what considerably updated. Anyways here is some more forum links on how to achieve this, so you got more than one example to work off of:-

1 Like

I’m still hitting my head against a wall, I’ve tried a lot of things but none seem to be working. The closest I have made it is with this which I stole from another answer:

New-UDTable -Title 'Stuff' -LoadData {
    $TableData = ConvertFrom-Json $Body

    $Data = Invoke-SqlCmd -ServerInstance $serverinstance -Database $databasename -Query "SELECT DISTINCT topic, type FROM topics" | ForEach-Object {
        @{ 
            name = $_.topic 
            host = $_.type
        }
    } 
    $Data | Out-UDTableData -Page $TableData.page -TotalCount $Data.Count -Properties $TableData.properties
} -Columns @(
    New-UDTableColumn -Property 'topic'
    New-UDTableColumn -Property 'type'
)

This at least lets the dashboard load but it is empty (minus indications that it received all 47 rows I was expecting)

I’m assuming the whole $TableData .... $Body stuff is just supposed to work as I haven’t been able to find an example which actually defined $Body explicitly.

I’ve tried several other things like the following which just straight-up don’t work. Any ideas/answers would be tremendously appreciated!

Ideas which failed:

New-UDTable -Columns $columns -LoadData {
    $topics = Invoke-SqlCmd -ServerInstance $serverinstance -Database $databasename -Query "SELECT DISTINCT topic, type FROM topics"
    $tableData = @()
    foreach ($item in $topics) {
        $row = [PSCustomObject]@{
            #topic = $item.topic
            #type = $item.type
            topic = "asdf"
            type = "zxcv"
            #delete = New-UDButton -Text "delete"
        }
        $tableData += $row
    }
    $tableData | Out-UDTableData -Page 1 -TotalCount 10 -Properties @("topic","type")
}

And this example stolen from elsewhere but didn’t actually load apart from the error below.

New-UDTable -Title "Server Information" -Headers @("Severity", "Name",'Affected Hosts') -Endpoint {
    1..10 | foreach {
      [pscustomobject]@{
        Severity = 'CRITICAL'
        Name = New-UDLink -Text 'KB123456' -Url "localhost\kb123456"
        'Affected Hosts' = '123'
      }
    }| Out-UDTableData -Property @("Severity", "Name",'Affected Hosts')
  }

image
I tried adding some variations of -columns and -properties and none of it worked. Not sure what’s supported in the current version versus what was only valid in older versions.

@theabraxas
are you using the old universal dashboard 2.9.0 or the new powershell universal with v3 framework?

1 Like

Hey @wsl2001 - I’m using the new Powershell Universal v3 framework but am very new to it but I used to use the older dashboard tech.

$Body only work if you are using server-side rendering via -LoadData parameter, if your data is static ( not pulling from SQL server for example) need to use -Data parameter

Out-UDTableData only needed for server side

the docs have examples for using both ways
https://docs.ironmansoftware.com/dashboard/components/data-display/table

1 Like

@theabraxas

cool i use the same and i figured out how to get the data and a button in my table, is that what are you looking for?

1 Like

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

@theabraxas
you got it right in the above code , you need to define custom column for your object and use the render scriptblock for the button.

1 Like