Skip to content

Commit d651d08

Browse files
refactor(Table): table display value use LookupService (#4935)
* refactor: 增加 LookupContent 组件 * fix: 修复 Table 显示值未使用 LookupService 问题 * chore: bump version 9.1.9-beta04 Co-Authored-By: Silver <63774265+StevenBase@users.noreply.github.com> * chore: bump version 9.19-beta03 Co-Authored-By: Silver <63774265+StevenBase@users.noreply.github.com> --------- Co-authored-by: Silver <63774265+StevenBase@users.noreply.github.com>
1 parent 4059dc2 commit d651d08

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.1.9-beta03</Version>
4+
<Version>9.1.9-beta04</Version>
55
</PropertyGroup>
66

77
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
5+
6+
using Microsoft.AspNetCore.Components.Rendering;
7+
using System.ComponentModel;
8+
9+
namespace BootstrapBlazor.Components;
10+
11+
internal class LookupContent : ComponentBase
12+
{
13+
/// <summary>
14+
/// 获得/设置 <see cref="ILookupService"/> 服务实例
15+
/// </summary>
16+
[Parameter]
17+
public ILookupService? LookupService { get; set; }
18+
19+
/// <summary>
20+
/// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作,可以通过 <see cref="LookupServiceData"/> 传递自定义数据
21+
/// </summary>
22+
[Parameter]
23+
[EditorRequired]
24+
public string? LookupServiceKey { get; set; }
25+
26+
/// <summary>
27+
/// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值自定义数据,通过 <see cref="LookupServiceKey"/> 指定键值
28+
/// </summary>
29+
[Parameter]
30+
public object? LookupServiceData { get; set; }
31+
32+
/// <summary>
33+
/// 获得/设置 字典数据源字符串比较规则 默认 <see cref="StringComparison.OrdinalIgnoreCase" /> 大小写不敏感
34+
/// </summary>
35+
[Parameter]
36+
public StringComparison LookupStringComparison { get; set; }
37+
38+
/// <summary>
39+
/// 获得/设置 显示值
40+
/// </summary>
41+
[Parameter]
42+
public string? Value { get; set; }
43+
44+
[Inject]
45+
[NotNull]
46+
private ILookupService? InjectLookupService { get; set; }
47+
48+
private string? _content;
49+
50+
private List<SelectedItem>? _items;
51+
52+
/// <summary>
53+
/// <inheritdoc/>
54+
/// </summary>
55+
/// <returns></returns>
56+
protected override async Task OnParametersSetAsync()
57+
{
58+
await base.OnParametersSetAsync();
59+
60+
_items ??= await GetLookupItemsAsync();
61+
var item = _items.Find(i => i.Value.Equals(Value, LookupStringComparison));
62+
_content = item?.Text ?? Value;
63+
}
64+
65+
/// <summary>
66+
/// <inheritdoc/>
67+
/// </summary>
68+
/// <param name="builder"></param>
69+
protected override void BuildRenderTree(RenderTreeBuilder builder)
70+
{
71+
if (!string.IsNullOrEmpty(_content))
72+
{
73+
builder.AddContent(0, _content);
74+
}
75+
}
76+
77+
private async Task<List<SelectedItem>> GetLookupItemsAsync()
78+
{
79+
var lookupService = LookupService ?? InjectLookupService;
80+
var items = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData);
81+
return items?.ToList() ?? [];
82+
}
83+
}

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,19 @@ private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string
285285
{
286286
pb.AddMarkupContent(20, text);
287287
}
288+
else if (col.IsLookup())
289+
{
290+
pb.OpenComponent<LookupContent>(30);
291+
pb.AddAttribute(31, nameof(LookupContent.LookupService), col.LookupService);
292+
pb.AddAttribute(32, nameof(LookupContent.LookupServiceKey), col.LookupServiceKey);
293+
pb.AddAttribute(33, nameof(LookupContent.LookupServiceData), col.LookupServiceData);
294+
pb.AddAttribute(34, nameof(LookupContent.LookupStringComparison), col.LookupStringComparison);
295+
pb.AddAttribute(35, nameof(LookupContent.Value), text);
296+
pb.CloseComponent();
297+
}
288298
else
289299
{
290-
pb.AddContent(30, text);
300+
pb.AddContent(40, text);
291301
}
292302
};
293303

0 commit comments

Comments
 (0)