Export from UDTable when table has only 1 element

Product: PowerShell Universal
Version: 5.5.4

I have an app page that generates a table.
The table allows for export.

All works fine if there are more then 1 rows in the table.

When there is only one element in the data array, table display still works fine.
But data export does not produce any downloads.

I know that it can be tricky in powershell to work with arrays that have only one element.
But I tried everything ( [array]data= or ,$data or @($data) …

Any ideas?

As usual, when I finally ask the forum, I have an idea… :wink:

if element count is 1, force JSON conversion as array

# just generate some Data array
$Data=@()
0..10|%{
    $Data+=[PSCustomObject]@{
        #column1=[char](64+$_)
        column1=$_
        column2="A"
    }
}




New-UDGrid -Container -Content {
    New-UDGrid -Item -ExtraSmallSize 12 -Content {
        New-UDTypography -Align right -text "$v $DashboardName" -Variant caption 
    }

    New-UDGrid -Item -ExtraSmallSize 12 -Content {
        New-UDTable -Id "table" -loadData   {
            foreach($Filter in $EventData.Filters) {
                $Data=$Data | ?{$_.$($filter.id) -like "*$($filter.value -replace '\.','?')*"}
            }
            $tdc=$Data.count

            if (-not [string]::IsNullOrEmpty($EventData.OrderBy.Field))
            {
                $Descending = $EventData.OrderDirection -ne 'asc'
                $Data = $Data | Sort-Object -Property ($EventData.orderBy.Field) -Descending:$Descending
            }

            $Data = $Data | Select-Object -First $EventData.PageSize -Skip ($EventData.Page * $EventData.PageSize)
            $Data| Out-UDTableData -Page $EventData.Page -TotalCount $tdc -Properties $EventData.Properties
        } -Columns @(
            $Data[0].psobject.properties | %{
                New-UDTableColumn -Property $_.name -ShowFilter -IncludeInExport -FilterType text
            }
        )  -OnExport {
            $Query = $Body | ConvertFrom-Json
            Show-UDToast -Message($query|out-string) -Duration 100000
            if ($query.allrows -eq "true") {
                $out=$Data
            } else {
                $out=$Data
                foreach($filter in $query.filters) {
                    $out=$out|?{$_.$($filter.id) -like "*$($filter.value -replace '\.','?')*"}
                }
            }
            Show-UDToast -Message "$($out|ConvertTo-Json) $($out.count)" -Duration 100000

            # if element count is 1, force JSON conversion as array
            $splat=($out.count -eq 1) ? @{AsArray=$true} :  @{AsArray=$false}
            $out|ConvertTo-Json @splat

        } -ShowPagination -ShowFilter -ShowSort -ShowExport -PageSize 10 -dense
    }
}