From 992c3e43d4ab7e42536d468e96d3557b4c884cb3 Mon Sep 17 00:00:00 2001 From: william19941994 <59403632+william19941994@users.noreply.github.com> Date: Mon, 14 Mar 2022 01:43:34 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E4=B8=AALookU?= =?UTF-8?q?pService=E7=B1=BB=EF=BC=8C=E9=80=9A=E8=BF=87AutoGenerateColumnA?= =?UTF-8?q?ttribute=E4=B8=AD=E7=9A=84LookUpServiceCatalog=E6=9D=A5?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E8=BD=BD=E5=85=A5Select=E4=B8=AD=E7=9A=84Loo?= =?UTF-8?q?kup=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实测EditorForm和Table的AutoGenerateColumn的时候都可以。 旧项目配合新库暂时未测试。 --- src/BootstrapBlazor.Shared/Data/Foo.cs | 9 +++ .../DemoLookUpServiceCollectionExtensions.cs | 61 +++++++++++++++++++ .../Extensions/ServicesExtensions.cs | 1 + .../Attributes/AutoGenerateColumnAttribute.cs | 6 ++ .../Components/EditorForm/EditorForm.razor.cs | 5 +- .../Components/EditorForm/EditorItem.cs | 6 ++ .../Components/EditorForm/IEditorItem.cs | 5 ++ .../Components/EditorForm/ILookUpService.cs | 30 +++++++++ .../Components/Table/InternalTableColumn.cs | 6 ++ .../Components/Table/Table.razor.cs | 10 ++- .../Components/Table/TableColumn.cs | 6 ++ ...tstrapBlazorServiceCollectionExtensions.cs | 1 + src/BootstrapBlazor/Utils/Utility.cs | 12 +++- 13 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs create mode 100644 src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs diff --git a/src/BootstrapBlazor.Shared/Data/Foo.cs b/src/BootstrapBlazor.Shared/Data/Foo.cs index a308cd6d0..d8594985d 100644 --- a/src/BootstrapBlazor.Shared/Data/Foo.cs +++ b/src/BootstrapBlazor.Shared/Data/Foo.cs @@ -78,6 +78,15 @@ public class Foo [AutoGenerateColumn(Order = 70, Editable = false)] public IEnumerable Hobby { get; set; } = new List(); + + /// + /// + /// + [Required(ErrorMessage = "请选择{0}")] + [Display(Name = "性别")] + [AutoGenerateColumn(Order = 80, LookUpServiceCatalog = "user_sex", ComponentType = typeof(Select))] + public int Sex { get; set; } = 0; + #region Static methods private static readonly Random random = new(); diff --git a/src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs b/src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs new file mode 100644 index 000000000..2efcc3d6e --- /dev/null +++ b/src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs @@ -0,0 +1,61 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Components; +using BootstrapBlazor.Shared.Components; +using Microsoft.Extensions.Localization; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Threading.Tasks; + +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// BootstrapBlazor 服务扩展类 + /// + public static class DemoLookUpServiceCollectionExtensions + { + /// + /// 增加 演示数据库操作服务 + /// + /// + /// + public static IServiceCollection AddDemoLookUpService(this IServiceCollection services) + { + services.AddScoped(typeof(ILookUpService), typeof(DemoLookUpService)); + return services; + } + } + + /// + /// 演示网站示例数据注入服务实现类 + /// + internal class DemoLookUpService : ILookUpService + { + + //private IStringLocalizer Localizer { get; set; } + public IEnumerable? GetLookUpByCatalog(string Catalog) + { + if(Catalog=="user_sex") + { + return new List() + { + new SelectedItem(){ Value="0",Text="未知"}, + new SelectedItem(){ Value="1",Text="男"}, + new SelectedItem(){ Value="2",Text="女"}, + }; + } + if (Catalog == "show_hide") + { + return new List() + { + new SelectedItem(){ Value="0",Text="显示"}, + new SelectedItem(){ Value="1",Text="隐藏"}, + }; + } + return null; + } + } +} diff --git a/src/BootstrapBlazor.Shared/Extensions/ServicesExtensions.cs b/src/BootstrapBlazor.Shared/Extensions/ServicesExtensions.cs index 0d0d21b78..0c62d69cf 100644 --- a/src/BootstrapBlazor.Shared/Extensions/ServicesExtensions.cs +++ b/src/BootstrapBlazor.Shared/Extensions/ServicesExtensions.cs @@ -26,6 +26,7 @@ public static class ServicesExtensions services.AddSingleton(); services.AddSingleton, ConfigureOptions>(); services.AddSingleton(typeof(IDataService<>), typeof(TableDemoDataService<>)); + services.AddSingleton(typeof(ILookUpService), typeof(DemoLookUpService)); // 增加模拟登录服务 services.AddScoped(); diff --git a/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs b/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs index 12716feba..90d8f70d5 100644 --- a/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs +++ b/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs @@ -173,6 +173,12 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu /// IEnumerable? IEditorItem.Lookup { get; set; } + /// + /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 + /// + public string? LookUpServiceCatalog { get; set; } + string? IEditorItem.LookUpServiceCatalog { get { return LookUpServiceCatalog; } set { value = LookUpServiceCatalog; } } + /// /// 获得/设置 单元格回调方法 /// diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs index adc4dd3f3..969e481b6 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs @@ -121,6 +121,8 @@ public sealed partial class EditorForm : IShowLabel [NotNull] private IStringLocalizer>? Localizer { get; set; } + [Inject] + private ILookUpService? LookUpService { get; set; } /// /// 获得/设置 配置编辑项目集合 /// @@ -225,6 +227,7 @@ public sealed partial class EditorForm : IShowLabel item.Text = el.Text; item.Items = el.Items; item.Lookup = el.Lookup; + item.LookUpServiceCatalog = el.LookUpServiceCatalog; item.ComponentType = el.ComponentType; item.ComponentParameters = el.ComponentParameters; item.SkipValidate = el.SkipValidate; @@ -252,7 +255,7 @@ public sealed partial class EditorForm : IShowLabel else { item.PlaceHolder ??= PlaceHolderText; - builder.CreateComponentByFieldType(this, item, Model, ShowLabel, ItemChangedType, IsSearch.Value); + builder.CreateComponentByFieldType(this, item, Model, ShowLabel, ItemChangedType, IsSearch.Value, LookUpService); } bool CanWrite(IEditorItem item) => item.CanWrite(typeof(TModel)) && item.IsEditable(ItemChangedType, IsSearch.Value); diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs index 0fddc9e4c..767c8eb26 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs @@ -149,6 +149,12 @@ public class EditorItem : ComponentBase, IEditorItem [Parameter] public IEnumerable? Lookup { get; set; } + /// + /// 获得/设置 字典数据源服务的类别参数 常用于外键自动转换为名称操作 + /// + [Parameter] + public string? LookUpServiceCatalog { get; set; } + /// /// 获得/设置 自定义验证集合 /// diff --git a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs index 81aca1dad..e8b7e47f3 100644 --- a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs @@ -87,6 +87,11 @@ public interface IEditorItem /// IEnumerable? Lookup { get; set; } + /// + /// 获得/设置 字典数据源服务的类别参数 + /// + string? LookUpServiceCatalog { get; set; } + /// /// 获得/设置 自定义验证集合 /// diff --git a/src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs b/src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs new file mode 100644 index 000000000..d92e522fc --- /dev/null +++ b/src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs @@ -0,0 +1,30 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using System.Collections.Generic; + +namespace BootstrapBlazor.Components +{ + /// + /// 获取Select的Items的接口 + /// + public interface ILookUpService + { + /// + /// 根据分类获取列表 + /// + IEnumerable? GetLookUpByCatalog(string Catalog); + } + + /// + /// 内部默认字典服务实现类 + /// + internal class NullLookUpService : ILookUpService + { + public IEnumerable? GetLookUpByCatalog(string Catalog) + { + throw new System.NotImplementedException(); + } + } +} diff --git a/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs b/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs index 3e6a36334..25dde7cce 100644 --- a/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs +++ b/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs @@ -116,6 +116,11 @@ internal class InternalTableColumn : ITableColumn /// public IEnumerable? Lookup { get; set; } + /// + /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 + /// + public string? LookUpServiceCatalog { get; set; } + /// /// 获得/设置 单元格回调方法 /// @@ -251,6 +256,7 @@ internal class InternalTableColumn : ITableColumn if (source.HeaderTemplate != null) dest.HeaderTemplate = source.HeaderTemplate; if (source.Items != null) dest.Items = source.Items; if (source.Lookup != null) dest.Lookup = source.Lookup; + if (source.LookUpServiceCatalog != null) dest.LookUpServiceCatalog = source.LookUpServiceCatalog; if (source.IsReadonlyWhenAdd) dest.IsReadonlyWhenAdd = source.IsReadonlyWhenAdd; if (source.IsReadonlyWhenEdit) dest.IsReadonlyWhenEdit = source.IsReadonlyWhenEdit; if (source.OnCellRender != null) dest.OnCellRender = source.OnCellRender; diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 4458bf5b0..c62783740 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -266,6 +266,8 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable [NotNull] private string? NotSetOnTreeExpandErrorMessage { get; set; } + [Inject] + private ILookUpService? LookUpService { get; set; } private bool ShowDetails() => IsDetails == null ? DetailRowTemplate != null : IsDetails.Value && DetailRowTemplate != null; @@ -947,6 +949,10 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable var content = ""; var val = GetItemValue(col.GetFieldName(), item); + if (col.LookUpServiceCatalog != null && col.Lookup == null) + { + col.Lookup = LookUpService?.GetLookUpByCatalog(col.LookUpServiceCatalog); + } if (col.Lookup == null && val is bool v1) { // 自动化处理 bool 值 @@ -1027,7 +1033,7 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable private RenderFragment RenderCell(ITableColumn col, TItem item, ItemChangedType changedType) => col.CanWrite(typeof(TItem)) && col.IsEditable(changedType) ? (col.EditTemplate == null - ? builder => builder.CreateComponentByFieldType(this, col, item, false, changedType) + ? builder => builder.CreateComponentByFieldType(this, col, item, false, changedType,false,LookUpService) : col.EditTemplate(item)) : (col.Template == null ? builder => builder.CreateDisplayByFieldType(this, col, item, false) @@ -1063,7 +1069,7 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable parameters.Add(new(nameof(ValidateBase.OnValueChanged), onValueChanged.Invoke(d, col, (model, column, val) => DynamicContext.OnValueChanged(model, column, val)))); col.ComponentParameters = parameters; } - builder.CreateComponentByFieldType(this, col, row, false, changedType); + builder.CreateComponentByFieldType(this, col, row, false, changedType,false, LookUpService); }; } diff --git a/src/BootstrapBlazor/Components/Table/TableColumn.cs b/src/BootstrapBlazor/Components/Table/TableColumn.cs index e9a7b673f..39d259255 100644 --- a/src/BootstrapBlazor/Components/Table/TableColumn.cs +++ b/src/BootstrapBlazor/Components/Table/TableColumn.cs @@ -341,6 +341,12 @@ public class TableColumn : BootstrapComponentBase, ITableColumn [Parameter] public IEnumerable? Lookup { get; set; } + /// + /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 + /// + [Parameter] + public string? LookUpServiceCatalog { get; set; } + /// /// 获得/设置 单元格回调方法 /// diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs index 8c1a8c3fc..511277f7b 100644 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs +++ b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs @@ -35,6 +35,7 @@ public static class BootstrapBlazorServiceCollectionExtensions services.TryAddSingleton(typeof(IDispatchService<>), typeof(DefaultDispatchService<>)); services.TryAddScoped(); services.TryAddScoped(typeof(IDataService<>), typeof(NullDataService<>)); + services.TryAddSingleton(typeof(ILookUpService), typeof(NullLookUpService)); services.TryAddScoped(); services.TryAddScoped(); diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index 091a5e07e..a9812d573 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -283,7 +283,7 @@ public static class Utility /// /// /// - public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, bool? showLabel = null, ItemChangedType changedType = ItemChangedType.Update, bool isSearch = false) + public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, bool? showLabel = null, ItemChangedType changedType = ItemChangedType.Update, bool isSearch = false, ILookUpService? lookUpService = null) { var fieldType = item.PropertyType; var fieldName = item.GetFieldName(); @@ -317,7 +317,15 @@ public static class Utility { builder.AddAttribute(7, nameof(CheckboxList>.Items), item.Items.Clone()); } - + // 增加字典服务类型的下拉框,手动设定 ComponentType 为 Select 并且 Data 有值 自动生成下拉框 + if (item.Items == null && item.ComponentType == typeof(Select<>).MakeGenericType(fieldType) + && (item.LookUpServiceCatalog != null || item.Lookup != null)) + { + if (item.Lookup == null && item.LookUpServiceCatalog != null && lookUpService != null) + { + item.Lookup = lookUpService?.GetLookUpByCatalog(item.LookUpServiceCatalog); + } + } // Lookup if (lookup != null && item.Items == null) { -- Gitee From 78946575050f0d49d19dfe57c0adf1995ad023f1 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 13:19:25 +0800 Subject: [PATCH 02/12] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Attributes/AutoGenerateColumnAttribute.cs | 2 +- .../Components/EditorForm/EditorForm.razor.cs | 2 +- .../Components/EditorForm/EditorItem.cs | 2 +- .../Components/EditorForm/IEditorItem.cs | 4 +-- .../Components/Table/InternalTableColumn.cs | 4 +-- .../Components/Table/Table.razor.cs | 17 ++++++---- .../Components/Table/TableColumn.cs | 2 +- ...tstrapBlazorServiceCollectionExtensions.cs | 3 +- .../EmptyLookUpService.cs} | 31 ++++++------------- .../Services/ILookUpService.cs | 17 ++++++++++ src/BootstrapBlazor/Utils/Utility.cs | 10 +++--- 11 files changed, 54 insertions(+), 40 deletions(-) rename src/BootstrapBlazor/{Components/EditorForm/ILookUpService.cs => Services/EmptyLookUpService.cs} (32%) create mode 100644 src/BootstrapBlazor/Services/ILookUpService.cs diff --git a/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs b/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs index 90d8f70d5..7da3319e5 100644 --- a/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs +++ b/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs @@ -177,7 +177,7 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 /// public string? LookUpServiceCatalog { get; set; } - string? IEditorItem.LookUpServiceCatalog { get { return LookUpServiceCatalog; } set { value = LookUpServiceCatalog; } } + string? IEditorItem.LookUpServiceKey { get { return LookUpServiceCatalog; } set { value = LookUpServiceCatalog; } } /// /// 获得/设置 单元格回调方法 diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs index 969e481b6..aa10e40f8 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs @@ -227,7 +227,7 @@ public sealed partial class EditorForm : IShowLabel item.Text = el.Text; item.Items = el.Items; item.Lookup = el.Lookup; - item.LookUpServiceCatalog = el.LookUpServiceCatalog; + item.LookUpServiceKey = el.LookUpServiceKey; item.ComponentType = el.ComponentType; item.ComponentParameters = el.ComponentParameters; item.SkipValidate = el.SkipValidate; diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs index 767c8eb26..6d9b6d6e9 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs @@ -153,7 +153,7 @@ public class EditorItem : ComponentBase, IEditorItem /// 获得/设置 字典数据源服务的类别参数 常用于外键自动转换为名称操作 /// [Parameter] - public string? LookUpServiceCatalog { get; set; } + public string? LookUpServiceKey { get; set; } /// /// 获得/设置 自定义验证集合 diff --git a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs index e8b7e47f3..9e277954c 100644 --- a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs @@ -88,9 +88,9 @@ public interface IEditorItem IEnumerable? Lookup { get; set; } /// - /// 获得/设置 字典数据源服务的类别参数 + /// 获得/设置 LookupService 服务获取 Lookup 数据集合键值 /// - string? LookUpServiceCatalog { get; set; } + string? LookUpServiceKey { get; set; } /// /// 获得/设置 自定义验证集合 diff --git a/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs b/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs index 25dde7cce..5d5b71171 100644 --- a/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs +++ b/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs @@ -119,7 +119,7 @@ internal class InternalTableColumn : ITableColumn /// /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 /// - public string? LookUpServiceCatalog { get; set; } + public string? LookUpServiceKey { get; set; } /// /// 获得/设置 单元格回调方法 @@ -256,7 +256,7 @@ internal class InternalTableColumn : ITableColumn if (source.HeaderTemplate != null) dest.HeaderTemplate = source.HeaderTemplate; if (source.Items != null) dest.Items = source.Items; if (source.Lookup != null) dest.Lookup = source.Lookup; - if (source.LookUpServiceCatalog != null) dest.LookUpServiceCatalog = source.LookUpServiceCatalog; + if (source.LookUpServiceKey != null) dest.LookUpServiceKey = source.LookUpServiceKey; if (source.IsReadonlyWhenAdd) dest.IsReadonlyWhenAdd = source.IsReadonlyWhenAdd; if (source.IsReadonlyWhenEdit) dest.IsReadonlyWhenEdit = source.IsReadonlyWhenEdit; if (source.OnCellRender != null) dest.OnCellRender = source.OnCellRender; diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index c62783740..7fc71a80d 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -263,11 +263,13 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable [NotNull] private IOptions? Options { get; set; } + [Inject] + [NotNull] + private ILookUpService? LookUpService { get; set; } + [NotNull] private string? NotSetOnTreeExpandErrorMessage { get; set; } - [Inject] - private ILookUpService? LookUpService { get; set; } private bool ShowDetails() => IsDetails == null ? DetailRowTemplate != null : IsDetails.Value && DetailRowTemplate != null; @@ -949,10 +951,13 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable var content = ""; var val = GetItemValue(col.GetFieldName(), item); - if (col.LookUpServiceCatalog != null && col.Lookup == null) + if (col.Lookup == null && !string.IsNullOrEmpty(col.LookUpServiceKey)) { - col.Lookup = LookUpService?.GetLookUpByCatalog(col.LookUpServiceCatalog); + // 未设置 Lookup + // 设置 LookupService 键值 + col.Lookup = LookUpService.GetItemsByKey(col.LookUpServiceKey); } + if (col.Lookup == null && val is bool v1) { // 自动化处理 bool 值 @@ -1033,7 +1038,7 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable private RenderFragment RenderCell(ITableColumn col, TItem item, ItemChangedType changedType) => col.CanWrite(typeof(TItem)) && col.IsEditable(changedType) ? (col.EditTemplate == null - ? builder => builder.CreateComponentByFieldType(this, col, item, false, changedType,false,LookUpService) + ? builder => builder.CreateComponentByFieldType(this, col, item, false, changedType, false, LookUpService) : col.EditTemplate(item)) : (col.Template == null ? builder => builder.CreateDisplayByFieldType(this, col, item, false) @@ -1069,7 +1074,7 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable parameters.Add(new(nameof(ValidateBase.OnValueChanged), onValueChanged.Invoke(d, col, (model, column, val) => DynamicContext.OnValueChanged(model, column, val)))); col.ComponentParameters = parameters; } - builder.CreateComponentByFieldType(this, col, row, false, changedType,false, LookUpService); + builder.CreateComponentByFieldType(this, col, row, false, changedType, false, LookUpService); }; } diff --git a/src/BootstrapBlazor/Components/Table/TableColumn.cs b/src/BootstrapBlazor/Components/Table/TableColumn.cs index 39d259255..6363f499f 100644 --- a/src/BootstrapBlazor/Components/Table/TableColumn.cs +++ b/src/BootstrapBlazor/Components/Table/TableColumn.cs @@ -345,7 +345,7 @@ public class TableColumn : BootstrapComponentBase, ITableColumn /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 /// [Parameter] - public string? LookUpServiceCatalog { get; set; } + public string? LookUpServiceKey { get; set; } /// /// 获得/设置 单元格回调方法 diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs index 511277f7b..51103c6ae 100644 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs +++ b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs @@ -4,6 +4,7 @@ using BootstrapBlazor.Components; using BootstrapBlazor.Localization.Json; +using BootstrapBlazor.Services; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using System.Globalization; @@ -33,9 +34,9 @@ public static class BootstrapBlazorServiceCollectionExtensions services.TryAddSingleton(); services.TryAddSingleton(typeof(IDispatchService<>), typeof(DefaultDispatchService<>)); + services.TryAddSingleton(typeof(ILookUpService), typeof(EmptyLookUpService)); services.TryAddScoped(); services.TryAddScoped(typeof(IDataService<>), typeof(NullDataService<>)); - services.TryAddSingleton(typeof(ILookUpService), typeof(NullLookUpService)); services.TryAddScoped(); services.TryAddScoped(); diff --git a/src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs b/src/BootstrapBlazor/Services/EmptyLookUpService.cs similarity index 32% rename from src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs rename to src/BootstrapBlazor/Services/EmptyLookUpService.cs index d92e522fc..754ca3b73 100644 --- a/src/BootstrapBlazor/Components/EditorForm/ILookUpService.cs +++ b/src/BootstrapBlazor/Services/EmptyLookUpService.cs @@ -2,29 +2,18 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ -using System.Collections.Generic; +using BootstrapBlazor.Components; -namespace BootstrapBlazor.Components -{ - /// - /// 获取Select的Items的接口 - /// - public interface ILookUpService - { - /// - /// 根据分类获取列表 - /// - IEnumerable? GetLookUpByCatalog(string Catalog); - } +namespace BootstrapBlazor.Services; +/// +/// 内部默认字典服务实现类 +/// +internal class EmptyLookUpService : ILookUpService +{ /// - /// 内部默认字典服务实现类 + /// 根据指定键值获取 Lookup 集合方法 /// - internal class NullLookUpService : ILookUpService - { - public IEnumerable? GetLookUpByCatalog(string Catalog) - { - throw new System.NotImplementedException(); - } - } + /// 获得 Lookup 数据集合键值 + public IEnumerable? GetItemsByKey(string key) => Enumerable.Empty(); } diff --git a/src/BootstrapBlazor/Services/ILookUpService.cs b/src/BootstrapBlazor/Services/ILookUpService.cs new file mode 100644 index 000000000..35fef39f0 --- /dev/null +++ b/src/BootstrapBlazor/Services/ILookUpService.cs @@ -0,0 +1,17 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Components; + +/// +/// 获取 Select 的 Items 的接口 +/// +public interface ILookUpService +{ + /// + /// 根据指定键值获取 Lookup 集合方法 + /// + /// 获得 Lookup 数据集合键值 + IEnumerable? GetItemsByKey(string key); +} diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index a9812d573..383bab962 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -283,6 +283,7 @@ public static class Utility /// /// /// + /// public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, bool? showLabel = null, ItemChangedType changedType = ItemChangedType.Update, bool isSearch = false, ILookUpService? lookUpService = null) { var fieldType = item.PropertyType; @@ -317,15 +318,16 @@ public static class Utility { builder.AddAttribute(7, nameof(CheckboxList>.Items), item.Items.Clone()); } + // 增加字典服务类型的下拉框,手动设定 ComponentType 为 Select 并且 Data 有值 自动生成下拉框 - if (item.Items == null && item.ComponentType == typeof(Select<>).MakeGenericType(fieldType) - && (item.LookUpServiceCatalog != null || item.Lookup != null)) + if (item.Items == null && item.ComponentType == typeof(Select<>).MakeGenericType(fieldType)) { - if (item.Lookup == null && item.LookUpServiceCatalog != null && lookUpService != null) + if (item.Lookup == null && lookUpService != null && !string.IsNullOrEmpty(item.LookUpServiceKey)) { - item.Lookup = lookUpService?.GetLookUpByCatalog(item.LookUpServiceCatalog); + item.Lookup = lookUpService.GetItemsByKey(item.LookUpServiceKey); } } + // Lookup if (lookup != null && item.Items == null) { -- Gitee From 4b2065ad8f0215fdfbe7ee604125dcfdb586a70b Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 13:24:34 +0800 Subject: [PATCH 03/12] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=20EmptyLoo?= =?UTF-8?q?kupService=20=E4=B8=BA=20NullLookupService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/BootstrapBlazorServiceCollectionExtensions.cs | 2 +- .../Services/{EmptyLookUpService.cs => NullLookUpService.cs} | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/BootstrapBlazor/Services/{EmptyLookUpService.cs => NullLookUpService.cs} (87%) diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs index 51103c6ae..0a97c6066 100644 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs +++ b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs @@ -34,7 +34,7 @@ public static class BootstrapBlazorServiceCollectionExtensions services.TryAddSingleton(); services.TryAddSingleton(typeof(IDispatchService<>), typeof(DefaultDispatchService<>)); - services.TryAddSingleton(typeof(ILookUpService), typeof(EmptyLookUpService)); + services.TryAddSingleton(typeof(ILookUpService), typeof(NullLookUpService)); services.TryAddScoped(); services.TryAddScoped(typeof(IDataService<>), typeof(NullDataService<>)); services.TryAddScoped(); diff --git a/src/BootstrapBlazor/Services/EmptyLookUpService.cs b/src/BootstrapBlazor/Services/NullLookUpService.cs similarity index 87% rename from src/BootstrapBlazor/Services/EmptyLookUpService.cs rename to src/BootstrapBlazor/Services/NullLookUpService.cs index 754ca3b73..333d9c14f 100644 --- a/src/BootstrapBlazor/Services/EmptyLookUpService.cs +++ b/src/BootstrapBlazor/Services/NullLookUpService.cs @@ -9,11 +9,11 @@ namespace BootstrapBlazor.Services; /// /// 内部默认字典服务实现类 /// -internal class EmptyLookUpService : ILookUpService +internal class NullLookUpService : ILookUpService { /// /// 根据指定键值获取 Lookup 集合方法 /// /// 获得 Lookup 数据集合键值 - public IEnumerable? GetItemsByKey(string key) => Enumerable.Empty(); + public IEnumerable? GetItemsByKey(string key) => null; } -- Gitee From 417dd6dc5919c8102688abaed18d6ea054bfab12 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 13:25:57 +0800 Subject: [PATCH 04/12] =?UTF-8?q?refactor:=20LookupService=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=A9=BA=E9=94=AE=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/ILookUpService.cs | 2 +- src/BootstrapBlazor/Services/NullLookUpService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Services/ILookUpService.cs b/src/BootstrapBlazor/Services/ILookUpService.cs index 35fef39f0..6a3b1a2ea 100644 --- a/src/BootstrapBlazor/Services/ILookUpService.cs +++ b/src/BootstrapBlazor/Services/ILookUpService.cs @@ -13,5 +13,5 @@ public interface ILookUpService /// 根据指定键值获取 Lookup 集合方法 /// /// 获得 Lookup 数据集合键值 - IEnumerable? GetItemsByKey(string key); + IEnumerable? GetItemsByKey(string? key); } diff --git a/src/BootstrapBlazor/Services/NullLookUpService.cs b/src/BootstrapBlazor/Services/NullLookUpService.cs index 333d9c14f..246354840 100644 --- a/src/BootstrapBlazor/Services/NullLookUpService.cs +++ b/src/BootstrapBlazor/Services/NullLookUpService.cs @@ -15,5 +15,5 @@ internal class NullLookUpService : ILookUpService /// 根据指定键值获取 Lookup 集合方法 /// /// 获得 Lookup 数据集合键值 - public IEnumerable? GetItemsByKey(string key) => null; + public IEnumerable? GetItemsByKey(string? key) => null; } -- Gitee From d0f65740180f93ef63dbec54cc1c94bd402fdaa8 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 13:34:37 +0800 Subject: [PATCH 05/12] =?UTF-8?q?feat:=20Display=20=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20LookupService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/EditorForm/EditorForm.razor.cs | 4 +++- .../Components/Table/Table.razor.cs | 2 +- src/BootstrapBlazor/Utils/Utility.cs | 24 +++++++------------ 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs index aa10e40f8..2a192dadd 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs @@ -122,7 +122,9 @@ public sealed partial class EditorForm : IShowLabel private IStringLocalizer>? Localizer { get; set; } [Inject] + [NotNull] private ILookUpService? LookUpService { get; set; } + /// /// 获得/设置 配置编辑项目集合 /// @@ -250,7 +252,7 @@ public sealed partial class EditorForm : IShowLabel { if (IsDisplay || !CanWrite(item)) { - builder.CreateDisplayByFieldType(this, item, Model, ShowLabel); + builder.CreateDisplayByFieldType(item, Model, ShowLabel, LookUpService); } else { diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 7fc71a80d..64dd6ad0d 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -1041,7 +1041,7 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable ? builder => builder.CreateComponentByFieldType(this, col, item, false, changedType, false, LookUpService) : col.EditTemplate(item)) : (col.Template == null - ? builder => builder.CreateDisplayByFieldType(this, col, item, false) + ? builder => builder.CreateDisplayByFieldType(col, item, false, LookUpService) : col.Template(item)); private RenderFragment RenderExcelCell(ITableColumn col, TItem item, ItemChangedType changedType) diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index 383bab962..f3b674713 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -241,11 +241,11 @@ public static class Utility /// /// /// - /// /// /// /// - public static void CreateDisplayByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, bool? showLabel = null) + /// + public static void CreateDisplayByFieldType(this RenderTreeBuilder builder, IEditorItem item, object model, bool? showLabel = null, ILookUpService? lookUpService = null) { var fieldType = item.PropertyType; var fieldName = item.GetFieldName(); @@ -265,10 +265,11 @@ public static class Utility else { builder.OpenComponent(0, typeof(Display<>).MakeGenericType(fieldType)); - builder.AddAttribute(1, nameof(ValidateBase.DisplayText), displayName); - builder.AddAttribute(2, nameof(ValidateBase.Value), fieldValue); - builder.AddAttribute(4, nameof(ValidateBase.ValueExpression), valueExpression); - builder.AddAttribute(5, nameof(ValidateBase.ShowLabel), showLabel ?? true); + builder.AddAttribute(1, nameof(Display.DisplayText), displayName); + builder.AddAttribute(2, nameof(Display.Value), fieldValue); + builder.AddAttribute(4, nameof(Display.ValueExpression), valueExpression); + builder.AddAttribute(5, nameof(Display.ShowLabel), showLabel ?? true); + builder.AddAttribute(6, nameof(Display.Lookup), lookUpService?.GetItemsByKey(item.LookUpServiceKey)); builder.CloseComponent(); } } @@ -293,7 +294,7 @@ public static class Utility var fieldValue = GenerateValue(model, fieldName); var fieldValueChanged = GenerateValueChanged(component, model, fieldName, fieldType); var valueExpression = GenerateValueExpression(model, fieldName, fieldType); - var lookup = item is ITableColumn col ? col.Lookup : null; + var lookup = item is ITableColumn col ? col.Lookup : lookUpService?.GetItemsByKey(item.LookUpServiceKey); var componentType = item.ComponentType ?? GenerateComponentType(fieldType, item.Rows != 0, lookup); builder.OpenComponent(0, componentType); if (componentType.IsSubclassOf(typeof(ValidateBase<>).MakeGenericType(fieldType))) @@ -319,15 +320,6 @@ public static class Utility builder.AddAttribute(7, nameof(CheckboxList>.Items), item.Items.Clone()); } - // 增加字典服务类型的下拉框,手动设定 ComponentType 为 Select 并且 Data 有值 自动生成下拉框 - if (item.Items == null && item.ComponentType == typeof(Select<>).MakeGenericType(fieldType)) - { - if (item.Lookup == null && lookUpService != null && !string.IsNullOrEmpty(item.LookUpServiceKey)) - { - item.Lookup = lookUpService.GetItemsByKey(item.LookUpServiceKey); - } - } - // Lookup if (lookup != null && item.Items == null) { -- Gitee From b812a2661b36b28a00e9743dfbffb76fe3c7ebce Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 13:40:20 +0800 Subject: [PATCH 06/12] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=98=BE=E5=BC=8F=E5=AE=9E=E7=8E=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Attributes/AutoGenerateColumnAttribute.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs b/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs index 7da3319e5..254def154 100644 --- a/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs +++ b/src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs @@ -174,10 +174,9 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu IEnumerable? IEditorItem.Lookup { get; set; } /// - /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 + /// 获得/设置 LookupService 服务指定数据集合键值 常用于外键自动转换为名称操作 /// - public string? LookUpServiceCatalog { get; set; } - string? IEditorItem.LookUpServiceKey { get { return LookUpServiceCatalog; } set { value = LookUpServiceCatalog; } } + public string? LookUpServiceKey { get; set; } /// /// 获得/设置 单元格回调方法 -- Gitee From 09d49d3c980dc8802261be4e6a2cba737d409263 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 14:26:15 +0800 Subject: [PATCH 07/12] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=20Lookup=20=E6=9C=8D=E5=8A=A1=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Shared/Data/Foo.cs | 9 --- .../DemoLookUpServiceCollectionExtensions.cs | 61 ------------------- .../Services/DemoLookUpService.cs | 35 +++++++++++ 3 files changed, 35 insertions(+), 70 deletions(-) delete mode 100644 src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs create mode 100644 src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs diff --git a/src/BootstrapBlazor.Shared/Data/Foo.cs b/src/BootstrapBlazor.Shared/Data/Foo.cs index d8594985d..a308cd6d0 100644 --- a/src/BootstrapBlazor.Shared/Data/Foo.cs +++ b/src/BootstrapBlazor.Shared/Data/Foo.cs @@ -78,15 +78,6 @@ public class Foo [AutoGenerateColumn(Order = 70, Editable = false)] public IEnumerable Hobby { get; set; } = new List(); - - /// - /// - /// - [Required(ErrorMessage = "请选择{0}")] - [Display(Name = "性别")] - [AutoGenerateColumn(Order = 80, LookUpServiceCatalog = "user_sex", ComponentType = typeof(Select))] - public int Sex { get; set; } = 0; - #region Static methods private static readonly Random random = new(); diff --git a/src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs b/src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs deleted file mode 100644 index 2efcc3d6e..000000000 --- a/src/BootstrapBlazor.Shared/Extensions/DemoLookUpServiceCollectionExtensions.cs +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -// Website: https://www.blazor.zone or https://argozhang.github.io/ - -using BootstrapBlazor.Components; -using BootstrapBlazor.Shared.Components; -using Microsoft.Extensions.Localization; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Threading.Tasks; - -namespace Microsoft.Extensions.DependencyInjection -{ - /// - /// BootstrapBlazor 服务扩展类 - /// - public static class DemoLookUpServiceCollectionExtensions - { - /// - /// 增加 演示数据库操作服务 - /// - /// - /// - public static IServiceCollection AddDemoLookUpService(this IServiceCollection services) - { - services.AddScoped(typeof(ILookUpService), typeof(DemoLookUpService)); - return services; - } - } - - /// - /// 演示网站示例数据注入服务实现类 - /// - internal class DemoLookUpService : ILookUpService - { - - //private IStringLocalizer Localizer { get; set; } - public IEnumerable? GetLookUpByCatalog(string Catalog) - { - if(Catalog=="user_sex") - { - return new List() - { - new SelectedItem(){ Value="0",Text="未知"}, - new SelectedItem(){ Value="1",Text="男"}, - new SelectedItem(){ Value="2",Text="女"}, - }; - } - if (Catalog == "show_hide") - { - return new List() - { - new SelectedItem(){ Value="0",Text="显示"}, - new SelectedItem(){ Value="1",Text="隐藏"}, - }; - } - return null; - } - } -} diff --git a/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs b/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs new file mode 100644 index 000000000..a3c7d0446 --- /dev/null +++ b/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs @@ -0,0 +1,35 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Components; + +namespace Microsoft.Extensions.DependencyInjection; + +/// +/// 演示网站示例数据注入服务实现类 +/// +internal class DemoLookUpService : ILookUpService +{ + public IEnumerable? GetItemsByKey(string? key) + { + if (key == "user_sex") + { + return new List() + { + new SelectedItem(){ Value="0",Text="未知"}, + new SelectedItem(){ Value="1",Text="男"}, + new SelectedItem(){ Value="2",Text="女"}, + }; + } + if (key == "show_hide") + { + return new List() + { + new SelectedItem(){ Value="0",Text="显示"}, + new SelectedItem(){ Value="1",Text="隐藏"}, + }; + } + return null; + } +} -- Gitee From 53dcdadb1631a90730d0ea8d3ebff9f41dbece49 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 14:26:26 +0800 Subject: [PATCH 08/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/EditorForm/EditorItem.cs | 2 +- src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs | 2 +- src/BootstrapBlazor/Components/Table/InternalTableColumn.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs index 6d9b6d6e9..b3945fb23 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorItem.cs @@ -150,7 +150,7 @@ public class EditorItem : ComponentBase, IEditorItem public IEnumerable? Lookup { get; set; } /// - /// 获得/设置 字典数据源服务的类别参数 常用于外键自动转换为名称操作 + /// 获得/设置 LookupService 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作 /// [Parameter] public string? LookUpServiceKey { get; set; } diff --git a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs index 9e277954c..305934f13 100644 --- a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs @@ -88,7 +88,7 @@ public interface IEditorItem IEnumerable? Lookup { get; set; } /// - /// 获得/设置 LookupService 服务获取 Lookup 数据集合键值 + /// 获得/设置 LookupService 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作 /// string? LookUpServiceKey { get; set; } diff --git a/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs b/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs index 5d5b71171..ae99bdcd6 100644 --- a/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs +++ b/src/BootstrapBlazor/Components/Table/InternalTableColumn.cs @@ -117,7 +117,7 @@ internal class InternalTableColumn : ITableColumn public IEnumerable? Lookup { get; set; } /// - /// 获得/设置 字典数据源服务的类别 常用于外键自动转换为名称操作 + /// 获得/设置 LookupService 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作 /// public string? LookUpServiceKey { get; set; } -- Gitee From e4b268400623536b59fb225097d6a8c492e0303b Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 14:38:01 +0800 Subject: [PATCH 09/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/Table/TablesLookup.razor | 2 +- .../Samples/Table/TablesLookup.razor.cs | 9 ------- .../Services/DemoLookUpService.cs | 27 +++++++++---------- 3 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor b/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor index bb858ef07..fb7962f20 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor @@ -19,7 +19,7 @@ - + } diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor.cs b/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor.cs index 5ca80ca56..082959eaf 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor.cs @@ -16,9 +16,6 @@ public partial class TablesLookup [NotNull] private List? Items { get; set; } - [NotNull] - private List? DataSource { get; set; } - [Inject] [NotNull] private IStringLocalizer? Localizer { get; set; } @@ -33,11 +30,5 @@ public partial class TablesLookup await Task.Delay(200); Items = Foo.GenerateFoo(Localizer); - - DataSource = new List - { - new SelectedItem{ Value = "true", Text = Localizer["True"].Value }, - new SelectedItem{ Value = "false", Text = Localizer["False"].Value } - }; } } diff --git a/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs b/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs index a3c7d0446..54618f646 100644 --- a/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs +++ b/src/BootstrapBlazor.Shared/Services/DemoLookUpService.cs @@ -3,6 +3,8 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using BootstrapBlazor.Components; +using BootstrapBlazor.Shared; +using Microsoft.Extensions.Localization; namespace Microsoft.Extensions.DependencyInjection; @@ -11,25 +13,22 @@ namespace Microsoft.Extensions.DependencyInjection; /// internal class DemoLookUpService : ILookUpService { + private IServiceProvider Provider { get; } + + public DemoLookUpService(IServiceProvider provider) => Provider = provider; + public IEnumerable? GetItemsByKey(string? key) { - if (key == "user_sex") - { - return new List() - { - new SelectedItem(){ Value="0",Text="未知"}, - new SelectedItem(){ Value="1",Text="男"}, - new SelectedItem(){ Value="2",Text="女"}, - }; - } - if (key == "show_hide") + IEnumerable? items = null; + if (key == "Foo.Complete") { - return new List() + var localizer = Provider.GetRequiredService>(); + items = new List() { - new SelectedItem(){ Value="0",Text="显示"}, - new SelectedItem(){ Value="1",Text="隐藏"}, + new() { Value = "true", Text = localizer["True"].Value }, + new() { Value = "false", Text = localizer["False"].Value } }; } - return null; + return items; } } -- Gitee From d1cb8ebdb611488fc3f4fdf653c1437b29938f3d Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 14:49:53 +0800 Subject: [PATCH 10/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor b/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor index fb7962f20..a08a33f11 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor +++ b/src/BootstrapBlazor.Shared/Samples/Table/TablesLookup.razor @@ -13,6 +13,7 @@ };

