diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index daa5abe234f..0938f1d71c8 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 9.1.9-beta03 + 9.1.9-beta04 diff --git a/src/BootstrapBlazor/Components/Table/LookupContent.cs b/src/BootstrapBlazor/Components/Table/LookupContent.cs new file mode 100644 index 00000000000..dfc18ea87cb --- /dev/null +++ b/src/BootstrapBlazor/Components/Table/LookupContent.cs @@ -0,0 +1,83 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using Microsoft.AspNetCore.Components.Rendering; +using System.ComponentModel; + +namespace BootstrapBlazor.Components; + +internal class LookupContent : ComponentBase +{ + /// + /// 获得/设置 服务实例 + /// + [Parameter] + public ILookupService? LookupService { get; set; } + + /// + /// 获得/设置 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作,可以通过 传递自定义数据 + /// + [Parameter] + [EditorRequired] + public string? LookupServiceKey { get; set; } + + /// + /// 获得/设置 服务获取 Lookup 数据集合键值自定义数据,通过 指定键值 + /// + [Parameter] + public object? LookupServiceData { get; set; } + + /// + /// 获得/设置 字典数据源字符串比较规则 默认 大小写不敏感 + /// + [Parameter] + public StringComparison LookupStringComparison { get; set; } + + /// + /// 获得/设置 显示值 + /// + [Parameter] + public string? Value { get; set; } + + [Inject] + [NotNull] + private ILookupService? InjectLookupService { get; set; } + + private string? _content; + + private List? _items; + + /// + /// + /// + /// + protected override async Task OnParametersSetAsync() + { + await base.OnParametersSetAsync(); + + _items ??= await GetLookupItemsAsync(); + var item = _items.Find(i => i.Value.Equals(Value, LookupStringComparison)); + _content = item?.Text ?? Value; + } + + /// + /// + /// + /// + protected override void BuildRenderTree(RenderTreeBuilder builder) + { + if (!string.IsNullOrEmpty(_content)) + { + builder.AddContent(0, _content); + } + } + + private async Task> GetLookupItemsAsync() + { + var lookupService = LookupService ?? InjectLookupService; + var items = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData); + return items?.ToList() ?? []; + } +} diff --git a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs index 30a057208d1..2a475b92977 100644 --- a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs @@ -285,9 +285,19 @@ private static RenderFragment RenderTooltip(this ITableColumn col, string { pb.AddMarkupContent(20, text); } + else if (col.IsLookup()) + { + pb.OpenComponent(30); + pb.AddAttribute(31, nameof(LookupContent.LookupService), col.LookupService); + pb.AddAttribute(32, nameof(LookupContent.LookupServiceKey), col.LookupServiceKey); + pb.AddAttribute(33, nameof(LookupContent.LookupServiceData), col.LookupServiceData); + pb.AddAttribute(34, nameof(LookupContent.LookupStringComparison), col.LookupStringComparison); + pb.AddAttribute(35, nameof(LookupContent.Value), text); + pb.CloseComponent(); + } else { - pb.AddContent(30, text); + pb.AddContent(40, text); } };