Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend GridSort possibilities #1860

Merged
merged 3 commits into from
Apr 17, 2024
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 @@ -1241,6 +1241,16 @@
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ByDescending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}},System.Collections.Generic.IComparer{``0})">
<summary>
Produces a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance that sorts according to the specified <paramref name="expression"/>
using the specified <paramref name="comparer"/>, descending.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenAscending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
Expand All @@ -1249,6 +1259,15 @@
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenAscending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}},System.Collections.Generic.IComparer{``0})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenDescending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
Expand All @@ -1257,6 +1276,49 @@
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenDescending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}},System.Collections.Generic.IComparer{``0})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenAlwaysAscending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenAlwaysAscending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}},System.Collections.Generic.IComparer{``0})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenAlwaysDescending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="M:Microsoft.FluentUI.AspNetCore.Components.GridSort`1.ThenAlwaysDescending``1(System.Linq.Expressions.Expression{System.Func{`0,``0}},System.Collections.Generic.IComparer{``0})">
<summary>
Updates a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance by appending a further sorting rule.
</summary>
<typeparam name="U">The type of the expression's value.</typeparam>
<param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
<param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
<returns>A <see cref="T:Microsoft.FluentUI.AspNetCore.Components.GridSort`1"/> instance representing the specified sorting rule.</returns>
</member>
<member name="T:Microsoft.FluentUI.AspNetCore.Components.PropertyColumn`2">
<summary>
Represents a <see cref="T:Microsoft.FluentUI.AspNetCore.Components.FluentDataGrid`1"/> column whose cells display a single value.
Expand Down
8 changes: 8 additions & 0 deletions examples/Demo/Shared/Pages/DataGrid/DataGridPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
</Description>
</DemoSection>

<DemoSection Title="SortBy Rank" Component="@typeof(DataGridRankSort)">
<Description>
<p>
Here is an example that demonstrates the rank sort.
</p>
</Description>
</DemoSection>


<DemoSection Title="Remote data" Component="@typeof(DataGridRemoteData)">
<Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<FluentDataGrid Items="@_gridData" ResizableColumns=true GridTemplateColumns="0.5fr 0.5fr">
<PropertyColumn Sortable="true" Property="x => x.Number" Title="Number" />

<TemplateColumn Sortable="true" SortBy="groupRank" Title="Group">
@context.Group
</TemplateColumn>

</FluentDataGrid>

<p>Keep numbers always sorted ascending inside the group when sorting by group</p>
<FluentDataGrid Items="@_gridData" ResizableColumns=true GridTemplateColumns="0.5fr 0.5fr">
<PropertyColumn Sortable="true" Property="x => x.Number" Title="Number" />

<TemplateColumn Sortable="true" SortBy="groupRankNumberAlwaysAscending" Title="Group">
@context.Group
</TemplateColumn>

</FluentDataGrid>



