Out-UDGridData with selectable rows?

Oooh! I think I discovered the better way using UDTableGrid and UDElement from this forum post: Coloring Elements in a Table based on a property - The downside is no “filter” feature, or column sorting. But I’m still learning.

I know others have attempted this: https://github.com/ironmansoftware/universal-dashboard/issues/911

There are some issues with it as seen in that issue.

I’m looking at introducing a new grid with many more features in the next minor version of UD - https://github.com/ironmansoftware/universal-dashboard/issues/697

That would allow for more of this type of functionality.

2 Likes

nice! keep us posted?

Of course! When I release that, you’ll know :wink:

1 Like

Hey @dtnyc9005 Did you ever get a working prototype for this? Im looking for the same functionality.

Hi J-C, I did not get a working prototype. The project I was working on, which that was for, got cancelled, and I was pulled in a different direction.

I think I figure Something out. I at least solved the problem I set out to solve.

Import-Module UniversalDashboard

Get-UDDashboard | Stop-UDDashboard

$ProcessList = Get-Process

$theme = Get-UDTheme -Name 'DefaultTight'

$Cache:CheckBoxElementArray = @{ }

$Dashboard = New-UDDashboard -Title "Process Info" -Theme $theme -Content {

    $GridSplat = @{

        Title      = "Process Information"

        Headers    = @(' ', "Select", "Name", "Id", "Handles", "Info")

        Properties = @(' ', "Select", "Name", "Id", "Handles", "info") # Case must match PSCO

        Endpoint   = {

            $Cache:CheckBoxElementArray = @{ }

            $ProcessList | Select-Object | ForEach-Object {

                [PSCustomObject]@{

                    Select  = New-UDCheckbox -Id $_.Id -OnChange (

                        New-UDEndpoint -Endpoint {

                            $CheckBoxElement = Get-UDElement -Id $_.Id

                            $Cache:CheckBoxElementArray[$_.Id] = @{

                                Checked = $CheckBoxElement.Attributes["checked"]

                                Name    = $_.ProcessName

                                Id      = $_.Id

                                Handles = $_.Handles

                            }

                        })

                    Name    = $_.ProcessName

                    Id      = $_.Id

                    Handles = $_.Handles

                    info    = New-UDButton -Text "Info" -OnClick (New-UDEndpoint -Endpoint {

                            Show-UDModal -Content {

                                New-UDTable -Title "Info" -Headers @( "Process Name", "Process Id", "Handles")  -Content {

                                    Get-Process -Id $_.Id | ForEach-Object {

                                        [PSCustomObject]@{

                                            Handles = $_.Handles

                                            Name    = $_.ProcessName

                                            Id      = $_.Id

                                        }

                                    } | Out-UDTableData -Property @(  "Name", "Id", "Handles")

                                }

                            }

                        })

                }

            } | Out-UDGridData

        }

    }

    New-UDButton -Text "Select Process" -OnClick (New-UDEndpoint -Endpoint {

            Show-UDModal -Height '60%' -Width '90%' -Content {

                New-UDGrid @GridSplat -PageSize 5

                New-UDButton -Text "Show Selections" -OnClick (

                    New-UDEndpoint -Endpoint {

                        Show-UDModal -Content {

                            New-UDTable -Title 'Selected' -Headers @("Name", "Id", "Handles") -Content {

                                foreach ($Key in $Cache:CheckBoxElementArray.Keys) {

                                    [PSCustomObject]@{

                                        Name    = $Cache:CheckBoxElementArray[$key]['Name']

                                        Id      = $Cache:CheckBoxElementArray[$key]['Id']

                                        Handles = $Cache:CheckBoxElementArray[$key]['Handles']

                                    } | Out-UDTableData -Property @("Name", "Id", "Handles")

                                }

                            }

                        }

                    }

                )

            }

        })

}

Start-UDDashboard -Dashboard $Dashboard -Port 10001
3 Likes

If anyone has any constructive criticism regarding my code in the above example feel free to lmk :slight_smile:

@J-C Thanks for sharing. I’m guessing shared code is likely different from what you really implement. Anyway if someone doesn’t like it they are free to change it as desired. My preference is 2 spaces indentation between blocks of code. In VScode, I set code folding preference to ‘indentation’.

   if (condition) {
     "this is true"
   } else {
     "this is false"
   }

Now in large code files ‘Ctrl K+0’ will collapse down to a single page in VScode ususally. I know others like to have multiple files per project which make sense for larger projects. Many moons ago when I was doing system programming, it wasn’t unusual to be working with code files with 100,000 lines of code or bigger. This was mainframe stuff. I think is best to develop your own style and stick to it and be acceptable of other styles that don’t match your desires.

1 Like

image

After changing the identation only, this is how your code folded up for me.

1 Like

Thanks for the tip!
I’m using format on save in my VScode settings which is responsible for the indentation structure atm.

Formatting aspects aside, the results look pretty good. Thanks!

1 Like

Just got around to running dashboard code myself and it worked great on 2.7.0

Had issues running on 2.9.0 though.

Thanks again for sharing.

1 Like

@J-C

Hi! Is it possible to exclude uncheck item? It seems to be added to the array, even when I uncheck the item.

@cn9ne Thanks for bringing this to my attention. I’m not really sure how to handle this.
Any ideas @psDevUK @adam ?

I"m running similar code to the above and getting the checked item hanging around longer than desired. A partial solution for me was to initialize

$Cache:CheckBoxElementArray = @{ }

after handling the checked event. The page seems to need a totally refreshed for the prior checked item to be totally forgotten. This is as far as I have gotten with the issue.

@cn9ne I got it to work by leaving $Cache:CheckBoxElementArray = @{ } where it was and wrapping the $Cache:CheckBoxElementArray[$_.Id] hashtable in an if/else:

if ($CheckBoxElement.Attributes["checked"]) {
$Cache:CheckBoxElementArray[$_.Id] = @{
    Checked = $CheckBoxElement.Attributes["checked"]
    Name    = $_.ProcessName
    Id      = $_.Id
    Handles = $_.Handles
}
}
else {
    $Cache:CheckBoxElementArray.Remove($($_.Id))
}

@J-C Sorry I’ve not been following this whole thread, just skimmed through it now, but, wouldnt you want to use $session instead of $cache? the reason being is that modifications to cache would apply for everyone, so if you get two people browsing the page at the same time and making similar modifications to the same items it could cause issues, while session is relevant to only the visiting user.

1 Like

@insomniacc Yes, makes perfect sense that $Session: over $Cache: should be used.

@J-C Thanks the if block around setting the hashtable did the trick.

1 Like

I’m new to UD and was unaware of $Session: I really appreciate this response and the explanation. thanks!