Skip to content

Commit 0e9c6ac

Browse files
authored
Allow Specifying the interactive SortOrder Sequence on Data Grids (#2366)
1 parent 1b68816 commit 0e9c6ac

File tree

5 files changed

+78
-74
lines changed

5 files changed

+78
-74
lines changed

Radzen.Blazor/RadzenDataGrid.razor.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ internal async Task OnSort(EventArgs args, RadzenDataGridColumn<TItem> column)
10161016

10171017
var property = column.GetSortProperty();
10181018

1019-
SetColumnSortOrder(column);
1019+
SetNextColumnSortOrder(column);
10201020
await Sort.InvokeAsync(new DataGridColumnSortEventArgs<TItem>() { Column = column, SortOrder = column.GetSortOrder() });
10211021
SaveSettings();
10221022

@@ -3153,7 +3153,20 @@ internal bool IsOData()
31533153

31543154
internal List<SortDescriptor> sorts = new List<SortDescriptor>();
31553155

3156-
internal void SetColumnSortOrder(RadzenDataGridColumn<TItem> column)
3156+
internal void SetNextColumnSortOrder(RadzenDataGridColumn<TItem> column)
3157+
{
3158+
var sequence = column.SortOrderSequence;
3159+
3160+
var pos = Array.IndexOf(sequence, column.GetSortOrder());
3161+
3162+
var nextSortOrder = pos == -1 || pos + 1 >= sequence.Length
3163+
? sequence.FirstOrDefault()
3164+
: sequence[pos + 1];
3165+
3166+
SetColumnSortOrder(column, nextSortOrder);
3167+
}
3168+
3169+
private void SetColumnSortOrder(RadzenDataGridColumn<TItem> column, SortOrder? sortOrder)
31573170
{
31583171
if (!AllowMultiColumnSorting)
31593172
{
@@ -3165,35 +3178,33 @@ internal void SetColumnSortOrder(RadzenDataGridColumn<TItem> column)
31653178
}
31663179

31673180
var descriptor = sorts.Where(d => d.Property == column?.GetSortProperty()).FirstOrDefault();
3168-
if (descriptor == null)
3169-
{
3170-
descriptor = new SortDescriptor() { Property = column.GetSortProperty() };
3171-
}
31723181

3173-
if (column.GetSortOrder() == null)
3174-
{
3175-
column.SetSortOrderInternal(SortOrder.Ascending);
3176-
descriptor.SortOrder = SortOrder.Ascending;
3177-
}
3178-
else if (column.GetSortOrder() == SortOrder.Ascending)
3179-
{
3180-
column.SetSortOrderInternal(SortOrder.Descending);
3181-
descriptor.SortOrder = SortOrder.Descending;
3182-
}
3183-
else if (column.GetSortOrder() == SortOrder.Descending)
3182+
column.SetSortOrderInternal(sortOrder);
3183+
3184+
if (!sortOrder.HasValue)
31843185
{
3185-
column.SetSortOrderInternal(null);
3186-
if (sorts.Where(d => d.Property == column?.GetSortProperty()).Any())
3186+
if (descriptor != null)
31873187
{
31883188
sorts.Remove(descriptor);
31893189
}
3190-
descriptor = null;
3190+
3191+
return;
31913192
}
31923193

3193-
if (descriptor != null && !sorts.Where(d => d.Property == column?.GetSortProperty()).Any())
3194+
if (descriptor == null)
31943195
{
3196+
descriptor = new SortDescriptor()
3197+
{
3198+
Property = column.GetSortProperty(),
3199+
SortOrder = sortOrder,
3200+
};
3201+
31953202
sorts.Add(descriptor);
3203+
3204+
return;
31963205
}
3206+
3207+
descriptor.SortOrder = sortOrder;
31973208
}
31983209

31993210
void GroupsCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
@@ -3377,7 +3388,7 @@ public void OrderBy(string property)
33773388

33783389
if (column != null)
33793390
{
3380-
SetColumnSortOrder(column);
3391+
SetColumnSortOrder(column, SortOrder.Ascending);
33813392
Sort.InvokeAsync(new DataGridColumnSortEventArgs<TItem>() { Column = column, SortOrder = column.GetSortOrder() });
33823393
SaveSettings();
33833394
}
@@ -3403,9 +3414,7 @@ public void OrderByDescending(string property)
34033414

34043415
if (column != null)
34053416
{
3406-
column.SetSortOrderInternal(SortOrder.Ascending);
3407-
SetColumnSortOrder(column);
3408-
3417+
SetColumnSortOrder(column, SortOrder.Descending);
34093418
Sort.InvokeAsync(new DataGridColumnSortEventArgs<TItem>() { Column = column, SortOrder = column.GetSortOrder() });
34103419
SaveSettings();
34113420
}

Radzen.Blazor/RadzenDataGridColumn.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ internal void SetOrderIndex(int? value)
278278
[Parameter]
279279
public SortOrder? SortOrder { get; set; }
280280

281+
/// <summary>
282+
/// Gets or sets the sequence of the <see cref="Radzen.SortOrder"/>s to use when sorting the column interactively.
283+
/// </summary>
284+
[Parameter]
285+
public SortOrder?[] SortOrderSequence { get; set; } = [Radzen.SortOrder.Ascending, Radzen.SortOrder.Descending, null];
286+
281287
bool visible = true;
282288
/// <summary>
283289
/// Gets or sets a value indicating whether this <see cref="RadzenDataGridColumn{TItem}"/> is visible.

Radzen.Blazor/RadzenPivotDataGrid.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
<tr class="rz-pivot-header-row">
199199
<th class="rz-pivot-column-header @(isSortable ? "rz-sortable-column" : "")" style="width:100%;margin-top:5px;">
200200
<div class="rz-pivot-header-content-wrapper">
201-
<div @onclick="@(args => OnColumnSort(args, column))"
201+
<div @onclick="@(args => OnAggregateSort(args, column))"
202202
@onkeydown="@(isSortable ? OnSortKeyPressed : (Func<KeyboardEventArgs, Task>)null)"
203203
class="rz-pivot-header-content @(isSortable ? "rz-sortable" : "")"
204204
tabindex="@(isSortable ? "0" : null)"

Radzen.Blazor/RadzenPivotDataGrid.razor.cs

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,68 +1537,51 @@ private void PadRowHeaderCells(List<PivotBodyRow> rows)
15371537
}
15381538

