Skip to content

Commit

Permalink
datasets: error handling - missing sorting capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Apr 27, 2024
1 parent ad878e7 commit 1295be1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public virtual IQueryable<T> ApplyToQueryable<T>(IQueryable<T> queryable)

public virtual void SetSortExpression(string? sortExpression)
{
if (sortExpression is {} && !IsSortingAllowed(sortExpression))
throw new ArgumentException($"Sorting by column '{sortExpression}' is not allowed.");

if (sortExpression == null)
{
Criteria.Clear();
Expand Down
3 changes: 3 additions & 0 deletions src/Framework/Core/Controls/Options/SortingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class SortingOptions : ISortingOptions, ISortingStateCapability, ISorting
/// </summary>
public virtual void SetSortExpression(string? sortExpression)
{
if (sortExpression is {} && !IsSortingAllowed(sortExpression))
throw new ArgumentException($"Sorting by column '{sortExpression}' is not allowed.");

if (sortExpression == null)
{
SortExpression = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Framework/Controls/GridView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ protected virtual void CreateHeaderRow(IDotvvmRequestContext context)
SetCellAttributes(column, cell, true);
var decoratedCell = Decorator.ApplyDecorators(cell, column.HeaderCellDecorators);
headerRow.Children.Add(decoratedCell);

column.CreateHeaderControls(context, this, gridViewCommands, sortCommandBindingOverride, cell, gridViewDataSet);
if (FilterPlacement == GridViewFilterPlacement.HeaderRow)
{
Expand Down
11 changes: 8 additions & 3 deletions src/Framework/Framework/Controls/GridViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ public virtual void CreateHeaderControls(IDotvvmRequestContext context, GridView

var sortExpression = GetSortExpression();

if (string.IsNullOrEmpty(sortExpression))
throw new DotvvmControlException(this, "The SortExpression property must be set when AllowSorting is true!");

if ((gridViewDataSet?.SortingOptions as ISortingSetSortExpressionCapability)?.IsSortingAllowed(sortExpression) == false)
throw new DotvvmControlException(this, $"The sort expression '{sortExpression}' is not allowed in the sorting options!");

var linkButton = new LinkButton();
linkButton.SetValue(ButtonBase.TextProperty, GetValueRaw(HeaderTextProperty));
linkButton.ClickArguments = new object?[] { sortExpression };
Expand Down Expand Up @@ -236,10 +242,9 @@ public virtual void CreateFilterControls(IDotvvmRequestContext context, GridView

private void SetSortedCssClass(HtmlGenericControl cell, ISortableGridViewDataSet? gridViewDataSet, GridViewCommands gridViewCommands)
{
if (gridViewDataSet is ISortableGridViewDataSet<ISortingStateCapability> sortableGridViewDataSet)
if (gridViewDataSet is ISortableGridViewDataSet<ISortingStateCapability> sortableGridViewDataSet &&
GetSortExpression() is {} sortExpression)
{
var sortExpression = GetSortExpression();

var cellAttributes = cell.Attributes;
if (!RenderOnServer)
{
Expand Down

0 comments on commit 1295be1

Please sign in to comment.