Skip to content

Commit

Permalink
add FilterTemplate to data grid
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Jun 10, 2024
1 parent 37aba17 commit 151dba3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ValueTemplate Context="filter">
<select value="@Binding.Format(filter.Value)"
@onchange="e => filter.Value = Binding.Convert<string>(e.Value)">
<option>- Select Bank -</option>
<option value="">- Select Bank -</option>
<option value="Chase Bank">Chase Bank</option>
<option value="Bank of America">Bank of America</option>
<option value="Wells Fargo">Wells Fargo</option>
Expand Down
3 changes: 3 additions & 0 deletions src/LoreSoft.Blazor.Controls/Data/DataColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public class DataColumn<TItem> : ComponentBase
[Parameter]
public RenderFragment FooterTemplate { get; set; }

[Parameter]
public RenderFragment<QueryFilter> FilterTemplate { get; set; }


internal int CurrentSortIndex { get; set; } = -1;

Expand Down
4 changes: 3 additions & 1 deletion src/LoreSoft.Blazor.Controls/Data/DataGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@
<QueryFields>
@foreach (var column in Columns.Where(c => c.Filterable))
{
<QueryBuilderField Field="column.Property" Title="@column.HeaderTitle()" />
<QueryBuilderField Field="column.Property"
Title="@column.HeaderTitle()"
ValueTemplate="@column.FilterTemplate" />
}
</QueryFields>
</QueryBuilder>
Expand Down
23 changes: 21 additions & 2 deletions src/LoreSoft.Blazor.Controls/Extensions/QueryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#nullable enable

using LoreSoft.Blazor.Controls.Utilities;

using System.Linq.Dynamic.Core;
using System.Text;

using LoreSoft.Blazor.Controls.Utilities;

namespace LoreSoft.Blazor.Controls.Extensions;

public static class QueryExtensions
Expand Down Expand Up @@ -62,4 +62,23 @@ public static IQueryable<T> Filter<T>(this IQueryable<T> query, QueryRule filter

return query.Where(predicate, parameters);
}

public static DataResult<T> DataQuery<T>(this IQueryable<T> query, DataRequest request)
{
var filterQuery = query.Filter(request.Query);

var total = filterQuery.Count();

if (total == 0)
return new DataResult<T>(total, []);

var sortedQuery = filterQuery.Sort(request.Sorts);

if (request.Page > 0 && request.PageSize > 0)
sortedQuery = sortedQuery.Page(request.Page, request.PageSize);

var results = sortedQuery.ToList();

return new DataResult<T>(total, results);
}
}

0 comments on commit 151dba3

Please sign in to comment.