Skip to content

Commit

Permalink
refactor(Table): table display value use LookupService (#4935)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
ArgoZhang and StevenBase authored Dec 24, 2024
1 parent 4059dc2 commit d651d08
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.1.9-beta03</Version>
<Version>9.1.9-beta04</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
83 changes: 83 additions & 0 deletions src/BootstrapBlazor/Components/Table/LookupContent.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务实例
/// </summary>
[Parameter]
public ILookupService? LookupService { get; set; }

/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作,可以通过 <see cref="LookupServiceData"/> 传递自定义数据
/// </summary>
[Parameter]
[EditorRequired]
public string? LookupServiceKey { get; set; }

/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值自定义数据,通过 <see cref="LookupServiceKey"/> 指定键值
/// </summary>
[Parameter]
public object? LookupServiceData { get; set; }

/// <summary>
/// 获得/设置 字典数据源字符串比较规则 默认 <see cref="StringComparison.OrdinalIgnoreCase" /> 大小写不敏感
/// </summary>
[Parameter]
public StringComparison LookupStringComparison { get; set; }

/// <summary>
/// 获得/设置 显示值
/// </summary>
[Parameter]
public string? Value { get; set; }

[Inject]
[NotNull]
private ILookupService? InjectLookupService { get; set; }

private string? _content;

private List<SelectedItem>? _items;

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
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;
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="builder"></param>
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (!string.IsNullOrEmpty(_content))
{
builder.AddContent(0, _content);
}
}

private async Task<List<SelectedItem>> GetLookupItemsAsync()
{
var lookupService = LookupService ?? InjectLookupService;
var items = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData);
return items?.ToList() ?? [];
}
}
12 changes: 11 additions & 1 deletion src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,19 @@ private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string
{
pb.AddMarkupContent(20, text);
}
else if (col.IsLookup())
{
pb.OpenComponent<LookupContent>(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);
}
};

Expand Down

0 comments on commit d651d08

Please sign in to comment.