From d9f71dc3082a840d7588673312c0259c8cb39920 Mon Sep 17 00:00:00 2001
From: Vincent Baaij The same example, adding a To utilize the SelectColumn feature in the Fluent DataGrid, there are two approaches available:
- Automatic Management via Examples
-
-SelectColumn
, to allow multi-select rows.SelectedItems
-
-
- Items
property.SelectedItems
property.
- Manual Management via Property
and OnSelect
:
-
Property
, OnSelect
, and SelectAll
attributes.Virtualize
or directly managing a custom IsSelected
property.
-
-
-
- By default the Fluent Design System recommends to only use the checkbox to indicate selected rows.
- It is possible to change this behavior by using a CSS style like this to set a background on selected rows:
-
-
-.fluent-data-grid-row:has([row-selected]) > td {
- background-color: var(--neutral-fill-stealth-hover)
-}
-
-
-
-
-
-
- Using this SelectColumn
, you can customize the checkboxes by using ChildContent
to define the contents of the selection for each row of the grid;
- or SelectAllTemplate
to customize the header of this column.
- If you don't want the user to be able to interact (click and change) on the SelectAll header, you can set the SelectAllDisabled="true"
attribute.
-
-
- Example:
-
- Here is an example of a data grid that uses in-memory data and enables features including pagination, sorting, filtering, column options, row highlighting and column resizing. -
-
- All columns, except 'Bronze', have a Tooltip
parameter value of true
.
- When using this for a TemplateColumn
(like 'Rank' here), you need to also supply a value for the TooltipText
parameter. No value given equals no tooltip shown.
- When using this for a PropertyColumn
, a value for the TooltipText
is not required. By default, the value given for Property
- will be re-used for the tooltip. If you do supply a value for TooltipText
its outcome will be used instead.
-
-
- TooltipText
is a lambda function that takes the current item as input and returns the text to show in the tooltip (and aria-label
).
- Look at the Razor tab to see how this is done and how it can be customized.
-
- Here is an example that demonstrates the rank sort. -
-
- If you're using Blazor WebAssembly, it's very common to fetch data from a JSON API on a server. If you want to
- fetch only the data that's needed for the current page/viewport and apply any sorting or filtering rules on the
- server, you can use the ItemsProvider
parameter.
-
- You can also use ItemsProvider
with Blazor Server if it needs to query an external endpoint, or in any
- other case where your requirements aren't covered by an IQueryable
.
-
- To do this, supply a callback matching the GridItemsProvider<TGridItem>
delegate type, where TGridItem
- is the type of data displayed in the grid. Your callback will be given a parameter of type GridItemsProviderRequest<TGridItem>
- which specifies the start index, maximum row count, and sort order of data to return. As well as returning the matching items, you need
- to return a totalItemCount
so that paging or virtualization can work.
-
- Here is an example of connecting a grid to the public OpenFDA Food Enforcement database. -
-- This grid is using a 'sticky' header (i.e. the header is always visible when scrolling). The buttons in the last column disappear under the header when scrolling. - In this example they don't really do anything more than writing a message in the console log' -
-
- The second column has a custom Style
parameter set and applied to it. The 4th column has its Tooltip
- parameter set to true. This will show the full content of the cell when hovering over it. See the 'Razor' tab for how these
- parameters have been applied.
-
- It can be expensive both to fetch and to render large numbers of items. If the amount of data you're - displaying might be large, you should use either paging or virtualization. -
-- Virtualization provides the appearance of continuous scrolling through an arbitrarily-large data set, - while only needing to fetch and render the rows that are currently in the scroll viewport. This can provide - excellent performance even when the data set is vast. FluentDataGrid's virtualization feature is built on Blazor's - built-in Virtualize component, so it shares the - same capabilities, requirements, and limitations. -
-
- Enabling virtualization is just a matter of passing Virtualize="true"
. For it to work
- properly and reliably, every row rendered must have the same known height.
- This is handled by the
- FluentDataGrid
code
- .
-
- Use the options below the grid to toggle the content of this non virtualized grid on and off and to simulate a loading state -
-
- TemplateColumn
uses arbitrary Razor fragments to supply contents for its cells. It can't
- infer the column's title or sort order automatically.
-
- You can make columns appear conditionally using normal Razor logic such as @@if
. Example:
-
- Also, in this example the column's Width parameter is being set instead of specifying all widths for all
- columns in the GridTemplateColumn
parameter.
-
- Here a custom comparer is being used to sort counties by the length of their name. The code has examples for both
- PropertyColumn
and TemplateColumn
implementations (see the Razor tab).
- For this example the code for the comparer is placed in the DataGridCustomComparer.razor.cs
file but it
- could of course be placed in its own file.
-
- For the paginator, this example also shows how to use the SummaryTemplate
and PaginationTextTemplate
parameters.
-
- This example also shows using an OnRowFocus
event callback to detect which row the cursor is over. By setting ShowHover
- to true, the current row will be highlighted. By default the system will use the designated hover color for this but you can specify an alternative
- color by setting the --datagrid-hover-color
CSS variable. See the Razor tab for how this is done in this example.
-
- You can customize the appearance of Paginator
by supplying a SummaryTemplate
.
- If you want further customization, you can create your own alternative UI that works with
- PaginationState
. Example:
-
- TemplateColumn
uses arbitrary Razor fragments to supply contents for its cells. It can't
- infer the column's title or sort order automatically.
-
System.ComponentModel.DataAnnotations.DisplayAttribute
on properties
- shown in the grid.
- MultiLine
to true when you have cells in your data that will take up more than a single line.
-
- Example of using an outside div
and the Style
parameter to achieve a table like DataGrid with infinite horizontal scrollbars to display all content on all devices.
-
- If you use this in combination with a sticky header, the style of the header will be lost for the columns that are rendered out of the view initially.
- You can fix this by adding the following Style
to your data grid: Style="min-width: max-content;"
-
- The example and code below show what you need to add to one of your Blazor page components to implement auto-fit. -
-
- The AutoFit
parameter is used to automatically adjust the column widths to fit the content. It only runs on
- the first render and does not update when the content changes.
-
- The column widths are calculated with the process below: -
GridTemplateColumns
string using the fr
unit
-
- This does not work
- when Virtualization
is turned on. The GridTemplateColumns
parameter is ignored
- when AutoFit
is set to true
. This is untested in MAUI.
-
+ The example and code below show what you need to add to one of your Blazor page components to implement auto-fit. +
+
+ The AutoFit
parameter is used to automatically adjust the column widths to fit the content. It only runs on
+ the first render and does not update when the content changes.
+
+ The column widths are calculated with the process below: +
GridTemplateColumns
string using the fr
unit
+
+ This does not work
+ when Virtualization
is turned on. The GridTemplateColumns
parameter is ignored
+ when AutoFit
is set to true
. This is untested in MAUI.
+
+ The example and code below show what you need to get auto items per page functionality for the pagination of a datagrid. +
++ Resize the page vertically to see the number of rows being displayed per page adapt to the available height. +
+
+ The AutoItemsPerPage
parameter must be set to true and obviously Pagination
must be used as well for this to work.
+ Also, the DataGrid
+
+ An example of how that can be done for this demo site layout is shown in the <style> section below
+
System.ComponentModel.DataAnnotations.DisplayAttribute
on properties
+ shown in the grid.
+
+ Here a custom comparer is being used to sort counties by the length of their name. The code has examples for both
+ PropertyColumn
and TemplateColumn
implementations (see the Razor tab).
+ For this example the code for the comparer is placed in the DataGridCustomComparer.razor.cs
file but it
+ could of course be placed in its own file.
+
+ For the paginator, this example also shows how to use the SummaryTemplate
and PaginationTextTemplate
parameters.
+
+ This example also shows using an OnRowFocus
event callback to detect which row the cursor is over. By setting ShowHover
+ to true, the current row will be highlighted. By default the system will use the designated hover color for this but you can specify an alternative
+ color by setting the --datagrid-hover-color
CSS variable. See the Razor tab for how this is done in this example.
+
+ You can customize the appearance of Paginator
by supplying a SummaryTemplate
.
+ If you want further customization, you can create your own alternative UI that works with
+ PaginationState
. Example:
+
+ You can make columns appear conditionally using normal Razor logic such as @@if
. Example:
+
+ Also, in this example the column's Width parameter is being set instead of specifying all widths for all
+ columns in the GridTemplateColumn
parameter.
+
The same example, adding a SelectColumn
, to allow multi-select rows.
To utilize the SelectColumn feature in the Fluent DataGrid, there are two approaches available:
+ +
+ Automatic Management via SelectedItems
+
Items
property.SelectedItems
property.
+ Manual Management via Property
and OnSelect
:
+
Property
, OnSelect
, and SelectAll
attributes.Virtualize
or directly managing a custom IsSelected
property.
+
+
+
+ By default the Fluent Design System recommends to only use the checkbox to indicate selected rows.
+ It is possible to change this behavior by using a CSS style like this to set a background on selected rows:
+
+
+.fluent-data-grid-row:has([row-selected]) > td {
+ background-color: var(--neutral-fill-stealth-hover)
+}
+
+
+
+
+ Using this SelectColumn
, you can customize the checkboxes by using ChildContent
to define the contents of the selection for each row of the grid;
+ or SelectAllTemplate
to customize the header of this column.
+ If you don't want the user to be able to interact (click and change) on the SelectAll header, you can set the SelectAllDisabled="true"
attribute.
+
+
+ Example:
+
MultiLine
to true when you have cells in your data that will take up more than a single line.
+ + Use the options below the grid to toggle the content of this non virtualized grid on and off and to simulate a loading state +
++ Here is an example that demonstrates the rank sort. +
+
+ If you're using Blazor WebAssembly, it's very common to fetch data from a JSON API on a server. If you want to
+ fetch only the data that's needed for the current page/viewport and apply any sorting or filtering rules on the
+ server, you can use the ItemsProvider
parameter.
+
+ You can also use ItemsProvider
with Blazor Server if it needs to query an external endpoint, or in any
+ other case where your requirements aren't covered by an IQueryable
.
+
+ To do this, supply a callback matching the GridItemsProvider<TGridItem>
delegate type, where TGridItem
+ is the type of data displayed in the grid. Your callback will be given a parameter of type GridItemsProviderRequest<TGridItem>
+ which specifies the start index, maximum row count, and sort order of data to return. As well as returning the matching items, you need
+ to return a totalItemCount
so that paging or virtualization can work.
+
+ Here is an example of connecting a grid to the public OpenFDA Food Enforcement database. +
++ This grid is using a 'sticky' header (i.e. the header is always visible when scrolling). The buttons in the last column disappear under the header when scrolling. + In this example they don't really do anything more than writing a message in the console log' +
+
+ The second column has a custom Style
parameter set and applied to it. The 4th column has its Tooltip
+ parameter set to true. This will show the full content of the cell when hovering over it. See the 'Razor' tab for how these
+ parameters have been applied.
+
+ Example of using an outside div
and the Style
parameter to achieve a table like DataGrid with infinite horizontal scrollbars to display all content on all devices.
+
+ If you use this in combination with a sticky header, the style of the header will be lost for the columns that are rendered out of the view initially.
+ You can fix this by adding the following Style
to your data grid: Style="min-width: max-content;"
+
+ TemplateColumn
uses arbitrary Razor fragments to supply contents for its cells. It can't
+ infer the column's title or sort order automatically.
+
+ TemplateColumn
uses arbitrary Razor fragments to supply contents for its cells. It can't
+ infer the column's title or sort order automatically.
+
+ Here is an example of a data grid that uses in-memory data and enables features including pagination, sorting, filtering, column options, row highlighting and column resizing. +
+
+ All columns, except 'Bronze', have a Tooltip
parameter value of true
.
+ When using this for a TemplateColumn
(like 'Rank' here), you need to also supply a value for the TooltipText
parameter. No value given equals no tooltip shown.
+ When using this for a PropertyColumn
, a value for the TooltipText
is not required. By default, the value given for Property
+ will be re-used for the tooltip. If you do supply a value for TooltipText
its outcome will be used instead.
+
+
+ TooltipText
is a lambda function that takes the current item as input and returns the text to show in the tooltip (and aria-label
).
+ Look at the Razor tab to see how this is done and how it can be customized.
+
+ It can be expensive both to fetch and to render large numbers of items. If the amount of data you're + displaying might be large, you should use either paging or virtualization. +
++ Virtualization provides the appearance of continuous scrolling through an arbitrarily-large data set, + while only needing to fetch and render the rows that are currently in the scroll viewport. This can provide + excellent performance even when the data set is vast. FluentDataGrid's virtualization feature is built on Blazor's + built-in Virtualize component, so it shares the + same capabilities, requirements, and limitations. +
+
+ Enabling virtualization is just a matter of passing Virtualize="true"
. For it to work
+ properly and reliably, every row rendered must have the same known height.
+ This is handled by the
+ FluentDataGrid
code
+ .
+