@code {
GridSort<GridRow> groupRank = GridSort<GridRow>
.ByAscending(x => x.Group)
.ThenAscending(x => x.Number);

GridSort<GridRow> groupRankNumberAlwaysAscending = GridSort<GridRow>
.ByAscending(x => x.Group)
.ThenAlwaysAscending(x => x.Number);

private static readonly IQueryable<GridRow> _gridData = new GridRow[] {
new(2, "B"),
new(1, "A"),
new(4, "B"),
new(3, "A")
}.AsQueryable();

public class GridRow(int number, string group)
{
public int Number { get; } = number;
public string Group { get; } = group;
}
}
121 changes: 112 additions & 9 deletions src/Core/Components/DataGrid/Columns/GridSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public static GridSort<TGridItem> ByDescending<U>(Expression<Func<TGridItem, U>>
=> new((queryable, asc) => asc ? queryable.OrderByDescending(expression) : queryable.OrderBy(expression),
(expression, false));

/// <summary>
/// Produces a <see cref="GridSort{T}"/> instance that sorts according to the specified <paramref name="expression"/>
/// using the specified <paramref name="comparer"/>, descending.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public static GridSort<TGridItem> ByDescending<U>(Expression<Func<TGridItem, U>> expression, IComparer<U> comparer)
=> new((queryable, asc) => asc ? queryable.OrderByDescending(expression, comparer) : queryable.OrderBy(expression, comparer),
(expression, false));

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
Expand All @@ -67,13 +79,25 @@ public static GridSort<TGridItem> ByDescending<U>(Expression<Func<TGridItem, U>>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAscending<U>(Expression<Func<TGridItem, U>> expression)
{
_then ??= [];
_thenExpressions ??= [];
_then.Add((queryable, asc) => asc ? queryable.ThenBy(expression) : queryable.ThenByDescending(expression));
_thenExpressions.Add((expression, true));
_cachedPropertyListAscending = null;
_cachedPropertyListDescending = null;
return this;
return AddThenExpression(
(queryable, asc) => asc ? queryable.ThenBy(expression) : queryable.ThenByDescending(expression),
(expression, true)
);
}

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAscending<U>(Expression<Func<TGridItem, U>> expression, IComparer<U> comparer)
{
return AddThenExpression(
(queryable, asc) => asc ? queryable.ThenBy(expression, comparer) : queryable.ThenByDescending(expression, comparer),
(expression, true)
);
}

/// <summary>
Expand All @@ -83,13 +107,92 @@ public GridSort<TGridItem> ThenAscending<U>(Expression<Func<TGridItem, U>> expre
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenDescending<U>(Expression<Func<TGridItem, U>> expression)
{
return AddThenExpression(
(queryable, asc) => asc ? queryable.ThenByDescending(expression) : queryable.ThenBy(expression),
(expression, false));
}

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenDescending<U>(Expression<Func<TGridItem, U>> expression, IComparer<U> comparer)
{
return AddThenExpression(
(queryable, asc) => asc ? queryable.ThenByDescending(expression, comparer) : queryable.ThenBy(expression, comparer),
(expression, false)
);
}

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAlwaysAscending<U>(Expression<Func<TGridItem, U>> expression)
{
return AddThenExpression(
(queryable, _) => queryable.ThenBy(expression),
(expression, true));
}

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAlwaysAscending<U>(Expression<Func<TGridItem, U>> expression, IComparer<U> comparer)
{
return AddThenExpression(
(queryable, _) => queryable.ThenBy(expression, comparer),
(expression, true)
);
}

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAlwaysDescending<U>(Expression<Func<TGridItem, U>> expression)
{
return AddThenExpression(
(queryable, _) => queryable.ThenByDescending(expression),
(expression, false));
}

/// <summary>
/// Updates a <see cref="GridSort{T}"/> instance by appending a further sorting rule.
/// </summary>
/// <typeparam name="U">The type of the expression's value.</typeparam>
/// <param name="expression">An expression defining how a set of <typeparamref name="TGridItem"/> instances are to be sorted.</param>
/// <param name="comparer">Defines how a items in a set of <typeparamref name="TGridItem"/> instances are to be compared.</param>
/// <returns>A <see cref="GridSort{T}"/> instance representing the specified sorting rule.</returns>
public GridSort<TGridItem> ThenAlwaysDescending<U>(Expression<Func<TGridItem, U>> expression, IComparer<U> comparer)
{
return AddThenExpression(
(queryable, _) => queryable.ThenByDescending(expression, comparer),
(expression, false)
);
}

private GridSort<TGridItem> AddThenExpression(Func<IOrderedQueryable<TGridItem>, bool, IOrderedQueryable<TGridItem>> thenSortExpression, (LambdaExpression, bool) thenExpression)
{
_then ??= [];
_thenExpressions ??= [];
_then.Add((queryable, asc) => asc ? queryable.ThenByDescending(expression) : queryable.ThenBy(expression));
_thenExpressions.Add((expression, false));
_then.Add(thenSortExpression);
_thenExpressions.Add(thenExpression);
_cachedPropertyListAscending = null;
_cachedPropertyListDescending = null;

return this;
}

Expand Down
Loading
Loading