组件自动将 Complete 转换为 DataSource 集合中的预设值

+

本例中使用 ILookupService 服务进行统一处理,通过设置 LookupServiceKey 值,从服务中获取 Lookup 数据集合

@if (Items != null) { -- Gitee From 5282e7cbea7ef32945b37f308f1a57abde0e61ad Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 14:50:04 +0800 Subject: [PATCH 11/12] chore: bump version 6.4.4 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index e6a0e0925..441491aa4 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.4.3-beta04 + 6.4.4 -- Gitee From dfac7fdf8b5488cc9f30461a321ddba13812fed6 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 14 Mar 2022 14:52:59 +0800 Subject: [PATCH 12/12] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=20TableColumn?= =?UTF-8?q?=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Samples/Table/Tables.razor.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/BootstrapBlazor.Shared/Samples/Table/Tables.razor.cs b/src/BootstrapBlazor.Shared/Samples/Table/Tables.razor.cs index f4690f0e7..aa6b17a39 100644 --- a/src/BootstrapBlazor.Shared/Samples/Table/Tables.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Table/Tables.razor.cs @@ -103,6 +103,20 @@ public partial class Tables ValueList = "true|false", DefaultValue = "false" }, + new() { + Name = nameof(IEditorItem.Lookup), + Description = "字典数据源", + Type = "IEnumerable", + ValueList = " — ", + DefaultValue = " — " + }, + new() { + Name = nameof(IEditorItem.LookUpServiceKey), + Description = "LookupService 服务获取 Lookup 数据集合键值", + Type = "string", + ValueList = " — ", + DefaultValue = " — " + }, new() { Name = "Readonly", Description = "编辑时是否只读模式", -- Gitee