How to clear udgrid?

This might be a newb question but how do you go about clearing a udgrid? Passing $null or “” | out-udgrid does nothing. clear-udelement doesnt work either. I have the code I am testing with below.

New-UDCard -Title "Target List" -Endpoint{
New-UdGrid -Id "Target" -AutoRefresh -RefreshInterval 1 -Endpoint {

    $modlist = @()
    $alist = @()

    if($session:slist.count -gt 0){
        $alist = $session:slist.ToArray() | sort -Unique

    $alist | Foreach-Object {
            [PSCustomObject]@{
                Name = $_.Name
                
                Remove = New-UDButton -Text "Remove" -OnClick {
                        $session:slist.Clear()
                        $newlist = $_
                        $modlist = $alist | ?{$_ -ne $newlist}
                        $modlist | %{$session:slist.Add($_)}
                 }
                    
             }
     } | Out-UDGridData
     }else{
        $null | Out-UDGridData
     }
}
    New-UDButton -Text "Clear List" -OnClick {
                        $session:slist.clear()
                        }

} 

}

In your button, force the grid to sync:

New-UDButton -Text "Clear List" -OnClick {
                        $session:slist.clear()
                        Sync-UDElement -Id "Target"
                        }
1 Like

Neat, I didn’t know about sync-udelement it definitely makes it more responsive when modifying the list, however even when added to the clear list button nothing happens. Also if I add items to the list and then remove them one by one the last one doesn’t remove and just stays around. I know the $session:slist is clear though because if I add another item back to the list the one that was stuck there goes away.

New-UdGrid -Id "Target" -AutoRefresh -RefreshInterval 5 -Endpoint {

    $modlist = @()
    $alist = @()

    if($session:slist.count -gt 0){
        $alist = $session:slist.ToArray() | sort -Unique
     }

    $alist | Foreach-Object {
            [PSCustomObject]@{
                Name = $_.Name
                
                Remove = New-UDButton -Text "Remove" -OnClick {
                        $session:slist.Clear()
                        $newlist = $_
                        $modlist = $alist | ?{$_ -ne $newlist}
                        $modlist | %{$session:slist.Add($_)}
                        Sync-UDElement -Id "Target"
                 }
                    
             }
     } | Out-UDGridData
}
    New-UDButton -Text "Clear List" -OnClick {
                        $session:slist.clear()
                        Sync-UDElement -Id "Target"
                        }

}