Skip to content

Commit

Permalink
add QuickSearch to DataGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Aug 2, 2024
1 parent c5df54a commit 36cb3d1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
19 changes: 19 additions & 0 deletions samples/Sample.Core/Pages/DataGrid/Examples/Basic.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
<button class="btn btn-secondary" @onclick="@(() => DataGrid.Pager.PageSize = 25)">Page Size 25</button>
<button class="btn btn-secondary" @onclick="ApplyFilter">Filter High Score</button>

<div class="d-flex justify-content-end p-1">
<div class="w-25">
<div class="input-group input-group-sm">
<span class="input-group-text bg-body border-end-0">
<i class="fas fa-search"></i>
</span>
<input type="search"
class="form-control border-start-0"
placeholder="Search"
@bind-value="SearchText.Value"
@bind-value:event="oninput" />
</div>
</div>
</div>



<DataGrid Data="People"
TItem="Person"
Expand Down Expand Up @@ -52,8 +68,11 @@

private DataGrid<Person> DataGrid { get; set; }

protected DebounceValue<string> SearchText { get; set; }

protected override void OnInitialized()
{
SearchText = new DebounceValue<string>((value) => InvokeAsync(() => DataGrid.QuickSearch(value)));
People = Data.GeneratePeople(200).ToList();
}

Expand Down
29 changes: 29 additions & 0 deletions src/LoreSoft.Blazor.Controls/Data/DataGrid.razor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using LoreSoft.Blazor.Controls.Extensions;
using LoreSoft.Blazor.Controls.Utilities;

Expand Down Expand Up @@ -88,6 +90,32 @@ protected async Task ApplyFilters()
await RefreshAsync(true);
}


public async Task QuickSearch(string searchText)
{

RootQuery.Filters.Clear();

if (!string.IsNullOrWhiteSpace(searchText))
{
RootQuery.Logic = QueryLogic.Or;

// all filterable string columns
foreach (var column in Columns.Where(c => c.Filterable && c.Type == typeof(string)))
{
var filter = new QueryFilter
{
Field = column.Name,
Operator = QueryOperators.Contains,
Value = searchText
};
RootQuery.Filters.Add(filter);
}
}

await RefreshAsync(true);
}

public async Task ClearFilters()
{
RootQuery.Filters.Clear();
Expand All @@ -104,6 +132,7 @@ public async Task ApplyFilters(IEnumerable<QueryRule> rules, bool replace = fals
await RefreshAsync(true);
}


public override async Task RefreshAsync(bool resetPager = false)
{
// clear row flags on refresh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private static void WriteStringFilter(StringBuilder builder, List<object?> param
.Append(method)
.Append("(@")
.Append(index)
.Append(", StringComparison.OrdinalIgnoreCase")
.Append(')');

parameters.Add(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void FilterContains()
builder.Build(queryFilter);

builder.Expression.Should().NotBeEmpty();
builder.Expression.Should().Be("Name.Contains(@0)");
builder.Expression.Should().Be("Name.Contains(@0, StringComparison.OrdinalIgnoreCase)");

builder.Parameters.Count.Should().Be(1);
builder.Parameters[0].Should().Be("berry");
Expand Down Expand Up @@ -266,7 +266,7 @@ public void FilterNotContains()
builder.Build(queryFilter);

builder.Expression.Should().NotBeEmpty();
builder.Expression.Should().Be("!Name.Contains(@0)");
builder.Expression.Should().Be("!Name.Contains(@0, StringComparison.OrdinalIgnoreCase)");

builder.Parameters.Count.Should().Be(1);
builder.Parameters[0].Should().Be("berry");
Expand Down Expand Up @@ -295,7 +295,7 @@ public void FilterStartsWith()
builder.Build(queryFilter);

builder.Expression.Should().NotBeEmpty();
builder.Expression.Should().Be("Name.StartsWith(@0)");
builder.Expression.Should().Be("Name.StartsWith(@0, StringComparison.OrdinalIgnoreCase)");

builder.Parameters.Count.Should().Be(1);
builder.Parameters[0].Should().Be("P");
Expand Down Expand Up @@ -324,7 +324,7 @@ public void FilterNotStartsWith()
builder.Build(queryFilter);

builder.Expression.Should().NotBeEmpty();
builder.Expression.Should().Be("!Name.StartsWith(@0)");
builder.Expression.Should().Be("!Name.StartsWith(@0, StringComparison.OrdinalIgnoreCase)");

builder.Parameters.Count.Should().Be(1);
builder.Parameters[0].Should().Be("P");
Expand Down Expand Up @@ -352,7 +352,7 @@ public void FilterEndsWith()
builder.Build(queryFilter);

builder.Expression.Should().NotBeEmpty();
builder.Expression.Should().Be("Name.EndsWith(@0)");
builder.Expression.Should().Be("Name.EndsWith(@0, StringComparison.OrdinalIgnoreCase)");

builder.Parameters.Count.Should().Be(1);
builder.Parameters[0].Should().Be("berry");
Expand Down Expand Up @@ -380,7 +380,7 @@ public void FilterNotEndsWith()
builder.Build(queryFilter);

builder.Expression.Should().NotBeEmpty();
builder.Expression.Should().Be("!Name.EndsWith(@0)");
builder.Expression.Should().Be("!Name.EndsWith(@0, StringComparison.OrdinalIgnoreCase)");

builder.Parameters.Count.Should().Be(1);
builder.Parameters[0].Should().Be("berry");
Expand Down

0 comments on commit 36cb3d1

Please sign in to comment.