Skip to content

Commit

Permalink
Fix initial sorting with OData back-end (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnavar committed Oct 20, 2020
1 parent 3ab70cf commit a7386ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
35 changes: 28 additions & 7 deletions GridBlazor/CGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ private CGrid(HttpClient httpClient, string url,
EmptyGridText = Strings.DefaultGridEmptyText;
Language = Strings.Lang;

_currentPagerODataProcessor = new PagerGridODataProcessor<T>(this);
_currentSortODataProcessor = new SortGridODataProcessor<T>(this, _settings.SortSettings);
_currentFilterODataProcessor = new FilterGridODataProcessor<T>(this, _settings.FilterSettings,
_settings.SearchSettings);
_currentSearchODataProcessor = new SearchGridODataProcessor<T>(this, _settings.SearchSettings);
_currentExpandODataProcessor = new ExpandGridODataProcessor<T>(this);

_annotations = new GridAnnotationsProvider();

//Set up column collection:
Expand All @@ -128,6 +121,14 @@ private CGrid(HttpClient httpClient, string url,
ComponentOptions = new GridOptions();

ApplyGridSettings();
SetInitSorting();

_currentPagerODataProcessor = new PagerGridODataProcessor<T>(this);
_currentSortODataProcessor = new SortGridODataProcessor<T>(this, _settings.SortSettings);
_currentFilterODataProcessor = new FilterGridODataProcessor<T>(this, _settings.FilterSettings,
_settings.SearchSettings);
_currentSearchODataProcessor = new SearchGridODataProcessor<T>(this, _settings.SearchSettings);
_currentExpandODataProcessor = new ExpandGridODataProcessor<T>(this);

Pager = new GridPager(query);

Expand Down Expand Up @@ -249,6 +250,7 @@ public IGridSettingsProvider Settings
private void UpdateQueryAndSettings()
{
_settings = new QueryStringGridSettingsProvider(_query);
SetInitSorting();
_columnsCollection.SortSettings = _settings.SortSettings;
_columnsCollection.UpdateColumnsSorting();
((GridPager)_pager).Query = _query;
Expand All @@ -258,6 +260,25 @@ private void UpdateQueryAndSettings()
_currentSearchODataProcessor.UpdateSettings(_settings.SearchSettings);
}

// keeps initial sorting on the client for OData grids
private void SetInitSorting()
{
string[] sortings = Query.Get(((QueryStringSortSettings)_settings.SortSettings).ColumnQueryParameterName).Count > 0 ?
Query.Get(((QueryStringSortSettings)_settings.SortSettings).ColumnQueryParameterName).ToArray() : null;

if ((_settings.SortSettings.SortValues == null || _settings.SortSettings.SortValues.Count == 0)
&& (sortings == null || sortings.Length == 0)
&& string.IsNullOrWhiteSpace(_settings.SortSettings.ColumnName))
{
var column = _columnsCollection.FirstOrDefault(r => ((ICGridColumn)r).InitialDirection.HasValue);
if (column != null)
{
_settings.SortSettings.ColumnName = column.Name;
_settings.SortSettings.Direction = ((ICGridColumn)column).InitialDirection.Value;
}
}
}

/// <summary>
/// Provides url used by the grid
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions GridBlazor/Columns/GridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public override IGridColumn<T> SortInitialDirection(GridSortDirection direction)
{
IsSorted = true;
Direction = direction;
InitialDirection = direction;

// added to enable initial sorting
_grid.Settings.SortSettings.ColumnName = Name;
_grid.Settings.SortSettings.Direction = direction;
}
return this;
}
Expand Down
2 changes: 2 additions & 0 deletions GridBlazor/Columns/GridColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public abstract class GridColumnBase<T> : GridStyledColumn, IGridColumn<T>, IExp
public bool IsSorted { get; set; }
public GridSortDirection? Direction { get; set; }

public GridSortDirection? InitialDirection { get; set; }

public bool Hidden { get; set; }

public CrudHidden CrudHidden { get; protected set; } = CrudHidden.NONE;
Expand Down
3 changes: 3 additions & 0 deletions GridBlazor/Columns/ICGridColumn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GridShared;
using GridShared.Sorting;
using GridShared.Utility;
using System;
using System.Threading.Tasks;
Expand All @@ -17,6 +18,8 @@ public interface ICGridColumn
/// </summary>
Func<object[], bool, bool, bool, bool, Task<IGrid>> SubGrids { get; }

GridSortDirection? InitialDirection { get; }

/// <summary>
/// Get foreign key values for subgrid records
/// </summary>
Expand Down

0 comments on commit a7386ea

Please sign in to comment.