Files
radzen-blazor/Radzen.Blazor/Spreadsheet/MultiKeySortCommand.cs
Atanas Korchev cae0e7d3a1 Fix RadzenSpreadsheet edit refresh, table sort, filter and tool issues
- Edited value cells now repaint: fire the changed cell's notification even
  inside a command batch (only dependent recalc is deferred to EndUpdate).
- Table sort excludes the header (and totals) row by sorting DataBodyRange;
  the cell-menu and context-menu sorts are now table/auto-filter aware.
- Re-apply active filters after a sort (and on sort undo) so filtered-out
  rows stay hidden by value.
- Enable Merge, Borders, Conditional and Data Validation tools when a range
  is selected by subscribing to Selection.Changed.
- Keep the worksheet prefix on cross-sheet references in the formula-bar
  syntax highlighter (use the raw token text).

Adds regression tests for all of the above.
2026-06-17 18:08:12 +03:00

73 lines
2.0 KiB
C#

using System;
using Radzen.Documents.Spreadsheet;
namespace Radzen.Blazor.Spreadsheet;
#nullable enable
/// <summary>
/// Sort a range using one or more <see cref="SortKey"/> levels. Supports undo/redo by
/// snapshotting affected cells before sorting.
/// </summary>
public class MultiKeySortCommand : RangeSnapshotCommandBase
{
/// <inheritdoc/>
public override SheetAction RequiredAction => SheetAction.Sort;
/// <inheritdoc/>
public override SpreadsheetFeature? Feature => SpreadsheetFeature.Sorting;
private readonly RangeRef range;
private readonly SortKey[] keys;
private readonly bool skipHeaderRow;
/// <summary>
/// Initializes a new instance of the <see cref="MultiKeySortCommand"/> class.
/// </summary>
public MultiKeySortCommand(Worksheet sheet, RangeRef range, SortKey[] keys, bool skipHeaderRow = false)
: base(sheet)
{
this.range = range;
this.keys = keys ?? throw new ArgumentNullException(nameof(keys));
this.skipHeaderRow = skipHeaderRow;
}
/// <inheritdoc/>
protected override bool DoExecute()
{
if (range == RangeRef.Invalid || keys.Length == 0)
{
return false;
}
var startRow = range.Start.Row + (skipHeaderRow ? 1 : 0);
for (var row = startRow; row <= range.End.Row; row++)
{
for (var col = range.Start.Column; col <= range.End.Column; col++)
{
Capture(new CellRef(row, col));
}
}
var sortRange = new RangeRef(new CellRef(startRow, range.Start.Column), range.End);
sheet.Sort(sortRange, keys);
return true;
}
/// <inheritdoc/>
public override void Unexecute()
{
if (range == RangeRef.Invalid)
{
return;
}
RestoreSnapshot();
// RestoreSnapshot only restores cell content, not row visibility - re-apply filters by value.
sheet.ReapplyFilters();
}
}