From 4d95348d83897e338a5c1ede047c3c8491e73192 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Fri, 6 Mar 2020 20:02:25 -0800 Subject: [PATCH 1/9] Added redirect --- src/BlazorTable.Sample.Wasm/wwwroot/_redirects | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/BlazorTable.Sample.Wasm/wwwroot/_redirects diff --git a/src/BlazorTable.Sample.Wasm/wwwroot/_redirects b/src/BlazorTable.Sample.Wasm/wwwroot/_redirects new file mode 100644 index 00000000..50a46335 --- /dev/null +++ b/src/BlazorTable.Sample.Wasm/wwwroot/_redirects @@ -0,0 +1 @@ +/* /index.html 200 \ No newline at end of file From 3e7a9294420441455868de6366be7c1890b008ec Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Fri, 6 Mar 2020 22:42:22 -0800 Subject: [PATCH 2/9] update table only if filter exists --- src/BlazorTable/Components/FilterManager.razor.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/BlazorTable/Components/FilterManager.razor.cs b/src/BlazorTable/Components/FilterManager.razor.cs index e05df14f..f508deef 100644 --- a/src/BlazorTable/Components/FilterManager.razor.cs +++ b/src/BlazorTable/Components/FilterManager.razor.cs @@ -28,8 +28,11 @@ private void ApplyFilter() private void ClearFilter() { Column.ToggleFilter(); - Column.Filter = null; - Column.Table.Update(); + if (Column.Filter != null) + { + Column.Filter = null; + Column.Table.Update(); + } } } } From 8f9e26261f0b74c7a6a2cce79fce908cf39b1c1a Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sat, 7 Mar 2020 00:18:17 -0800 Subject: [PATCH 3/9] fixed Custom IFilters not working --- src/BlazorTable/Components/Column.razor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlazorTable/Components/Column.razor b/src/BlazorTable/Components/Column.razor index 7e101498..5ce141ad 100644 --- a/src/BlazorTable/Components/Column.razor +++ b/src/BlazorTable/Components/Column.razor @@ -14,7 +14,7 @@ @if (this.CustomIFilters != null) { - this.CustomIFilters(this); + @this.CustomIFilters(this); } From 40e5fb93daa6acf3b1f60858a2e701b5c37998ae Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sat, 7 Mar 2020 00:19:05 -0800 Subject: [PATCH 4/9] cleanup --- .../Components/EmptyDataTemplate.razor.cs | 1 - src/BlazorTable/Components/FilterManager.razor.cs | 14 ++++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/BlazorTable/Components/EmptyDataTemplate.razor.cs b/src/BlazorTable/Components/EmptyDataTemplate.razor.cs index 4fb4cd41..e01e57ec 100644 --- a/src/BlazorTable/Components/EmptyDataTemplate.razor.cs +++ b/src/BlazorTable/Components/EmptyDataTemplate.razor.cs @@ -5,7 +5,6 @@ namespace BlazorTable /// /// Child content for empty dataset /// - /// public partial class EmptyDataTemplate { /// diff --git a/src/BlazorTable/Components/FilterManager.razor.cs b/src/BlazorTable/Components/FilterManager.razor.cs index f508deef..46ce23ff 100644 --- a/src/BlazorTable/Components/FilterManager.razor.cs +++ b/src/BlazorTable/Components/FilterManager.razor.cs @@ -5,21 +5,26 @@ namespace BlazorTable { public partial class FilterManager { - [CascadingParameter(Name = "Column")] public IColumn Column { get; set; } + [CascadingParameter(Name = "Column")] + public IColumn Column { get; set; } - [Parameter] public RenderFragment ChildContent { get; set; } + [Parameter] + public RenderFragment ChildContent { get; set; } - [Inject] public ILogger> Logger { get; set; } + [Inject] + public ILogger> Logger { get; set; } private void ApplyFilter() { Column.ToggleFilter(); + if (Column.FilterControl != null) { Column.Filter = Column.FilterControl.GetFilter(); Column.Table.Update(); Column.Table.FirstPage(); - } else + } + else { Logger.LogInformation("Filter is null"); } @@ -28,6 +33,7 @@ private void ApplyFilter() private void ClearFilter() { Column.ToggleFilter(); + if (Column.Filter != null) { Column.Filter = null; From 1a9675f822fe6e24024018b34e50a256f642cc90 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sat, 7 Mar 2020 00:21:50 -0800 Subject: [PATCH 5/9] simplified condition --- src/BlazorTable/Filters/BooleanFilter.razor | 2 +- src/BlazorTable/Filters/DateFilter.razor | 2 +- src/BlazorTable/Filters/EnumFilter.razor | 4 ++-- src/BlazorTable/Filters/NumberFilter.razor | 2 +- src/BlazorTable/Filters/StringFilter.razor | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BlazorTable/Filters/BooleanFilter.razor b/src/BlazorTable/Filters/BooleanFilter.razor index d9ef07ae..bc287066 100644 --- a/src/BlazorTable/Filters/BooleanFilter.razor +++ b/src/BlazorTable/Filters/BooleanFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (FilterTypes.Contains(Column.Type.GetNonNullableType())) +@if (Column.FilterControl == this) { Condition = (BooleanCondition)Enum.Parse(typeof(BooleanCondition), x.Value.ToString()))"> @foreach (var option in (BooleanCondition[])Enum.GetValues(typeof(BooleanCondition))) diff --git a/src/BlazorTable/Filters/DateFilter.razor b/src/BlazorTable/Filters/DateFilter.razor index 83da9aa1..3ecd55aa 100644 --- a/src/BlazorTable/Filters/DateFilter.razor +++ b/src/BlazorTable/Filters/DateFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (Column.Type.GetNonNullableType() == typeof(DateTime)) +@if (Column.FilterControl == this) { Condition = (NumberCondition)Enum.Parse(typeof(NumberCondition), x.Value.ToString()))"> @foreach (var option in (NumberCondition[])Enum.GetValues(typeof(NumberCondition))) diff --git a/src/BlazorTable/Filters/EnumFilter.razor b/src/BlazorTable/Filters/EnumFilter.razor index 20dda537..791825dc 100644 --- a/src/BlazorTable/Filters/EnumFilter.razor +++ b/src/BlazorTable/Filters/EnumFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (Column.Type.GetNonNullableType().IsEnum) +@if (Column.FilterControl == this) { Condition = (EnumCondition)Enum.Parse(typeof(EnumCondition), x.Value.ToString()))"> @foreach (EnumCondition option in Enum.GetValues(typeof(EnumCondition))) @@ -9,7 +9,7 @@ @option.GetDescription() } - + @if (Condition != EnumCondition.IsNull && Condition != EnumCondition.IsNotNull) { FilterValue = Enum.Parse(Column.Type.GetNonNullableType(), x.Value.ToString()))"> diff --git a/src/BlazorTable/Filters/NumberFilter.razor b/src/BlazorTable/Filters/NumberFilter.razor index 3446ff50..b12cd8bc 100644 --- a/src/BlazorTable/Filters/NumberFilter.razor +++ b/src/BlazorTable/Filters/NumberFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (Column.Type.IsNumeric() && !Column.Type.GetNonNullableType().IsEnum) +@if (Column.FilterControl == this) { Condition = (NumberCondition)Enum.Parse(typeof(NumberCondition), x.Value.ToString()))"> @foreach (var option in (NumberCondition[])Enum.GetValues(typeof(NumberCondition))) diff --git a/src/BlazorTable/Filters/StringFilter.razor b/src/BlazorTable/Filters/StringFilter.razor index 700f15e2..ae3cc211 100644 --- a/src/BlazorTable/Filters/StringFilter.razor +++ b/src/BlazorTable/Filters/StringFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (Column.Type == FilterType) +@if (Column.FilterControl == this) { Condition = (StringCondition)Enum.Parse(typeof(StringCondition), x.Value.ToString()))"> @foreach (var option in (StringCondition[])Enum.GetValues(typeof(StringCondition))) From d3394ea5b65d027f0e93ecf8c08648c73f449d21 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sat, 7 Mar 2020 00:34:19 -0800 Subject: [PATCH 6/9] Breaking Change Simplified Interfaces --- .../Pages/Detail.razor | 14 ++--- .../Pages/EmptyData.razor | 8 +-- .../Pages/Index.razor | 56 +++++++++---------- .../Pages/LoadingData.razor | 8 +-- .../Components/DetailTemplate.razor.cs | 2 +- .../Components/EmptyDataTemplate.razor.cs | 2 +- .../Components/LoadingDataTemplate.razor.cs | 2 +- src/BlazorTable/Components/Table.razor.cs | 6 +- 8 files changed, 45 insertions(+), 53 deletions(-) diff --git a/src/BlazorTable.Sample.Shared/Pages/Detail.razor b/src/BlazorTable.Sample.Shared/Pages/Detail.razor index c2a262eb..70fb661d 100644 --- a/src/BlazorTable.Sample.Shared/Pages/Detail.razor +++ b/src/BlazorTable.Sample.Shared/Pages/Detail.razor @@ -31,14 +31,12 @@ - - Name @personContext.full_name - - Email @personContext.email - - Created Date@personContext.created_date - - + Name @context.full_name + + Email @context.email + + Created Date@context.created_date + diff --git a/src/BlazorTable.Sample.Shared/Pages/EmptyData.razor b/src/BlazorTable.Sample.Shared/Pages/EmptyData.razor index 73300773..cd87a848 100644 --- a/src/BlazorTable.Sample.Shared/Pages/EmptyData.razor +++ b/src/BlazorTable.Sample.Shared/Pages/EmptyData.razor @@ -10,11 +10,9 @@ - - - No rows found! - - + + No rows found! + diff --git a/src/BlazorTable.Sample.Shared/Pages/Index.razor b/src/BlazorTable.Sample.Shared/Pages/Index.razor index abcb10f3..64d5d086 100644 --- a/src/BlazorTable.Sample.Shared/Pages/Index.razor +++ b/src/BlazorTable.Sample.Shared/Pages/Index.razor @@ -35,35 +35,33 @@ @context.cc_type - - - - - - - - @context.email - - - - - @context.paid.ToString() - - - - - - @(context.created_date.HasValue ? context.created_date.Value.ToShortDateString() : string.Empty) - - - - - @context.cc_type - - - - - + + + + + + + @context.email + + + + + @context.paid.ToString() + + + + + + @(context.created_date.HasValue ? context.created_date.Value.ToShortDateString() : string.Empty) + + + + + @context.cc_type + + + + diff --git a/src/BlazorTable.Sample.Shared/Pages/LoadingData.razor b/src/BlazorTable.Sample.Shared/Pages/LoadingData.razor index 2891c049..dc0bb892 100644 --- a/src/BlazorTable.Sample.Shared/Pages/LoadingData.razor +++ b/src/BlazorTable.Sample.Shared/Pages/LoadingData.razor @@ -11,11 +11,9 @@ - - - Custom Loading Template - - + + Custom Loading Template + diff --git a/src/BlazorTable/Components/DetailTemplate.razor.cs b/src/BlazorTable/Components/DetailTemplate.razor.cs index c7fa009a..0212d26e 100644 --- a/src/BlazorTable/Components/DetailTemplate.razor.cs +++ b/src/BlazorTable/Components/DetailTemplate.razor.cs @@ -18,7 +18,7 @@ public partial class DetailTemplate /// Content to show /// [Parameter] - public RenderFragment Template { get; set; } + public RenderFragment ChildContent { get; set; } /// /// When initialized, tell table of this item diff --git a/src/BlazorTable/Components/EmptyDataTemplate.razor.cs b/src/BlazorTable/Components/EmptyDataTemplate.razor.cs index e01e57ec..d9f29e05 100644 --- a/src/BlazorTable/Components/EmptyDataTemplate.razor.cs +++ b/src/BlazorTable/Components/EmptyDataTemplate.razor.cs @@ -17,7 +17,7 @@ public partial class EmptyDataTemplate /// Content to show /// [Parameter] - public RenderFragment Template { get; set; } + public RenderFragment ChildContent { get; set; } /// /// When initialized, tell table of this item diff --git a/src/BlazorTable/Components/LoadingDataTemplate.razor.cs b/src/BlazorTable/Components/LoadingDataTemplate.razor.cs index 15535f52..3bb8bb5f 100644 --- a/src/BlazorTable/Components/LoadingDataTemplate.razor.cs +++ b/src/BlazorTable/Components/LoadingDataTemplate.razor.cs @@ -18,7 +18,7 @@ public partial class LoadingDataTemplate /// Content to show /// [Parameter] - public RenderFragment Template { get; set; } + public RenderFragment ChildContent { get; set; } /// /// When initialized, tell table of this item diff --git a/src/BlazorTable/Components/Table.razor.cs b/src/BlazorTable/Components/Table.razor.cs index ec532976..6d91c8f6 100644 --- a/src/BlazorTable/Components/Table.razor.cs +++ b/src/BlazorTable/Components/Table.razor.cs @@ -292,7 +292,7 @@ private string RowClass(TableItem item) /// public void SetEmptyDataTemplate(EmptyDataTemplate emptyDataTemplate) { - _emptyDataTemplate = emptyDataTemplate?.Template; + _emptyDataTemplate = emptyDataTemplate?.ChildContent; } private RenderFragment _emptyDataTemplate; @@ -303,7 +303,7 @@ public void SetEmptyDataTemplate(EmptyDataTemplate emptyDataTemplate) /// public void SetLoadingDataTemplate(LoadingDataTemplate loadingDataTemplate) { - _loadingDataTemplate = loadingDataTemplate?.Template; + _loadingDataTemplate = loadingDataTemplate?.ChildContent; } private RenderFragment _loadingDataTemplate; @@ -314,7 +314,7 @@ public void SetLoadingDataTemplate(LoadingDataTemplate loadingDataTemplate) /// public void SetDetailTemplate(DetailTemplate detailTemplate) { - _detailTemplate = detailTemplate?.Template; + _detailTemplate = detailTemplate?.ChildContent; } private RenderFragment _detailTemplate; From 8de99bc693b61cf3d093da7df9e67a3a51b010b7 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sat, 7 Mar 2020 00:47:18 -0800 Subject: [PATCH 7/9] cleanup --- src/BlazorTable/Components/LoadingDataTemplate.razor.cs | 1 - src/BlazorTable/Components/Pager.razor.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/BlazorTable/Components/LoadingDataTemplate.razor.cs b/src/BlazorTable/Components/LoadingDataTemplate.razor.cs index 3bb8bb5f..9a96935f 100644 --- a/src/BlazorTable/Components/LoadingDataTemplate.razor.cs +++ b/src/BlazorTable/Components/LoadingDataTemplate.razor.cs @@ -5,7 +5,6 @@ namespace BlazorTable /// /// Child content for null dataset /// - /// public partial class LoadingDataTemplate { /// diff --git a/src/BlazorTable/Components/Pager.razor.cs b/src/BlazorTable/Components/Pager.razor.cs index 75916927..f6f9eeca 100644 --- a/src/BlazorTable/Components/Pager.razor.cs +++ b/src/BlazorTable/Components/Pager.razor.cs @@ -5,7 +5,6 @@ namespace BlazorTable /// /// BlazorTable Pager /// - /// public partial class Pager { [CascadingParameter(Name = "Table")] From fcd304f1655df9fa0767dcdadbf6b7eae7c441aa Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sun, 8 Mar 2020 19:21:08 -0700 Subject: [PATCH 8/9] Exposed Filtered Items --- src/BlazorTable/Components/Table.razor | 6 +++--- src/BlazorTable/Components/Table.razor.cs | 7 +++++-- src/BlazorTable/Interfaces/ITableGen.cs | 5 +++++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/BlazorTable/Components/Table.razor b/src/BlazorTable/Components/Table.razor index 8427ef23..7699d483 100644 --- a/src/BlazorTable/Components/Table.razor +++ b/src/BlazorTable/Components/Table.razor @@ -45,13 +45,13 @@ - @if (TempItems != null) + @if (FilteredItems != null) { - if (TempItems.Any()) + if (FilteredItems.Any()) { int i = 0; - foreach (TableItem item in TempItems) + foreach (TableItem item in FilteredItems) { diff --git a/src/BlazorTable/Components/Table.razor.cs b/src/BlazorTable/Components/Table.razor.cs index 6d91c8f6..e31fc145 100644 --- a/src/BlazorTable/Components/Table.razor.cs +++ b/src/BlazorTable/Components/Table.razor.cs @@ -68,7 +68,10 @@ public partial class Table : ITable [Inject] private ILogger> Logger { get; set; } - private IEnumerable TempItems { get; set; } + /// + /// Collection of filtered items + /// + public IEnumerable FilteredItems { get; private set; } /// /// List of All Available Columns @@ -151,7 +154,7 @@ private IEnumerable GetData() public void Update() { detailsViewOpen = new bool[PageSize]; - TempItems = GetData(); + FilteredItems = GetData(); Refresh(); } diff --git a/src/BlazorTable/Interfaces/ITableGen.cs b/src/BlazorTable/Interfaces/ITableGen.cs index 45bdd623..addd64a2 100644 --- a/src/BlazorTable/Interfaces/ITableGen.cs +++ b/src/BlazorTable/Interfaces/ITableGen.cs @@ -38,6 +38,11 @@ public interface ITable : ITable /// IEnumerable Items { get; set; } + /// + /// Collection of filtered items + /// + IEnumerable FilteredItems { get; } + /// /// Set the SetDetailTemplate for the table /// From bdc56c795af4bbc629e2734501615dfa53c98465 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Sun, 8 Mar 2020 19:30:21 -0700 Subject: [PATCH 9/9] Added Custom Select IFilter --- .../Properties/launchSettings.json | 7 +- src/BlazorTable.Sample.Shared/NavMenu.razor | 5 + .../Pages/CustomSelectExample.razor | 53 ++++++++ .../Components/CustomSelectOption.razor | 1 + .../Components/CustomSelectOption.razor.cs | 30 +++++ src/BlazorTable/Filters/CustomSelect.razor | 26 ++++ src/BlazorTable/Filters/CustomSelect.razor.cs | 118 ++++++++++++++++++ src/BlazorTable/Filters/EnumFilter.razor.cs | 2 - src/BlazorTable/Interfaces/ICustomSelect.cs | 7 ++ 9 files changed, 244 insertions(+), 5 deletions(-) create mode 100644 src/BlazorTable.Sample.Shared/Pages/CustomSelectExample.razor create mode 100644 src/BlazorTable/Components/CustomSelectOption.razor create mode 100644 src/BlazorTable/Components/CustomSelectOption.razor.cs create mode 100644 src/BlazorTable/Filters/CustomSelect.razor create mode 100644 src/BlazorTable/Filters/CustomSelect.razor.cs create mode 100644 src/BlazorTable/Interfaces/ICustomSelect.cs diff --git a/src/BlazorTable.Sample.Server/Properties/launchSettings.json b/src/BlazorTable.Sample.Server/Properties/launchSettings.json index 22324bd0..44b37566 100644 --- a/src/BlazorTable.Sample.Server/Properties/launchSettings.json +++ b/src/BlazorTable.Sample.Server/Properties/launchSettings.json @@ -18,10 +18,11 @@ "BlazorTable.Sample.Server": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "http://localhost:51075/", + "launchUrl": "/CustomSelect", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "applicationUrl": "http://localhost:51075/" } } -} +} \ No newline at end of file diff --git a/src/BlazorTable.Sample.Shared/NavMenu.razor b/src/BlazorTable.Sample.Shared/NavMenu.razor index 9c51b5ed..303e9176 100644 --- a/src/BlazorTable.Sample.Shared/NavMenu.razor +++ b/src/BlazorTable.Sample.Shared/NavMenu.razor @@ -42,6 +42,11 @@ JObject + + + CustomSelect + + diff --git a/src/BlazorTable.Sample.Shared/Pages/CustomSelectExample.razor b/src/BlazorTable.Sample.Shared/Pages/CustomSelectExample.razor new file mode 100644 index 00000000..b9ca7266 --- /dev/null +++ b/src/BlazorTable.Sample.Shared/Pages/CustomSelectExample.razor @@ -0,0 +1,53 @@ +@page "/CustomSelect" + +@using BlazorTable + +CustomSelect + +The CustomSelect can be used to display custom filter options. + + + + + + + + + + + + + + + +@code +{ + [Inject] + private HttpClient Http { get; set; } + + private PersonData[] data; + + protected override async Task OnInitializedAsync() + { + data = await Http.GetJsonAsync("sample-data/MOCK_DATA.json"); + } + + public class PersonData + { + public int? id { get; set; } + public string full_name { get; set; } + public string email { get; set; } + public bool? paid { get; set; } + public decimal? price { get; set; } + public CreditCard? cc_type { get; set; } + public DateTime? created_date { get; set; } + } + + public enum CreditCard + { + none = 0, + [Description("MasterCard")] + MasterCard = 1, + Visa = 2 + } +} diff --git a/src/BlazorTable/Components/CustomSelectOption.razor b/src/BlazorTable/Components/CustomSelectOption.razor new file mode 100644 index 00000000..8fba2a64 --- /dev/null +++ b/src/BlazorTable/Components/CustomSelectOption.razor @@ -0,0 +1 @@ +@namespace BlazorTable \ No newline at end of file diff --git a/src/BlazorTable/Components/CustomSelectOption.razor.cs b/src/BlazorTable/Components/CustomSelectOption.razor.cs new file mode 100644 index 00000000..5484eff9 --- /dev/null +++ b/src/BlazorTable/Components/CustomSelectOption.razor.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Components; + +namespace BlazorTable +{ + /// + /// Option for CustomSelect + /// + public partial class CustomSelectOption + { + /// + /// Parent table + /// + [CascadingParameter(Name = "CustomSelect")] + public ICustomSelect CustomSelect { get; set; } + + [Parameter] + public string Key { get; set; } + + [Parameter] + public object Value { get; set; } + + /// + /// When initialized, tell CustomSelect of this item + /// + protected override void OnInitialized() + { + CustomSelect.AddSelect(Key, Value); + } + } +} diff --git a/src/BlazorTable/Filters/CustomSelect.razor b/src/BlazorTable/Filters/CustomSelect.razor new file mode 100644 index 00000000..b82de818 --- /dev/null +++ b/src/BlazorTable/Filters/CustomSelect.razor @@ -0,0 +1,26 @@ +@namespace BlazorTable +@typeparam TableItem + +@if (Column.FilterControl == this) +{ + Condition = (CustomSelectCondition)Enum.Parse(typeof(CustomSelectCondition), x.Value.ToString()))"> + @foreach (CustomSelectCondition option in Enum.GetValues(typeof(CustomSelectCondition))) + { + @option.GetDescription() + } + + + @if (Condition != CustomSelectCondition.IsNull && Condition != CustomSelectCondition.IsNotNull) + { + FilterValue = x.Value)"> + @foreach (var option in Items) + { + @option.Key + } + + } +} + + + @ChildContent + \ No newline at end of file diff --git a/src/BlazorTable/Filters/CustomSelect.razor.cs b/src/BlazorTable/Filters/CustomSelect.razor.cs new file mode 100644 index 00000000..14ca8e03 --- /dev/null +++ b/src/BlazorTable/Filters/CustomSelect.razor.cs @@ -0,0 +1,118 @@ +using Microsoft.AspNetCore.Components; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; +using System.Linq.Expressions; + +namespace BlazorTable +{ + public partial class CustomSelect : IFilter, ICustomSelect + { + [CascadingParameter(Name = "Column")] + public IColumn Column { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } + + private List> Items = new List>(); + + private CustomSelectCondition Condition { get; set; } + + private object FilterValue { get; set; } + + protected override void OnInitialized() + { + Column.FilterControl = this; + + if (Column.Filter?.Body is BinaryExpression binaryExpression) + { + if (binaryExpression.NodeType == ExpressionType.AndAlso) + { + switch (binaryExpression.Right.NodeType) + { + case ExpressionType.Equal: + Condition = CustomSelectCondition.IsEqualTo; + break; + case ExpressionType.NotEqual: + Condition = CustomSelectCondition.IsNotEqualTo; + break; + } + } + else + { + if (binaryExpression.NodeType == ExpressionType.Equal) + { + Condition = CustomSelectCondition.IsNull; + } + else if (binaryExpression.NodeType == ExpressionType.NotEqual) + { + Condition = CustomSelectCondition.IsNotNull; + } + } + + if (binaryExpression.Right is BinaryExpression binaryExpression2 + && binaryExpression2.Right is ConstantExpression constantExpression) + { + FilterValue = constantExpression.Value; + } + } + } + + public Expression> GetFilter() + { + return Condition switch + { + CustomSelectCondition.IsEqualTo => + Expression.Lambda>( + Expression.AndAlso( + Expression.NotEqual(Column.Field.Body, Expression.Constant(null)), + Expression.Equal( + Expression.Convert(Column.Field.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Field.Parameters), + + CustomSelectCondition.IsNotEqualTo => Expression.Lambda>( + Expression.AndAlso( + Expression.NotEqual(Column.Field.Body, Expression.Constant(null)), + Expression.NotEqual( + Expression.Convert(Column.Field.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Field.Parameters), + + CustomSelectCondition.IsNull => + Expression.Lambda>( + Expression.Equal(Column.Field.Body, Expression.Constant(null)), + Column.Field.Parameters), + + CustomSelectCondition.IsNotNull => + Expression.Lambda>( + Expression.NotEqual(Column.Field.Body, Expression.Constant(null)), + Column.Field.Parameters), + + _ => throw new ArgumentException(Condition + " is not defined!"), + }; + } + + public void AddSelect(string key, object value) + { + Items.Add(new KeyValuePair(key, value)); + StateHasChanged(); + } + + public enum CustomSelectCondition + { + [Description("Is Equal To")] + IsEqualTo, + + [Description("Is Not Equal To")] + IsNotEqualTo, + + [Description("Is null")] + IsNull, + + [Description("Is not null")] + IsNotNull + } + } +} diff --git a/src/BlazorTable/Filters/EnumFilter.razor.cs b/src/BlazorTable/Filters/EnumFilter.razor.cs index f846d1e5..d2d23618 100644 --- a/src/BlazorTable/Filters/EnumFilter.razor.cs +++ b/src/BlazorTable/Filters/EnumFilter.razor.cs @@ -21,8 +21,6 @@ protected override void OnInitialized() { Column.FilterControl = this; - FilterValue = Enum.GetValues(Column.Type.GetNonNullableType()).GetValue(0); - if (Column.Filter?.Body is BinaryExpression binaryExpression) { if (binaryExpression.NodeType == ExpressionType.AndAlso) diff --git a/src/BlazorTable/Interfaces/ICustomSelect.cs b/src/BlazorTable/Interfaces/ICustomSelect.cs new file mode 100644 index 00000000..e1977af5 --- /dev/null +++ b/src/BlazorTable/Interfaces/ICustomSelect.cs @@ -0,0 +1,7 @@ +namespace BlazorTable +{ + public interface ICustomSelect + { + public void AddSelect(string key, object value); + } +} \ No newline at end of file
The CustomSelect can be used to display custom filter options.
CustomSelect