Skip to content

Commit 6e16e95

Browse files
rajrakuRaj Kumar Sri Ramulugvreddy04
authored
Enum filters (#1073)
* Updating the Enum filter values to Display attribute for name * Updated the example to use the Display attribute * Fixed the param related error * Example updates --------- Co-authored-by: Raj Kumar Sri Ramulu <[email protected]> Co-authored-by: Vikram Reddy <[email protected]>
1 parent 41859ad commit 6e16e95

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

BlazorBootstrap.Demo.RCL/Components/Pages/Grid/03-filters/Grid_Demo_05_Enum_Filters.razor

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<Grid TItem="User"
1+
@using System.ComponentModel.DataAnnotations
2+
3+
<Grid TItem="User"
24
Class="table table-hover table-bordered table-striped"
35
DataProvider="UsersDataProvider"
46
AllowFiltering="true"
@@ -15,7 +17,7 @@
1517
@context.DOB
1618
</GridColumn>
1719
<GridColumn TItem="User" HeaderText="Status" PropertyName="Status" FilterTextboxWidth="170">
18-
@context.Status
20+
@(typeof(UserStatus).GetDisplayName(context.Status.ToString()))
1921
</GridColumn>
2022
</GridColumns>
2123

@@ -60,6 +62,7 @@
6062
public enum UserStatus
6163
{
6264
Registered,
65+
[Display(Name = "Pending Verification")]
6366
VerificationPending,
6467
Verified
6568
}

BlazorBootstrap.Demo.RCL/Components/Pages/Grid/03-filters/Grid_Demo_06_Guid_Filters.razor

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<Grid TItem="User"
1+
@using System.ComponentModel.DataAnnotations
2+
3+
<Grid TItem="User"
24
Class="table table-hover table-bordered table-striped"
35
DataProvider="UsersDataProvider"
46
AllowFiltering="true"
@@ -18,7 +20,7 @@
1820
@context.DOB
1921
</GridColumn>
2022
<GridColumn TItem="User" HeaderText="Status" PropertyName="Status" FilterTextboxWidth="170">
21-
@context.Status
23+
@(typeof(UserStatus).GetDisplayName(context.Status.ToString()))
2224
</GridColumn>
2325
</GridColumns>
2426

@@ -64,6 +66,7 @@
6466
public enum UserStatus
6567
{
6668
Registered,
69+
[Display(Name = "Pending Verification")]
6770
VerificationPending,
6871
Verified
6972
}

blazorbootstrap/Components/Grid/GridColumnFilter.razor

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@
6262
}
6363
else
6464
{
65-
<span class="px-2">@filterValue</span>
65+
<span class="px-2">@(PropertyType!.GetDisplayName(filterValue))</span>
6666
}
6767
</DropdownToggleButton>
6868
<DropdownMenu Class="bb-dropdown-menu-enum" Style="max-height: var(--bb-grid-filter-dropdown-max-height); overflow-x: hidden; overflow-y: auto;">
6969
@if (PropertyType is not null)
7070
{
7171
@foreach (var item in Enum.GetValues(PropertyType!))
7272
{
73-
<DropdownItem @onclick="@(async () => await OnEnumFilterValueChangedAsync(item))">@item</DropdownItem>
73+
var name = PropertyType!.GetDisplayName(Enum.GetName(PropertyType!, item));
74+
<DropdownItem @onclick="@(async () => await OnEnumFilterValueChangedAsync(item))">@name</DropdownItem>
7475
}
7576
}
7677
</DropdownMenu>

blazorbootstrap/Extensions/TypeExtensions.cs

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
namespace BlazorBootstrap;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Reflection;
4+
25

36
/// <summary>
47
/// Various extension methods for <see cref="Type" />.
@@ -82,5 +85,16 @@ public static string GetPropertyTypeName(this Type type, string propertyName)
8285
return type.GetProperty(propertyName)?.PropertyType;
8386
}
8487

88+
public static string? GetDisplayName(this Type type, string? name)
89+
{
90+
if (name != null)
91+
{
92+
var attr = type!.GetMember(name).FirstOrDefault()?.GetCustomAttribute<DisplayAttribute>();
93+
name = attr?.Name ?? name;
94+
}
95+
96+
return name;
97+
}
98+
8599
#endregion
86100
}

0 commit comments

Comments
 (0)