15391539
/// <summary>
1540-
/// Handles column header click for sorting.
1540+
/// Handles aggregate click for sorting.
15411541
/// </summary>
1542-
internal async Task OnColumnSort(EventArgs args, RadzenPivotField<TItem> column)
1542+
internal async Task OnAggregateSort(EventArgs args, RadzenPivotAggregate<TItem> aggregate)
15431543
{
1544-
if (AllowSorting && column.Sortable)
1545-
{
1546-
// Toggle sort order: None -> Ascending -> Descending -> None
1547-
if (column.GetSortOrder() == null)
1548-
{
1549-
column.SetSortOrderInternal(SortOrder.Ascending);
1550-
}
1551-
else if (column.GetSortOrder() == SortOrder.Ascending)
1552-
{
1553-
column.SetSortOrderInternal(SortOrder.Descending);
1554-
}
1555-
else
1556-
{
1557-
column.SetSortOrderInternal(null);
1558-
}
1559-
1560-
// Clear other column sorts if single column sorting
1561-
if (column.GetSortOrder() != null)
1562-
{
1563-
foreach (var col in pivotColumns.Where(c => c != column))
1564-
{
1565-
col.SetSortOrderInternal(null);
1566-
}
1567-
}
1568-
1569-
await Reload();
1570-
}
1544+
await HandleFieldSort(pivotAggregates, aggregate);
1545+
}
15711546

1572-
await Task.CompletedTask;
1547+
/// <summary>
1548+
/// Handles column header click for sorting.
1549+
/// </summary>
1550+
internal async Task OnColumnSort(EventArgs args, RadzenPivotColumn<TItem> column)
1551+
{
1552+
await HandleFieldSort(pivotColumns, column);
15731553
}
15741554

15751555
/// <summary>
15761556
/// Handles row header click for sorting.
15771557
/// </summary>
15781558
internal async Task OnRowSort(EventArgs args, RadzenPivotRow<TItem> row)
15791559
{
1580-
if (AllowSorting && row.Sortable)
1560+
await HandleFieldSort(pivotRows, row);
1561+
}
1562+
1563+
private async Task HandleFieldSort<T>(List<T> allFields, T sortedField)
1564+
where T : RadzenPivotField<TItem>
1565+
{
1566+
if (AllowSorting && sortedField.Sortable)
15811567
{
1582-
// Toggle sort order: None -> Ascending -> Descending -> None
1583-
if (row.GetSortOrder() == null)
1584-
{
1585-
row.SetSortOrderInternal(SortOrder.Ascending);
1586-
}
1587-
else if (row.GetSortOrder() == SortOrder.Ascending)
1588-
{
1589-
row.SetSortOrderInternal(SortOrder.Descending);
1590-
}
1591-
else
1592-
{
1593-
row.SetSortOrderInternal(null);
1594-
}
1568+
// Toggle sort order
1569+
var sequence = sortedField.SortOrderSequence;
15951570

1596-
// Clear other row sorts if single row sorting
1597-
if (row.GetSortOrder() != null)
1571+
var pos = Array.IndexOf(sequence, sortedField.GetSortOrder());
1572+
1573+
var nextSortOrder = pos == -1 || pos + 1 >= sequence.Length
1574+
? sequence.FirstOrDefault()
1575+
: sequence[pos + 1];
1576+
1577+
sortedField.SetSortOrderInternal(nextSortOrder);
1578+
1579+
// Clear other column sorts if single column sorting
1580+
if (sortedField.GetSortOrder() != null)
15981581
{
1599-
foreach (var r in pivotRows.Where(r => r != row))
1582+
foreach (var col in allFields.Where(c => c != sortedField))
16001583
{
1601-
r.SetSortOrderInternal(null);
1584+
col.SetSortOrderInternal(null);
16021585
}
16031586
}
16041587

Radzen.Blazor/RadzenPivotField.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public partial class RadzenPivotField<TItem> : ComponentBase, IDisposable
3030
[Parameter]
3131
public SortOrder? SortOrder { get; set; }
3232

33+
/// <summary>
34+
/// Gets or sets the sequence of the <see cref="Radzen.SortOrder"/>s to use when sorting the column interactively.
35+
/// </summary>
36+
[Parameter]
37+
public SortOrder?[] SortOrderSequence { get; set; } = [Radzen.SortOrder.Ascending, Radzen.SortOrder.Descending, null];
38+
3339
/// <summary>
3440
/// Gets or sets a value indicating whether this column is sortable.
3541
/// </summary>

0 commit comments

Comments
 (0)