diff --git a/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml b/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml
index 0e80cc1459..88d658e67c 100644
--- a/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml
+++ b/examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml
@@ -2130,6 +2130,11 @@
Gets or sets a callback when a row is double-clicked.
+
+
+ Event callback for when the grid's sort order changes.
+
+
Optionally defines a class to be applied to a rendered row.
@@ -15181,6 +15186,22 @@
Step Status
+
+
+ Supplies information about a sort change event.
+
+ The type of data represented by each row in the grid.
+
+
+
+ Gets the column that defines the sort order.
+
+
+
+
+ Gets a value indicating whether the grid is sorted ascending.
+
+
An class that is used as arguments
diff --git a/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomComparer.razor b/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomComparer.razor
index babb4ae65d..976b86c29c 100644
--- a/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomComparer.razor
+++ b/examples/Demo/Shared/Pages/DataGrid/Examples/DataGridCustomComparer.razor
@@ -36,6 +36,7 @@
Pagination="@pagination"
TGridItem="Country"
OnRowFocus="HandleRowFocus"
+ OnSortChanged="HandleSortChanged"
GridTemplateColumns="0.2fr 1fr 0.2fr 0.2fr 0.2fr 0.2fr"
ShowHover="true">
@@ -124,6 +125,11 @@
DemoLogger.WriteLine($"[Custom comparer] Row focused: {row.Item?.Name}");
}
+ private void HandleSortChanged(DataGridSortEventArgs args)
+ {
+ DemoLogger.WriteLine($"[Custom comparer] Sort changed: {args.Column?.Title} columns sorted {(args.SortByAscending ? "ascending" : "descending")}");
+ }
+
public class StringLengthComparer : IComparer
{
public static readonly StringLengthComparer Instance = new StringLengthComparer();
diff --git a/src/Core/Components/DataGrid/FluentDataGrid.razor.cs b/src/Core/Components/DataGrid/FluentDataGrid.razor.cs
index 4bb434e16c..04c17937a9 100644
--- a/src/Core/Components/DataGrid/FluentDataGrid.razor.cs
+++ b/src/Core/Components/DataGrid/FluentDataGrid.razor.cs
@@ -248,6 +248,12 @@ public partial class FluentDataGrid : FluentComponentBase, IHandleEve
[Parameter]
public EventCallback> OnRowDoubleClick { get; set; }
+ ///
+ /// Event callback for when the grid's sort order changes.
+ ///
+ [Parameter]
+ public EventCallback> OnSortChanged { get; set; }
+
///
/// Optionally defines a class to be applied to a rendered row.
///
@@ -372,7 +378,7 @@ public partial class FluentDataGrid : FluentComponentBase, IHandleEve
// Returns Loading if set (controlled). If not controlled,
// we assume the grid is loading until the next data load completes
- internal bool EffectiveLoadingValue => Loading ?? ItemsProvider is not null;
+ internal bool EffectiveLoadingValue => Loading ?? (ItemsProvider is not null);
private ElementReference? _gridReference;
//private DotNetObjectReference? _dotNetObjectReference;
@@ -632,7 +638,7 @@ private void FinishCollectingColumns()
/// The column that defines the new sort order.
/// The direction of sorting. If the value is , then it will toggle the direction on each call.
/// A representing the completion of the operation.
- public Task SortByColumnAsync(ColumnBase column, SortDirection direction = SortDirection.Auto)
+ public async Task SortByColumnAsync(ColumnBase column, SortDirection direction = SortDirection.Auto)
{
_sortByAscending = direction switch
{
@@ -644,8 +650,17 @@ public Task SortByColumnAsync(ColumnBase column, SortDirection direct
_sortByColumn = column;
- StateHasChanged(); // We want to see the updated sort order in the header, even before the data query is completed
- return RefreshDataAsync();
+ if (OnSortChanged.HasDelegate)
+ {
+ await OnSortChanged.InvokeAsync(new()
+ {
+ Column = _sortByColumn,
+ SortByAscending = _sortByAscending
+ });
+ }
+
+ _ = InvokeAsync(StateHasChanged); // We want to see the updated sort order in the header, even before the data query is completed
+ await RefreshDataAsync();
}
///
@@ -677,17 +692,26 @@ public Task SortByColumnAsync(int index, SortDirection direction = SortDirection
///
/// The column to check against the current sorted on column.
/// A representing the completion of the operation.
- public Task RemoveSortByColumnAsync(ColumnBase column)
+ public async Task RemoveSortByColumnAsync(ColumnBase column)
{
if (_sortByColumn == column && !column.IsDefaultSortColumn)
{
_sortByColumn = _internalGridContext.DefaultSortColumn.Column ?? null;
_sortByAscending = _internalGridContext.DefaultSortColumn.Direction != SortDirection.Descending;
- StateHasChanged(); // We want to see the updated sort order in the header, even before the data query is completed
- return RefreshDataCoreAsync();
+ if (OnSortChanged.HasDelegate)
+ {
+ await OnSortChanged.InvokeAsync(new()
+ {
+ Column = _sortByColumn,
+ SortByAscending = _sortByAscending
+ });
+ }
+
+ _ = InvokeAsync(StateHasChanged); // We want to see the updated sort order in the header, even before the data query is completed
+ await RefreshDataCoreAsync();
+ return;
}
- return Task.CompletedTask;
}
///
diff --git a/src/Core/Events/DataGridSortEventArgs.cs b/src/Core/Events/DataGridSortEventArgs.cs
new file mode 100644
index 0000000000..4bd2063b18
--- /dev/null
+++ b/src/Core/Events/DataGridSortEventArgs.cs
@@ -0,0 +1,22 @@
+// ------------------------------------------------------------------------
+// This file is licensed to you under the MIT License.
+// ------------------------------------------------------------------------
+
+namespace Microsoft.FluentUI.AspNetCore.Components;
+
+///
+/// Supplies information about a sort change event.
+///
+/// The type of data represented by each row in the grid.
+public class DataGridSortEventArgs : EventArgs
+{
+ ///
+ /// Gets the column that defines the sort order.
+ ///
+ public ColumnBase? Column { get; init; }
+
+ ///
+ /// Gets a value indicating whether the grid is sorted ascending.
+ ///
+ public bool SortByAscending { get; init; }
+}