The UDDataGrid component is an advanced version of the table that is useful for displaying large amounts of data. It supports many of the same features as the table but also provides complex filtering, row virtualization, multi-column sort and more.
Simple Data Grid
Data grids load their data via the -LoadRows event handler. You will need to return a hashtable that contains the row data and the total number of rows.
Columns are defined using hashtables.
New-UDDataGrid-LoadRows { $Data =@(@{ Name ='Adam'; Number =Get-Random}@{ Name ='Tom'; Number =Get-Random}@{ Name ='Sarah'; Number =Get-Random} ) $Data |Out-UDDataGridData-Context $EventData -TotalRows $Rows.Length} -Columns @(New-UDDataGridColumn-Field nameNew-UDDataGridColumn-Field number) -AutoHeight $true
Columns
Columns are customizable using New-UDDataGridColumn. More information on this cmdlet can be found here.
Rendering Custom Columns
You can render custom components in columns by specifying render within the column hashtable. You can access the current row's data by using the $EventData or $Row variable
In this example, the number is shown in the name column with a New-UDTypography component.
New-UDDataGrid-LoadRows { $Rows =1..100|% {@{ Name ='Adam'; Number =Get-Random} } $Rows|Out-UDDataGridData-Context $EventData -TotalRows $Rows.Length} -Columns @(New-UDDataGridColumn-Field name -Render {New-UDTypography $EventData.number }New-UDDataGridColumn-Field number) -AutoHeight $true
Flexible Width Columns
Column fluidity or responsiveness can be achieved by setting the flex property of a column.
The flex property accepts a value between 0 and ∞. It works by dividing the remaining space in the grid among all flex columns in proportion to their flex value.
For example, consider a grid with a total width of 500px that has three columns: the first with width: 200; the second with flex: 1; and the third with flex: 0.5. The first column will be 200px wide, leaving 300px remaining. The column with flex: 1 is twice the size of flex: 0.5, which means that final sizes will be: 200px, 200px, 100px.
To set a minimum and maximum width for a flex column set the minWidth and the maxWidth property on the column.
New-UDDataGrid-LoadRows { $Rows =1..100|% { @{ Name = 'Adam'; Number = "This column is a very long string. This column is a very long string. This column is a very long string. This column is a very long string. This column is a very long string. This column is a very long string."}
} $Rows|Out-UDDataGridData-Context $EventData -TotalRows $Rows.Length} -Columns @(New-UDDataGridColumn-Field name -Render {New-UDTypography $EventData.number }New-UDDataGridColumn-Field number -Flex 1.0) -AutoHeight $true
LoadRows
The -LoadRows parameter is used to return data for the data grid. Table state will be provided to the event handler as $EventData. You will find the following properties within the $EventData object.
Paging
To implement paging, you can access the page and pageSize properties of the $EventData variable. Out-UDDataGridData automatically implements paging.
New-UDDataGrid-LoadRows { $Rows =1..100|% {@{ Name ='Adam'; Number =Get-Random} } $Rows|Out-UDDataGridData-Context $EventData -TotalRows $Rows.Length} -Columns @(New-UDDataGridColumn-Field nameNew-UDDataGridColumn-Field number) -AutoHeight $true-Pagination
Filtering
The filter hashtable is included in the $EventData for the -LoadRows event handler when a filter is defined. The hashtable has a structure as follows.
The items property contains an array of columns, operators and values. You can use these to filter your data.
LinkOperator
The link operator field is used to specify the link between the filters. This can be and or or.
Sorting
The $EventData object will contain a Sort property when the user sorts the data grid. It contains properties for each column that is sorted. The properties will start as 0 and increment as more columns are sorted.
For example, you can access the first sorted column as follows.
$EventData.Sort.'0'.field
You will also receive the sort direction for each column.
Detailed Content
You can use the -LoadDetailedContent event handler to display additional information about the row you are expanding. Information about the current row is available in $EventData.row.
New-UDDataGrid-LoadRows { $Data =@(@{ Name ='Adam'; Number =Get-Random }@{ Name ='Tom'; Number =Get-Random }@{ Name ='Sarah'; Number =Get-Random } ) $Data|Out-UDDataGridData-Context $EventData -TotalRows $Data.Length} -Columns @(New-UDDataGridColumn-Field nameNew-UDDataGridColumn-Field number) -AutoHeight $true-LoadDetailContent {Show-UDToast $BodyNew-UDAlert-Text $EventData.row.Name}
Editing
Tables provide editor support by specifying the -OnEdit event handler. The new row data will be provided as $EventData. You can chose to return updated row information (for example, adjusting something the user has entered) and return it from the event handler. If you do not return anything, the row will reflect what the user entered.
The $EventData has the following format.
@{ newRow =@{} oldRow =@{}}
Ensure that you provide the editable property to each column you wish for the user to edit.
New-UDDataGrid-LoadRows { $Data =@(@{ Name ='Adam'; number =Get-Random }@{ Name ='Tom'; number =Get-Random }@{ Name ='Sarah'; number =Get-Random } ) $Data|Out-UDDataGridData-Context $EventData -TotalRows $Data.Length} -Columns @(New-UDDataGridColumn-Field name -EditableNew-UDDataGridColumn-Field number -Editable) -AutoHeight $true-OnEdit {Show-UDToast"Editing $Body"}
Custom Export
To override the default export functionality, use the -OnExport event handler. $EventData will be the same context object used for -LoadRows. You should use Out-UDDataGridExport to return the data from -OnExport.
$Data =@(@{ name ='Adam'; Number =Get-Random}@{ name ='Tom'; Number =Get-Random}@{ name ='Sarah'; Number =Get-Random})New-UDDataGrid-LoadRows {@{ rows = $Data rowCount = $Data.Length }} -Columns @(New-UDDataGridColumn-Field nameNew-UDDataGridColumn-Field number) -OnExport { $ExportContent = $Data |ConvertTo-Csv-NoTypeInformation |Out-StringOut-UDDataGridExport-Data $ExportContent -FileName 'export.csv'}
Example: Static Data
In this example, we generate an array of 10,000 records. We will create a new function, Out-UDDataGridData to manage the paging, sorting and filtering. This function is already included in the Universal module.