Setting Selected row in UD-Table - Possible?

Setting Selected row in UD-Table - Possible?

avatar
Product: PowerShell Universal
Version: 5.6.1


Hi all, is it possible to set a UD-Table’s row selection programmatically? I need to have options selected based off of a second table’s selection. From what I can tell, there is no native support. I’ve tried a workaround by appending a ‘checked’ property to my data, and having a new-udbutton be created on the data columns -OnRender event; however, this only works if I have one page of data. Once I click on the second page, the data resets and it doesn’t work with a sync if I wrap it in a UD-Dynamic.

I figured it was a native feature, because ya know, that’s how tables normally work. Anyone have any insight / workarounds?

avatar
(anonymous user)

Recommended Answer

If I’ve understood you correctly, then you want to update a column that contains a checkbox based on whatever your data provides?

Here is an example If that the case:

New-UDApp -Content { 

    
    $DummyData = @()
    1..20 | ForEach-Object {
        $DummyData += [PSCustomObject]@{
            ID       = $_
            Name     = "Item $_"
            Category = "A" # A, B, C, D, E
            Price    = [math]::Round((Get-Random -Minimum 10 -Maximum 200))
            InStock  = (Get-Random -Minimum 0 -Maximum 2) -eq 1
        }
    }

    New-UDTable -Id "DummyTable" -Data $DummyData -ShowFilter -ShowSort -ShowPagination -PageSize 10 -Columns @(
        New-UDTableColumn -Property "ID"       -Title "ID"       -ShowFilter
        New-UDTableColumn -Property "Name"     -Title "Name"     -ShowFilter
        New-UDTableColumn -Property "Category" -Title "Category" -ShowFilter -FilterType select
        New-UDTableColumn -Property "Price"    -Title "Price"    -ShowSort
        New-UDTableColumn -Property "InStock"  -Title "In Stock" -ShowFilter -FilterType select
        New-UDTableColumn -Property "CheckBox" -OnRender {

            ## Your logic for if the checkbox is checked or not - Here I'm checking all rows where the price is lower than 50
            $isChecked = ([int]$EventData.Price) -lt [int]'50'
            New-UDCheckBox -Id ("chkprice_" + $EventData.Price) -Checked $isChecked

        }
    ) -InitialState @{
        orderBy = @{ field = "ID" }
    } -ShowSelection


}


This will update the checkbox based on the logic in the -onrender component of the new-udtablecolumn. So basically check any row with a price below 50. This also works across pagination.

All Comments (2)

avatar

If I’ve understood you correctly, then you want to update a column that contains a checkbox based on whatever your data provides?

Here is an example If that the case:

New-UDApp -Content { 

    
    $DummyData = @()
    1..20 | ForEach-Object {
        $DummyData += [PSCustomObject]@{
            ID       = $_
            Name     = "Item $_"
            Category = "A" # A, B, C, D, E
            Price    = [math]::Round((Get-Random -Minimum 10 -Maximum 200))
            InStock  = (Get-Random -Minimum 0 -Maximum 2) -eq 1
        }
    }

    New-UDTable -Id "DummyTable" -Data $DummyData -ShowFilter -ShowSort -ShowPagination -PageSize 10 -Columns @(
        New-UDTableColumn -Property "ID"       -Title "ID"       -ShowFilter
        New-UDTableColumn -Property "Name"     -Title "Name"     -ShowFilter
        New-UDTableColumn -Property "Category" -Title "Category" -ShowFilter -FilterType select
        New-UDTableColumn -Property "Price"    -Title "Price"    -ShowSort
        New-UDTableColumn -Property "InStock"  -Title "In Stock" -ShowFilter -FilterType select
        New-UDTableColumn -Property "CheckBox" -OnRender {

            ## Your logic for if the checkbox is checked or not - Here I'm checking all rows where the price is lower than 50
            $isChecked = ([int]$EventData.Price) -lt [int]'50'
            New-UDCheckBox -Id ("chkprice_" + $EventData.Price) -Checked $isChecked

        }
    ) -InitialState @{
        orderBy = @{ field = "ID" }
    } -ShowSelection


}


This will update the checkbox based on the logic in the -onrender component of the new-udtablecolumn. So basically check any row with a price below 50. This also works across pagination.

avatar

Thanks for that snippit, it seemed to work. It’s very similar to what I was doing, except I was referencing a session-scope variable to see if it was checked. Not sure why, but it was out of scope and wasn’t able to get referenced. Re-calculating to see what needed to be checked on-render works.

I think there are some other underlaying issues with what I’m doing (or PSUniversal, not sure), because the table only renders 50% of the time once I wrap it in a UD-Dynamic. Statically works well, I need it in the UD-Dynamic because I need it to refresh based on the selection in the other table. Works after a couple page refreshes.

Anyways I ended up just writing this in C# Windows Form the other day and moved all of my Active Directory and SQL calls into an endpoint on PSUniversal that I just call from the desktop app. It’s probably something that I’m doing, but the webapp just wasn’t reliable enough and the winforms just works.

Anyways, thanks again for your response!

Closed