Skip to content

Commit 93bab57

Browse files
author
Raj Kumar Sri Ramulu
committed
Updating the Enum filter values to Display attribute for name
1 parent 4d67533 commit 93bab57

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

blazorbootstrap/Components/Grid/GridColumnFilter.razor

Lines changed: 3 additions & 2 deletions
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

Lines changed: 14 additions & 0 deletions
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)