New-UDDataGrid sort and filter not working

Product: PowerShell Universal
Version: 3.3.6

Hey everyone, what am I missing here with sort and filter?

Sort does nothing, and Filter throws errors for each row:

An error occurred: You cannot call a method on a null-valued expression.
Endpoint: feac84e3-09ed-4401-8512-2d9d38b101c9
Session: 9cd5ecec-cc62-42ef-947f-0a27d9bb1aea, User:
Stack Trace:

  • You cannot call a method on a null-valued expression. at feac84e3-09ed-4401-8512-2d9d38b101c9:94
  • feac84e3-09ed-4401-8512-2d9d38b101c9:65
  • dynamic:63
  • c6388a1e-8793-4c6c-a5ff-171764159f39:62
$test = @{
    field1 = "apple"
    field2 = "banana"
},@{
    field1 = "toast"
    field2 = "hats"
}

$test | export-csv /home/csv.csv

function Out-UDDataGridData {
    param(
        [Parameter(Mandatory)]
        $Context, 
        [Parameter(Mandatory, ValueFromPipeline)]
        [object]$Data, 
        [Parameter()]
        [int]$TotalRows = -1
    )

    Begin {
        $Items = [System.Collections.ArrayList]::new()
    }

    Process {
        $Items.Add($Data) | Out-Null
    }

    End {
        if ($TotalRows  -eq -1)
        {
            $TotalRows = $Items.Count
        }

        $Filter = $Context.Filter 
        foreach ($item in $Filter.Items) {
            $Property = $item.columnField
            $Value = $item.Value
            switch ($item.operatorValue) {
                "contains" { $Items = $Items | Where-Object {  $_[$Property].ToString().Contains($Value)}; } 
                "equals" { $Items = $Items | Where-Object {  $_[$Property].ToString() -eq $Value } }  
                "startsWith" { $Items = $Items | Where-Object { $_[$Property].ToString().StartsWith($Value)} }
                "endsWith" { $Items = $Items | Where-Object { $_[$Property].ToString().EndsWiths($Value)} }
                "isEmpty" { $Items = $Items | Where-Object { [string]::IsNullOrEmpty($_[$Property].ToString()) } }
                "isNotEmpty" { $Items = $Items | Where-Object { -not [string]::IsNullOrEmpty($_[$Property].ToString()) } }
                "isAnyOf" { $Items = $Items | Where-Object { $_[$Property].ToString() -in $Value } }
            }
        }

        $Sort = $Context.Sort.'0'
        $Items = $Items | Sort-Object -Property $Sort.field -Descending:$($Sort.Sort -eq 'desc')

        $Items = $Items | Select-Object -Skip ($Context.Page * $Context.pageSize) -First $Context.PageSize

        @{
            rows     = [Array]$Items 
            rowCount = $TotalRows
        }
    }    
}

$page = new-udpage -name 'test' -content {

    New-UDDynamic -id 'dynamic' -Content {
        
        $data = import-csv "/home/csv.csv"

        New-UDDataGrid -LoadRows {  
            $data | Out-UDDataGridData -context $eventdata
        } -Columns @(
            @{ field = 'field1' ; Filterable = $true ; Sortable = $true }
            @{ field = 'field2' ; Filterable = $true ; Sortable = $true }
        ) -AutoHeight -Pagination

    }
}

# finally, create a dashboard page with the title and the page content
New-UDDashboard -Title 'test' -pages $page

You need to replace…

$_[$Property]

With

$_.$Property

Reason being, in the documentation the object is a hashtable. In your code, you are exporting it to a csv, then re-importing it, which means it’s no longer a hashtable, but an object.

Also that New-UDDynamic isn’t buying you anything with a datagrid. -LoadRows is already dynamic.

I thought that’s where the problem was, thanks for the reply. Filtering appears to be working now.

It seems like sorting might be broken for a similar reason.

Do you know if -LoadRows and -OnRowExpand work? (I will open a new thread if I can’t figure it out).

Yep, sort needs to be…

$Sort = $Context.Sort

You’re looking for the “-LoadDetailContent {}” parameter.

1 Like