From 409152a94195612b6604ad312095250ea79b03c2 Mon Sep 17 00:00:00 2001 From: tesar-tech Date: Wed, 25 Dec 2024 23:51:07 +0100 Subject: [PATCH 1/5] Filter text input in DropDownList. --- .../Blazorise.Components/DropdownList.razor | 25 +++++---- .../DropdownList.razor.cs | 51 ++++++++++++++++++- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor b/Source/Extensions/Blazorise.Components/DropdownList.razor index 87a85762a3..2e476c8b00 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor @@ -1,12 +1,19 @@ @using Microsoft.AspNetCore.Components.Web.Virtualization @typeparam TItem @typeparam TValue - - @ChildContent - - @if ( Data != null ) + + @ChildContent + + @if (FilterEnabled) { - @if ( Virtualize && Data is ICollection collectionableData ) + + } + @if (FilteredData != null) + { + @if ( Virtualize && FilteredData is ICollection collectionableData ) { @itemFragment( item ) @@ -14,7 +21,7 @@ } else { - @foreach ( var item in Data ?? Enumerable.Empty() ) + @foreach ( var item in FilteredData ?? Enumerable.Empty() ) { @itemFragment( item ) } @@ -22,9 +29,9 @@ } + @code { - protected RenderFragment itemFragment => item => __builder => - { + protected RenderFragment itemFragment => item => __builder => { var text = GetItemText( item ); var value = GetItemValue( item ); var disabled = GetItemDisabled( item ); @@ -32,6 +39,6 @@ @text + CheckedChanged="@((isChecked) => HandleDropdownItemChecked( isChecked, value ))">@text }; } \ No newline at end of file diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs index 26ee1a66b9..a22e8e1fdf 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs @@ -28,8 +28,10 @@ public partial class DropdownList : ComponentBase /// Reference to the DropdownToggle component. /// protected DropdownToggle dropdownToggleRef; - + private List selectedValues; + + private IEnumerable filteredData; #endregion @@ -103,6 +105,34 @@ private bool GetItemDisabled( TItem item ) return DisabledItem.Invoke( item ); } + private void FilterData( IQueryable query ) + { + dirtyFilter = false; + if ( !FilterEnabled || string.IsNullOrEmpty( FilterText ) ) + { + filteredData = Data; + return; + } + + if ( query == null ) + { + filteredData = Enumerable.Empty(); + return; + } + + if ( TextField == null ) + return; + + filteredData = Data.Where( x => TextField.Invoke( x ).Contains( FilterText, StringComparison.OrdinalIgnoreCase ) ); + } + + private Task OnFilterTextChangedHandler( string filteredText ) + { + FilterText = filteredText; + dirtyFilter = true; + return Task.CompletedTask; + } + #endregion #region Properties @@ -148,6 +178,20 @@ protected bool IsSelected( TValue value ) /// [Parameter] public IEnumerable Data { get; set; } + private IEnumerable FilteredData + { + get + { + if ( dirtyFilter ) + FilterData( Data?.AsQueryable() ); + return filteredData; + } + } + + private bool dirtyFilter = true; + + private string FilterText { get; set; } + /// /// Method used to get the display field from the supplied data source. /// @@ -168,6 +212,11 @@ protected bool IsSelected( TValue value ) /// [Parameter] public EventCallback SelectedValueChanged { get; set; } + /// + /// Enebles filter text input on the top of the items list. + /// + [Parameter] public bool FilterEnabled { get; set; } + /// /// Custom classname for dropdown element. /// From 7c480fa52c90e614a8971253ac106bcda221deb2 Mon Sep 17 00:00:00 2001 From: tesar-tech Date: Sat, 28 Dec 2024 11:30:47 +0100 Subject: [PATCH 2/5] renames to Filterable --- Source/Extensions/Blazorise.Components/DropdownList.razor | 4 ++-- Source/Extensions/Blazorise.Components/DropdownList.razor.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor b/Source/Extensions/Blazorise.Components/DropdownList.razor index 2e476c8b00..1bfe27712b 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor @@ -5,8 +5,8 @@ Disabled="@Disabled" Direction="@Direction" Attributes="@Attributes"> @ChildContent - - @if (FilterEnabled) + + @if (Filterable) { diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs index a22e8e1fdf..843898067e 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs @@ -108,7 +108,7 @@ private bool GetItemDisabled( TItem item ) private void FilterData( IQueryable query ) { dirtyFilter = false; - if ( !FilterEnabled || string.IsNullOrEmpty( FilterText ) ) + if ( !Filterable || string.IsNullOrEmpty( FilterText ) ) { filteredData = Data; return; @@ -215,7 +215,7 @@ private IEnumerable FilteredData /// /// Enebles filter text input on the top of the items list. /// - [Parameter] public bool FilterEnabled { get; set; } + [Parameter] public bool Filterable { get; set; } /// /// Custom classname for dropdown element. From 34c14d6c46b5c6745cdbd7f1034b2d5caaed2ee9 Mon Sep 17 00:00:00 2001 From: Mladen Macanovic Date: Mon, 13 Jan 2025 11:02:57 +0100 Subject: [PATCH 3/5] formating --- .../Pages/Tests/DropdownListPage.razor | 3 ++- .../Blazorise.Components/DropdownList.razor | 13 +++++-------- .../Blazorise.Components/DropdownList.razor.cs | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor b/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor index e152a36185..9d833c7aa0 100644 --- a/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor +++ b/Demos/Blazorise.Demo/Pages/Tests/DropdownListPage.razor @@ -19,7 +19,8 @@ @bind-SelectedValue="@selectedDropValue" Color="Color.Primary" MaxMenuHeight="200px" - DropdownToggleSize="Size.Large"> + DropdownToggleSize="Size.Large" + Filterable> Select item diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor b/Source/Extensions/Blazorise.Components/DropdownList.razor index 1bfe27712b..6b13d3245a 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor @@ -1,17 +1,14 @@ @using Microsoft.AspNetCore.Components.Web.Virtualization @typeparam TItem @typeparam TValue - - @ChildContent + + @ChildContent - @if (Filterable) + @if ( Filterable ) { - + } - @if (FilteredData != null) + @if ( FilteredData != null ) { @if ( Virtualize && FilteredData is ICollection collectionableData ) { diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs index 843898067e..0489ff7638 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor.cs +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor.cs @@ -28,9 +28,9 @@ public partial class DropdownList : ComponentBase /// Reference to the DropdownToggle component. /// protected DropdownToggle dropdownToggleRef; - + private List selectedValues; - + private IEnumerable filteredData; #endregion @@ -113,7 +113,7 @@ private void FilterData( IQueryable query ) filteredData = Data; return; } - + if ( query == null ) { filteredData = Enumerable.Empty(); @@ -187,7 +187,7 @@ private IEnumerable FilteredData return filteredData; } } - + private bool dirtyFilter = true; private string FilterText { get; set; } From 56ad325ebf45220a636b28ea982c608302a2886d Mon Sep 17 00:00:00 2001 From: Mladen Macanovic Date: Mon, 13 Jan 2025 11:03:29 +0100 Subject: [PATCH 4/5] remove unnecesary if statement --- .../Blazorise.Components/DropdownList.razor | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor b/Source/Extensions/Blazorise.Components/DropdownList.razor index 6b13d3245a..e7fc178922 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor @@ -8,20 +8,17 @@ { } - @if ( FilteredData != null ) + @if ( Virtualize && FilteredData is ICollection collectionableData ) { - @if ( Virtualize && FilteredData is ICollection collectionableData ) - { - - @itemFragment( item ) - - } - else + + @itemFragment( item ) + + } + else + { + @foreach ( var item in FilteredData ?? Enumerable.Empty() ) { - @foreach ( var item in FilteredData ?? Enumerable.Empty() ) - { - @itemFragment( item ) - } + @itemFragment( item ) } } From a18235900ccef76adad8a0aac6b73ab9e7308f79 Mon Sep 17 00:00:00 2001 From: Mladen Macanovic Date: Mon, 13 Jan 2025 11:30:54 +0100 Subject: [PATCH 5/5] add field with spacing --- Source/Extensions/Blazorise.Components/DropdownList.razor | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Extensions/Blazorise.Components/DropdownList.razor b/Source/Extensions/Blazorise.Components/DropdownList.razor index e7fc178922..57b946d746 100644 --- a/Source/Extensions/Blazorise.Components/DropdownList.razor +++ b/Source/Extensions/Blazorise.Components/DropdownList.razor @@ -6,7 +6,9 @@ @if ( Filterable ) { - + + + } @if ( Virtualize && FilteredData is ICollection collectionableData ) {