Skip to content

Commit

Permalink
Add splitted words search
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnavar committed Feb 2, 2022
1 parent 5b44e32 commit 9545a55
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 22 deletions.
2 changes: 2 additions & 0 deletions GridBlazor/CGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ private CGrid(HttpClient httpClient, string url,

public bool SearchingHiddenColumns { get; set; }

public bool SearchingSplittedWords { get; set; }

public bool ExtSortingEnabled { get; set; }

public bool HiddenExtSortingHeader { get; set; } = false;
Expand Down
6 changes: 6 additions & 0 deletions GridBlazor/Client/GridClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,16 @@ public IGridClient<T> Searchable(bool enable, bool onlyTextColumns)
}

public IGridClient<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns)
{
return Searchable(enable, onlyTextColumns, false, false);
}

public IGridClient<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns, bool splittedWords)
{
_source.SearchingEnabled = enable;
_source.SearchingOnlyTextColumns = onlyTextColumns;
_source.SearchingHiddenColumns = hiddenColumns;
_source.SearchingSplittedWords = splittedWords;
return this;
}

Expand Down
5 changes: 5 additions & 0 deletions GridBlazor/Client/IGridClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public interface IGridClient<T>
/// </summary>
IGridClient<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns);

/// <summary>
/// Enable or disable searching for all columns
/// </summary>
IGridClient<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns, bool splittedWords);

/// <summary>
/// Enable extended sorting
/// </summary>
Expand Down
21 changes: 19 additions & 2 deletions GridBlazor/Filtering/FilterGridODataProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,25 @@ public string Process()
if (!gridColumn.Filter.IsTextColumn()) continue;

List<ColumnFilterValue> options = new List<ColumnFilterValue>();
var columnFilterValue = new ColumnFilterValue(column.Name, GridFilterType.Contains, _searchSettings.SearchValue);
options.Add(columnFilterValue);
if (_grid.SearchingSplittedWords)
{
var searchWords = _searchSettings.SearchValue.Split(' ');
foreach (var searchWord in searchWords)
{
var columnFilterValue = new ColumnFilterValue(column.Name, GridFilterType.Contains, searchWord);
options.Add(columnFilterValue);
}
if (searchWords.Length > 1)
{
var columnFilterCondition = new ColumnFilterValue(column.Name, GridFilterType.Condition, GridFilterCondition.Or.ToString());
options.Add(columnFilterCondition);
}
}
else
{
var columnFilterValue = new ColumnFilterValue(column.Name, GridFilterType.Contains, _searchSettings.SearchValue);
options.Add(columnFilterValue);
}

var filter = gridColumn.Filter.GetFilter(options);
if (!string.IsNullOrWhiteSpace(filter))
Expand Down
2 changes: 2 additions & 0 deletions GridCore/SGridCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public IGridColumnCollection<T> Columns

public bool SearchingHiddenColumns { get; set; }

public bool SearchingSplittedWords { get; set; }

public bool ExtSortingEnabled { get; set; }

public bool HiddenExtSortingHeader { get; set; } = false;
Expand Down
51 changes: 33 additions & 18 deletions GridCore/Searching/SearchGridItemsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,16 @@ public IQueryable<T> Process(IQueryable<T> items)
ParameterExpression parameter = Expression.Parameter(typeof(T), "x");
Expression binaryExpression = null;

foreach (IGridColumn column in _grid.Columns)
if (_grid.SearchingSplittedWords)
{
IGridColumn<T> gridColumn = column as IGridColumn<T>;
if (gridColumn == null) continue;
if (gridColumn.Search == null) continue;
if (!_grid.SearchingHiddenColumns && gridColumn.Hidden) continue;

if (binaryExpression == null)
{
binaryExpression = gridColumn.Search.GetExpression(_settings.SearchValue,
_grid.SearchingOnlyTextColumns, parameter, _grid.RemoveDiacritics);
}
else
var searchWords = _settings.SearchValue.Split(' ');
foreach (var searchWord in searchWords)
{
Expression expression = gridColumn.Search.GetExpression(_settings.SearchValue,
_grid.SearchingOnlyTextColumns, parameter, _grid.RemoveDiacritics);
if (expression != null)
{
binaryExpression = Expression.OrElse(binaryExpression, expression);
}
binaryExpression = GetExpression(_grid, binaryExpression, parameter, searchWord);
}
}
else
binaryExpression = GetExpression(_grid, binaryExpression, parameter, _settings.SearchValue);

// apply extension to items
if (binaryExpression != null)
Expand All @@ -71,6 +59,33 @@ public IQueryable<T> Process(IQueryable<T> items)
return items;
}

private Expression GetExpression(ISGrid grid, Expression binaryExpression, ParameterExpression parameter, string searchValue)
{
foreach (IGridColumn column in grid.Columns)
{
IGridColumn<T> gridColumn = column as IGridColumn<T>;
if (gridColumn == null) continue;
if (gridColumn.Search == null) continue;
if (!grid.SearchingHiddenColumns && gridColumn.Hidden) continue;

if (binaryExpression == null)
{
binaryExpression = gridColumn.Search.GetExpression(searchValue,
grid.SearchingOnlyTextColumns, parameter, grid.RemoveDiacritics);
}
else
{
Expression expression = gridColumn.Search.GetExpression(searchValue,
grid.SearchingOnlyTextColumns, parameter, grid.RemoveDiacritics);
if (expression != null)
{
binaryExpression = Expression.OrElse(binaryExpression, expression);
}
}
}
return binaryExpression;
}

#endregion
}
}
6 changes: 6 additions & 0 deletions GridCore/Server/GridCoreServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,16 @@ public IGridServer<T> Searchable(bool enable, bool onlyTextColumns)
}

public IGridServer<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns)
{
return Searchable(enable, onlyTextColumns, false, false);
}

public IGridServer<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns, bool splittedWords)
{
_source.SearchingEnabled = enable;
_source.SearchingOnlyTextColumns = onlyTextColumns;
_source.SearchingHiddenColumns = hiddenColumns;
_source.SearchingSplittedWords = splittedWords;
return this;
}

Expand Down
5 changes: 5 additions & 0 deletions GridCore/Server/IGridServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public interface IGridServer<T>
/// </summary>
IGridServer<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns);

/// <summary>
/// Enable or disable searching for all columns
/// </summary>
IGridServer<T> Searchable(bool enable, bool onlyTextColumns, bool hiddenColumns, bool splittedWords);

/// <summary>
/// Enable extended sorting
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions GridMvc/Html/HtmlGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public bool SearchingHiddenColumns
set { _source.SearchingHiddenColumns = value; }
}

public bool SearchingSplittedWords
{
get { return _source.SearchingSplittedWords; }
set { _source.SearchingSplittedWords = value; }
}

public bool ExtSortingEnabled
{
get { return _source.ExtSortingEnabled; }
Expand Down
10 changes: 8 additions & 2 deletions GridShared/IGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,21 @@ public interface IGrid
bool ClearFiltersButtonEnabled { get; set; }

/// <summary>
/// Set or get value of searching for all columns or only text ones
/// Set or get value of searching all columns or only text ones
/// </summary>
bool SearchingOnlyTextColumns { get; set; }

/// <summary>
/// Set or get value of searching for all columns including hidden ones
/// Set or get value of searching all columns including hidden ones
/// </summary>
bool SearchingHiddenColumns { get; set; }

/// <summary>
/// Set or get value of searching all columns for splitted words
/// </summary>
bool SearchingSplittedWords { get; set; }


/// <summary>
/// Text in empty grid (no items for display)
/// </summary>
Expand Down

0 comments on commit 9545a55

Please sign in to comment.