diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs b/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs index 40842590f0b5927a46a8a4f7d25e778e346d9dcd..50497cbb14d8a97c733ea84c40ae79641d10c9c4 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Samples/Table/TablesDynamic.razor.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Data; using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; @@ -59,22 +60,30 @@ namespace BootstrapBlazor.Shared.Pages.Table if (propertyName == nameof(Foo.DateTime)) { context.AddRequiredAttribute(nameof(Foo.DateTime)); - col.Text = Localizer[nameof(Foo.DateTime)]; + // 使用 AutoGenerateColumnAttribute 设置显示名称示例 + context.AddAutoGenerateColumnAttribute(nameof(Foo.DateTime), new KeyValuePair[] { + new(nameof(AutoGenerateColumnAttribute.Text), Localizer[nameof(Foo.DateTime)].Value) + }); } else if (propertyName == nameof(Foo.Name)) { - context.AddRequiredAttribute(nameof(Foo.Name), "测试姓名不能为空"); + context.AddRequiredAttribute(nameof(Foo.Name), Localizer["Name.Required"]); + // 使用 Text 设置显示名称示例 col.Text = Localizer[nameof(Foo.Name)]; } else if (propertyName == nameof(Foo.Count)) { context.AddRequiredAttribute(nameof(Foo.Count)); - col.Text = Localizer[nameof(Foo.Count)]; + // 使用 DisplayNameAttribute 设置显示名称示例 + context.AddDisplayNameAttribute(nameof(Foo.Count), Localizer[nameof(Foo.Count)].Value); } else if (propertyName == nameof(Foo.Complete)) { col.ComponentType = typeof(Switch); - col.Text = Localizer[nameof(Foo.Complete)]; + // 使用 DisplayAttribute 设置显示名称示例 + context.AddDisplayAttribute(nameof(Foo.Complete), new KeyValuePair[] { + new(nameof(DisplayAttribute.Name), Localizer[nameof(Foo.Complete)].Value) + }); } }); } diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs index f55f625fe39afb9a1b87fbe699388fde5586ce29..e79fe050688ffb1ae4ae9707a8deee42a7aad04f 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DynamicObjectContext.cs @@ -42,12 +42,14 @@ namespace BootstrapBlazor.Components /// /// /// - public void AddAttribute(string columnName, Type attributeType, Type[] types, object?[] constructorArgs, PropertyInfo[] propertyInfos, object?[] propertyValues) + public void AddAttribute(string columnName, Type attributeType, Type[] types, object?[] constructorArgs, PropertyInfo[]? propertyInfos = null, object?[]? propertyValues = null) { var attr = attributeType.GetConstructor(types); if (attr != null) { - var cab = new CustomAttributeBuilder(attr, constructorArgs, propertyInfos, propertyValues); + var cab = new CustomAttributeBuilder(attr, constructorArgs, + namedProperties: propertyInfos ?? Array.Empty(), + propertyValues: propertyValues ?? Array.Empty()); CustomerAttributeBuilderCache.AddOrUpdate(columnName, key => new List { cab }, (key, builders) => diff --git a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs index 0dbadadefd2cae5593a4f224dae3f7cb51b9cd3b..673fa2be76dd2bd12ee235adecd8c5a623221168 100644 --- a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs +++ b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs @@ -4,8 +4,8 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Reflection; namespace BootstrapBlazor.Components @@ -24,20 +24,12 @@ namespace BootstrapBlazor.Components /// public static void AddRequiredAttribute(this DynamicObjectContext context, string columnName, string? errorMessage = null, bool allowEmptyStrings = false) { - var type = typeof(RequiredAttribute); - var propertyInfos = new List(); - var propertyValues = new List(); - if (!string.IsNullOrEmpty(errorMessage)) - { - propertyInfos.Add(type.GetProperty(nameof(RequiredAttribute.ErrorMessage))!); - propertyValues.Add(errorMessage); - } - if (allowEmptyStrings) + var parameters = new KeyValuePair[] { - propertyInfos.Add(type.GetProperty(nameof(RequiredAttribute.AllowEmptyStrings))!); - propertyValues.Add(true); - } - context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty(), propertyInfos.ToArray(), propertyValues.ToArray()); + new(nameof(RequiredAttribute.ErrorMessage), errorMessage), + new(nameof(RequiredAttribute.AllowEmptyStrings), allowEmptyStrings) + }; + context.AddMultipleParameterAttribute(columnName, parameters); } /// @@ -46,9 +38,19 @@ namespace BootstrapBlazor.Components /// /// /// - public static void AddAutoGenerateColumnAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) + public static void AddAutoGenerateColumnAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) => context.AddMultipleParameterAttribute(columnName, parameters); + + /// + /// 增加 DisplayAttribute 扩展方法 + /// + /// + /// + /// + public static void AddDisplayAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) => context.AddMultipleParameterAttribute(columnName, parameters); + + private static void AddMultipleParameterAttribute(this DynamicObjectContext context, string columnName, IEnumerable> parameters) where TAttribute : Attribute { - var type = typeof(AutoGenerateColumnAttribute); + var type = typeof(TAttribute); var propertyInfos = new List(); var propertyValues = new List(); foreach (var kv in parameters) @@ -62,5 +64,29 @@ namespace BootstrapBlazor.Components } context.AddAttribute(columnName, type, Type.EmptyTypes, Array.Empty(), propertyInfos.ToArray(), propertyValues.ToArray()); } + + /// + /// 增加 DisplayNameAttribute 扩展方法 + /// + /// + /// + /// + public static void AddDisplayNameAttribute(this DynamicObjectContext context, string columnName, string displayName) + { + var type = typeof(DisplayNameAttribute); + context.AddAttribute(columnName, type, new Type[] { typeof(string) }, new object?[] { displayName }); + } + + /// + /// 增加 DescriptionAttribute 扩展方法 + /// + /// + /// + /// + public static void AddDescriptionAttribute(this DynamicObjectContext context, string columnName, string description) + { + var type = typeof(DescriptionAttribute); + context.AddAttribute(columnName, type, new Type[] { typeof(string) }, new object?[] { description }); + } } }