From 95c160baea9fe1a2fb1e0de519f47baa668340e4 Mon Sep 17 00:00:00 2001 From: Phillip Hoff Date: Fri, 30 May 2025 16:09:23 -0700 Subject: [PATCH 1/3] Sketch implementation of FluentDataGrid HeaderCellTitleTemplate. --- .../Examples/DataGridCustomHeader.razor | 52 +++++++++++++++++++ .../DataGridColumnHeaderGenerationPage.razor | 9 ++++ .../DataGrid/Columns/ColumnBase.razor | 33 ++++++++++-- .../DataGrid/Columns/ColumnBase.razor.cs | 7 +++ 4 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomHeader.razor diff --git a/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomHeader.razor b/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomHeader.razor new file mode 100644 index 0000000000..6dd4e5cce1 --- /dev/null +++ b/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomHeader.razor @@ -0,0 +1,52 @@ +@using FluentUI.Demo.Shared.Pages.DataGrid.Examples +@using Microsoft.FluentUI.AspNetCore.Components + + + + + + + @context.Title + + + + + + + + + Birth date + + + + + +@code { + public class Person + { + public Person(int personId, string name, DateOnly birthDate) + { + PersonId = personId; + Name = name; + BirthDate = birthDate; + } + + public int PersonId { get; set; } + + public string Name { get; set; } + + public DateOnly BirthDate { get; set; } + } + + IQueryable people = new[] + { + new Person(10895, "Jean Martin", new DateOnly(1985, 3, 16)), + new Person(10944, "António Langa", new DateOnly(1991, 12, 1)), + new Person(11203, "Julie Smith", new DateOnly(1958, 10, 10)), + new Person(11205, "Nur Sari", new DateOnly(1922, 4, 27)), + new Person(11898, "Jose Hernandez", new DateOnly(2011, 5, 3)), + new Person(12130, "Kenji Sato", new DateOnly(2004, 1, 9)), + }.AsQueryable(); +} \ No newline at end of file diff --git a/examples/Demo/Shared/Pages/DataGrid/Pages/DataGridColumnHeaderGenerationPage.razor b/examples/Demo/Shared/Pages/DataGrid/Pages/DataGridColumnHeaderGenerationPage.razor index 8846b0ee25..845a61f54d 100644 --- a/examples/Demo/Shared/Pages/DataGrid/Pages/DataGridColumnHeaderGenerationPage.razor +++ b/examples/Demo/Shared/Pages/DataGrid/Pages/DataGridColumnHeaderGenerationPage.razor @@ -11,3 +11,12 @@ See the 'Razor' tab on how these attributes have been applied to the class properties. + + + + The DataGrid can display custom column header titles by setting the HeaderCellTitleTemplate property on columns + shown in the grid. +
+ See the 'Razor' tab on how these properties have been applied to the columns. +
+
diff --git a/src/Core/Components/DataGrid/Columns/ColumnBase.razor b/src/Core/Components/DataGrid/Columns/ColumnBase.razor index fac2fb8d67..f77e151c8d 100644 --- a/src/Core/Components/DataGrid/Columns/ColumnBase.razor +++ b/src/Core/Components/DataGrid/Columns/ColumnBase.razor @@ -22,7 +22,16 @@ @if (AnyColumnActionEnabled) { -
@Title
+
+ @if (HeaderCellTitleTemplate is not null) + { + @HeaderCellTitleTemplate(this) + } + else + { + @Title + } +
@if (Grid.SortByAscending.HasValue && IsActiveSortColumn) { @@ -44,7 +53,16 @@ else {
-
@Title
+
+ @if (HeaderCellTitleTemplate is not null) + { + @HeaderCellTitleTemplate(this) + } + else + { + @Title + } +
} @@ -122,7 +140,16 @@ { -
@Title
+
+ @if (HeaderCellTitleTemplate is not null) + { + @HeaderCellTitleTemplate(this) + } + else + { + @Title + } +
@if (Grid.SortByAscending.HasValue && IsActiveSortColumn) { diff --git a/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs b/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs index 3bcaa0431e..4f634c117e 100644 --- a/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs +++ b/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs @@ -83,6 +83,13 @@ public abstract partial class ColumnBase [Parameter] public RenderFragment>? HeaderCellItemTemplate { get; set; } + /// + /// Gets or sets a template for the title content of this column's header cell. + /// If not specified, the default header template includes the . + /// + [Parameter] + public RenderFragment>? HeaderCellTitleTemplate { get; set; } + /// /// If specified, indicates that this column has this associated options UI. A button to display this /// UI will be included in the header cell by default. From 585666a0311a24b36dd3eb0b59ccdf31d6bbd51e Mon Sep 17 00:00:00 2001 From: Phillip Hoff Date: Mon, 2 Jun 2025 15:34:31 -0700 Subject: [PATCH 2/3] Consolidate header title rendering. --- .../DataGrid/Columns/ColumnBase.razor | 41 ++++++++----------- .../DataGrid/Columns/ColumnBase.razor.cs | 3 ++ 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/Core/Components/DataGrid/Columns/ColumnBase.razor b/src/Core/Components/DataGrid/Columns/ColumnBase.razor index f77e151c8d..9e3d813df5 100644 --- a/src/Core/Components/DataGrid/Columns/ColumnBase.razor +++ b/src/Core/Components/DataGrid/Columns/ColumnBase.razor @@ -7,6 +7,18 @@ } @code { + private void RenderDefaultHeaderTitle(RenderTreeBuilder __builder) + { + @if (HeaderCellTitleTemplate is not null) + { + @HeaderCellTitleTemplate(this) + } + else + { + @Title + } + } + private void RenderDefaultHeaderContent(RenderTreeBuilder __builder) { @if (HeaderCellItemTemplate is not null) @@ -23,14 +35,7 @@ {
- @if (HeaderCellTitleTemplate is not null) - { - @HeaderCellTitleTemplate(this) - } - else - { - @Title - } + @HeaderTitleContent
@if (Grid.SortByAscending.HasValue && IsActiveSortColumn) @@ -54,14 +59,7 @@ {
- @if (HeaderCellTitleTemplate is not null) - { - @HeaderCellTitleTemplate(this) - } - else - { - @Title - } + @HeaderTitleContent
} @@ -141,14 +139,7 @@
- @if (HeaderCellTitleTemplate is not null) - { - @HeaderCellTitleTemplate(this) - } - else - { - @Title - } + @HeaderTitleContent
@if (Grid.SortByAscending.HasValue && IsActiveSortColumn) @@ -173,7 +164,7 @@ {
- @Title + @HeaderTitleContent @if (ColumnOptions is not null && Filtered.GetValueOrDefault() && Grid.ResizeType.HasValue) { diff --git a/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs b/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs index 4f634c117e..f6c151c5e0 100644 --- a/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs +++ b/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs @@ -228,6 +228,8 @@ protected internal virtual Task OnCellKeyDownAsync(FluentDataGridCell ///
protected internal RenderFragment HeaderContent { get; protected set; } + protected internal RenderFragment HeaderTitleContent { get; protected set; } + /// /// Gets a value indicating whether this column should act as sortable if no value was set for the /// parameter. The default behavior is not to be @@ -254,6 +256,7 @@ protected void HandleKeyDown(FluentKeyCodeEventArgs e) public ColumnBase() { HeaderContent = RenderDefaultHeaderContent; + HeaderTitleContent = RenderDefaultHeaderTitle; } private async Task HandleColumnHeaderClickedAsync() From 4acc061f139e6fd42721b886134b9e0a3201b604 Mon Sep 17 00:00:00 2001 From: Phillip Hoff Date: Mon, 2 Jun 2025 15:36:32 -0700 Subject: [PATCH 3/3] Updated docs. --- src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs b/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs index f6c151c5e0..4e5060c87a 100644 --- a/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs +++ b/src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs @@ -228,6 +228,12 @@ protected internal virtual Task OnCellKeyDownAsync(FluentDataGridCell /// protected internal RenderFragment HeaderContent { get; protected set; } + /// + /// Gets or sets a that will be rendered for this column's header title. + /// This allows derived components to change the header title output. However, derived components are then + /// responsible for using within that new output if they want to continue + /// respecting that option. + /// protected internal RenderFragment HeaderTitleContent { get; protected set; } ///