Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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([email protected]) 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider refreshing lookup items when parameters change

The current caching of _items might cause stale data if LookupService or LookupServiceData changes. Consider clearing _items when these parameters change to ensure fresh data.

Suggested implementation:

    private string? _content;
    private List<SelectedItem>? _items;
    private ILookupService? _previousLookupService;
    private object? _previousLookupServiceData;

    /// <summary>
    /// Method invoked when the component has received parameters from its parent in
    /// the render tree, and the incoming values have been assigned to properties.
    /// </summary>
    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        // Check if LookupService or LookupServiceData have changed
        if (!ReferenceEquals(_previousLookupService, LookupService))
        {
            _items = null;
            _previousLookupService = LookupService;
        }
    }

    /// <summary>

Note: Since I can't see the full file, you'll need to:

  1. Make sure the class inherits from ComponentBase (which provides OnParametersSet)
  2. If LookupServiceData is a parameter, add similar change detection for it in OnParametersSet
  3. Verify this doesn't conflict with any existing parameter change handling


/// <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
Loading