Sync-UDElement stops working

I am working on a filter checkbox to filter a grid list and i’m using the Sync-UDElement to update the grid list. After interacting with the checkbox elements a couple of times, the Sync-UDElement stops triggering. I’m not sure if this is the best route to what I want to accomplish but I was wondering if anyone else experienced this same issue? Also here’s a snippet of what I want to do:

   Start-UDDashboard -Dashboard ( 
      
    New-UDDashboard -Title "Service Status" -Content {

     $Cache:Filter = New-Object System.Collections.ArrayList

     $Cache:Data = @(
                        [pscustomobject]@{Platform = "Joey";Service="Joeys Bike";Status="Working"},
                        [pscustomobject]@{Platform = "Joey";Service="Joeys Car";Status="Working"},
                        [pscustomobject]@{Platform = "Bill";Service="Bill's Car";Status="Out of Service"},
                        [pscustomobject]@{Platform = "Bill";Service="Bill's Bike";Status="Working"},
                        [pscustomobject]@{Platform = "Susan";Service="Susan's Bike";Status="Working"},
                        [pscustomobject]@{Platform = "Susan";Service="Susan's Car";Status="Working"}
                    )
        New-UDLayout -Columns 3 -Content{
             
             #Platform Filter
             New-UDCard -Title "Platform Filter" -Endpoint {
                $Data = $Cache:Data
                    foreach ($Platform in ($Data.Platform | select -Unique)){
                        New-UDCheckbox -Id $Platform -Label "$Platform" -OnChange {
                             
                                #If Checked
                                if ($EventData){    
                                    #Check if doesn't Exist
                                    if ($Cache:Filter.IndexOf($Platform) -lt 0){
                                        $Cache:Filter.Add($Platform)
                                        
                                        
                                    }

                                }else{
                            
                                    #Remove if exists
                                    if ($Cache:Filter.IndexOf($Platform) -ge 0){
                                        $Cache:Filter = $Cache:Filter | ?{$_ -ne $Platform} 
                                    }
                                }
                                Sync-UDElement -Id FilterList
                                Sync-UDElement -Id GridServiceStatus
                        }
                    }
            }
            
            #List of Applied Filters
            New-UDGrid -Id FilterList  -Endpoint {
                        $Cache:Filter | % { [PSCustomObject]@{Filter = $_} } | Out-UDGridData 
            } -Headers @("Filter") -Properties @("Filter")             
        }

        #Platform Grid List
        New-UDGrid -Title "Service Status" -Headers @("Platform", "Service", "Status") -Properties @("Platform", "Service", "Status") -Endpoint {
            $Data = $Cache:Data

            
            #Filter if filter was provided
            if ($Cache:Filter.count -gt 0){
                
                $Data = $Data |? {$Cache:Filter.indexOf($_.Platform) -ge 0}
            }

            #Send Data out
            $Data | Out-UDGridData
        } -Id GridServiceStatus
    
    }
) -Port 1000

Can you get some diagnostic info? Open the web browser developer tools, usually F12 and see if you are getting any errors. Enable UD logging via Enable-UDLogging.

I’m not seeing any errors in the console and network sections in chrome. Looking at the logs, I only see this error:

22:02:26 [Warn] ExecutionService Error executing endpoint script. You cannot call a method on a null-valued expression. 
 at <ScriptBlock>, <No file>: line 3
22:02:26 [Warn] ExecutionService Error executing endpoint script. You cannot call a method on a null-valued expression. 
 at <ScriptBlock>, <No file>: line 10

I’m not sure in the script where this is happening.

Hello adam,

The issue is still there but I added a “apply” button to the filter and it’s working .

Hi,

I’m aware i’m a bit late but i ran into this problem lately and i found a working solution so i thought i can share it, maybe it will help in the future.

The problem resided in the New-UDCheckbox

Here is the sample that work for me:

  New-UDCheckbox -Id $Platform -Label "$Platform" -OnChange {
                                     if ($EventData){ 
                                #Check if doesn't Exist
                                if($Cache:Filter.IndexOf($Platform) -le 0){
                                    $Cache:Filter.Add($Platform)
                         
                 } 
                     }
                          #Remove if exists

                           if(!$EventData){
                                    $Cache:Filter.Remove($Platform)
                                    
                         }
                         if($Cache:Filter.Count -cle 0 ){
                          
                         }

                             Sync-UDElement -Id FilterList
                            Sync-UDElement -Id GridServiceStatus
                             
                    }

For the array check…

if($Cache:Filter.IndexOf($Platform) -le 0){

you need to use -lt instead of -le. Otherwise you’ll get a false positive if $Platform is element 0.

You might consider using a hashset or a hashtable instead of the arraylist or list you are using, which would allow you to skip the check.

Thanks,
Tim Curwick

Hi Tim,

Thanks for pointing out the -lt tricks, i replaced it.

I agree a hashtable would be a better way. I will get over it asap.

Thanks