Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,11 @@
Gets or sets a callback when a row is double-clicked.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.OnSortChanged">
<summary>
Event callback for when the grid's sort order changes.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1.RowClass">
<summary>
Optionally defines a class to be applied to a rendered row.
Expand Down Expand Up @@ -15181,6 +15186,22 @@
Step Status
</summary>
</member>
<member name="T:Microsoft.FluentUI.AspNetCore.Components.DataGridSortEventArgs`1">
<summary>
Supplies information about a sort change event.
</summary>
<typeparam name="TGridItem">The type of data represented by each row in the grid.</typeparam>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.DataGridSortEventArgs`1.Column">
<summary>
Gets the column that defines the sort order.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.DataGridSortEventArgs`1.SortByAscending">
<summary>
Gets a value indicating whether the grid is sorted ascending.
</summary>
</member>
<member name="T:Microsoft.FluentUI.AspNetCore.Components.NavMenuActionArgs">
<summary>
An <see cref="T:System.EventArgs"/> class that is used as arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<TemplateColumn Title="Rank" Sortable="true" SortBy="@rankSort" Align="Align.Center">
Expand Down Expand Up @@ -124,6 +125,11 @@
DemoLogger.WriteLine($"[Custom comparer] Row focused: {row.Item?.Name}");
}

private void HandleSortChanged(DataGridSortEventArgs<Country> args)
{
DemoLogger.WriteLine($"[Custom comparer] Sort changed: {args.Column?.Title} columns sorted {(args.SortByAscending ? "ascending" : "descending")}");
}

public class StringLengthComparer : IComparer<string>
{
public static readonly StringLengthComparer Instance = new StringLengthComparer();
Expand Down
40 changes: 32 additions & 8 deletions src/Core/Components/DataGrid/FluentDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ public partial class FluentDataGrid<TGridItem> : FluentComponentBase, IHandleEve
[Parameter]
public EventCallback<FluentDataGridRow<TGridItem>> OnRowDoubleClick { get; set; }

/// <summary>
/// Event callback for when the grid's sort order changes.
/// </summary>
[Parameter]
public EventCallback<DataGridSortEventArgs<TGridItem>> OnSortChanged { get; set; }

/// <summary>
/// Optionally defines a class to be applied to a rendered row.
/// </summary>
Expand Down Expand Up @@ -372,7 +378,7 @@ public partial class FluentDataGrid<TGridItem> : 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);
Comment thread
vnbaaij marked this conversation as resolved.

private ElementReference? _gridReference;
//private DotNetObjectReference<Type>? _dotNetObjectReference;
Expand Down Expand Up @@ -632,7 +638,7 @@ private void FinishCollectingColumns()
/// <param name="column">The column that defines the new sort order.</param>
/// <param name="direction">The direction of sorting. If the value is <see cref="SortDirection.Auto"/>, then it will toggle the direction on each call.</param>
/// <returns>A <see cref="Task"/> representing the completion of the operation.</returns>
public Task SortByColumnAsync(ColumnBase<TGridItem> column, SortDirection direction = SortDirection.Auto)
public async Task SortByColumnAsync(ColumnBase<TGridItem> column, SortDirection direction = SortDirection.Auto)
{
_sortByAscending = direction switch
{
Expand All @@ -644,8 +650,17 @@ public Task SortByColumnAsync(ColumnBase<TGridItem> 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();
}

/// <summary>
Expand Down Expand Up @@ -677,17 +692,26 @@ public Task SortByColumnAsync(int index, SortDirection direction = SortDirection
/// </summary>
/// <param name="column">The column to check against the current sorted on column.</param>
/// <returns>A <see cref="Task"/> representing the completion of the operation.</returns>
public Task RemoveSortByColumnAsync(ColumnBase<TGridItem> column)
public async Task RemoveSortByColumnAsync(ColumnBase<TGridItem> 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;
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions src/Core/Events/DataGridSortEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ------------------------------------------------------------------------
// This file is licensed to you under the MIT License.
// ------------------------------------------------------------------------

namespace Microsoft.FluentUI.AspNetCore.Components;

/// <summary>
/// Supplies information about a sort change event.
/// </summary>
/// <typeparam name="TGridItem">The type of data represented by each row in the grid.</typeparam>
public class DataGridSortEventArgs<TGridItem> : EventArgs
{
/// <summary>
/// Gets the column that defines the sort order.
/// </summary>
public ColumnBase<TGridItem>? Column { get; init; }

/// <summary>
/// Gets a value indicating whether the grid is sorted ascending.
/// </summary>
public bool SortByAscending { get; init; }
}
Loading