From d926d911a37d18f8194dc55314ed1f616499984b Mon Sep 17 00:00:00 2001 From: Vincent Baaij Date: Mon, 1 Jul 2024 13:25:36 +0200 Subject: [PATCH 1/2] Make PropertyColumn use DisplayAttribute value for enum --- .../Demo/Shared/Pages/Lab/IssueTester.razor | 46 ++++++++++++++++++- .../DataGrid/Columns/PropertyColumn.cs | 16 ++++++- src/Core/Extensions/EnumExtensions.cs | 8 ++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/examples/Demo/Shared/Pages/Lab/IssueTester.razor b/examples/Demo/Shared/Pages/Lab/IssueTester.razor index 3f0945b4a7..1411b720c8 100644 --- a/examples/Demo/Shared/Pages/Lab/IssueTester.razor +++ b/examples/Demo/Shared/Pages/Lab/IssueTester.razor @@ -1,3 +1,47 @@ @page "/issue-tester" +@using System.ComponentModel.DataAnnotations - + + + + + + + +@code { + + public enum Positions + { + [Display(Name = "Employee (DA)")] + Employee, + [Display(Name = "HR Manager (DA)")] + HrManager, + [Display(Name = "Project Manager (DA)")] + ProjectManager, + [Display(Name = "Administrator (DA)")] + Administrator + } + + public class Employee + { + [Display(Name = "Id")] + public int Id { get; set; } + [Required, Display(Name = "Full Name")] + public string FullName { get; set; } + [Required, Display(Name = "Subdivision")] + public string Subdivision { get; set; } + [Required, Display(Name = "Position")] + public Positions Position { get; set; } + + } + + private List employees = new List + { + new Employee { Id = 1, FullName = "John Doe", Subdivision = "IT", Position = Positions.Employee }, + new Employee { Id = 2, FullName = "Jane Doe", Subdivision = "HR", Position = Positions.HrManager }, + new Employee { Id = 3, FullName = "John Smith", Subdivision = "IT", Position = Positions.ProjectManager }, + new Employee { Id = 4, FullName = "Jane Smith", Subdivision = "HR", Position = Positions.Administrator } + }; + + +} diff --git a/src/Core/Components/DataGrid/Columns/PropertyColumn.cs b/src/Core/Components/DataGrid/Columns/PropertyColumn.cs index d49e556fa9..817a588fa8 100644 --- a/src/Core/Components/DataGrid/Columns/PropertyColumn.cs +++ b/src/Core/Components/DataGrid/Columns/PropertyColumn.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; using Microsoft.FluentUI.AspNetCore.Components.DataGrid.Infrastructure; +using Microsoft.FluentUI.AspNetCore.Components.Extensions; namespace Microsoft.FluentUI.AspNetCore.Components; @@ -73,7 +74,20 @@ protected override void OnParametersSet() } else { - _cellTextFunc = item => compiledPropertyExpression!(item)?.ToString(); + _cellTextFunc = item => + { + var value = compiledPropertyExpression!(item); + + if (typeof(TProp).IsEnum) + { + var displayValue = (value as Enum)?.GetDisplayName(); + return displayValue; + } + else + { + return value?.ToString(); + } + }; } _sortBuilder = Comparer is not null ? GridSort.ByAscending(Property, Comparer) : GridSort.ByAscending(Property); diff --git a/src/Core/Extensions/EnumExtensions.cs b/src/Core/Extensions/EnumExtensions.cs index b3e4abc33f..fc437c8c73 100644 --- a/src/Core/Extensions/EnumExtensions.cs +++ b/src/Core/Extensions/EnumExtensions.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.ComponentModel.DataAnnotations; using System.Reflection; namespace Microsoft.FluentUI.AspNetCore.Components.Extensions; @@ -38,4 +39,11 @@ public static class EnumExtensions return description; } + + public static string GetDisplayName(this Enum enumValue) + { + var memberInfo = enumValue.GetType().GetMember(enumValue.ToString()); + var displayAttribute = memberInfo[0].GetCustomAttribute(); + return displayAttribute?.Name ?? enumValue.ToString(); + } } From a17e5dd35e08e1b7cc3c40024dcebfffdea14b62 Mon Sep 17 00:00:00 2001 From: Vincent Baaij Date: Mon, 1 Jul 2024 13:36:54 +0200 Subject: [PATCH 2/2] Simplify code an extend example with a non attribute value --- examples/Demo/Shared/Pages/Lab/IssueTester.razor | 5 ++--- src/Core/Components/DataGrid/Columns/PropertyColumn.cs | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/examples/Demo/Shared/Pages/Lab/IssueTester.razor b/examples/Demo/Shared/Pages/Lab/IssueTester.razor index 1411b720c8..2589c2a7da 100644 --- a/examples/Demo/Shared/Pages/Lab/IssueTester.razor +++ b/examples/Demo/Shared/Pages/Lab/IssueTester.razor @@ -12,8 +12,7 @@ public enum Positions { - [Display(Name = "Employee (DA)")] - Employee, + employee, [Display(Name = "HR Manager (DA)")] HrManager, [Display(Name = "Project Manager (DA)")] @@ -37,7 +36,7 @@ private List employees = new List { - new Employee { Id = 1, FullName = "John Doe", Subdivision = "IT", Position = Positions.Employee }, + new Employee { Id = 1, FullName = "John Doe", Subdivision = "IT", Position = Positions.employee }, new Employee { Id = 2, FullName = "Jane Doe", Subdivision = "HR", Position = Positions.HrManager }, new Employee { Id = 3, FullName = "John Smith", Subdivision = "IT", Position = Positions.ProjectManager }, new Employee { Id = 4, FullName = "Jane Smith", Subdivision = "HR", Position = Positions.Administrator } diff --git a/src/Core/Components/DataGrid/Columns/PropertyColumn.cs b/src/Core/Components/DataGrid/Columns/PropertyColumn.cs index 817a588fa8..9c4557b023 100644 --- a/src/Core/Components/DataGrid/Columns/PropertyColumn.cs +++ b/src/Core/Components/DataGrid/Columns/PropertyColumn.cs @@ -80,8 +80,7 @@ protected override void OnParametersSet() if (typeof(TProp).IsEnum) { - var displayValue = (value as Enum)?.GetDisplayName(); - return displayValue; + return (value as Enum)?.GetDisplayName(); } else {