diff --git a/src/BootstrapBlazor/Components/Filters/TableFilter.razor b/src/BootstrapBlazor/Components/Filters/TableFilter.razor index 2e41bdef4cb..df50a1e7af4 100644 --- a/src/BootstrapBlazor/Components/Filters/TableFilter.razor +++ b/src/BootstrapBlazor/Components/Filters/TableFilter.razor @@ -44,7 +44,7 @@ else { } - else if (IsLookup) + else if (Column.IsLookup()) { } diff --git a/src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs b/src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs index 87a17fea804..66d99990879 100644 --- a/src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs +++ b/src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs @@ -256,6 +256,4 @@ private void OnClickMinus() _count--; } } - - private bool IsLookup => Column.Lookup != null || !string.IsNullOrEmpty(Column.LookupServiceKey); } diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 17b7af3f915..2d3f3ecf514 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -1296,13 +1296,7 @@ protected RenderFragment GetValue(ITableColumn col, TItem item) => builder => } else { - if (col.Lookup == null && !string.IsNullOrEmpty(col.LookupServiceKey)) - { - // 未设置 Lookup - // 设置 LookupService 键值 - col.Lookup = LookupService.GetItemsByKey(col.LookupServiceKey, col.LookupServiceData); - } - builder.AddContent(20, col.RenderValue(item)); + builder.AddContent(20, col.RenderValue(item, LookupService)); } }; #endregion diff --git a/src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs b/src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs index 2071082215a..c38e7a6924d 100644 --- a/src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs +++ b/src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs @@ -17,7 +17,7 @@ public static class IEditorItemExtensions /// /// /// - public static bool IsLookup(this IEditorItem item) => item.Lookup != null || item.LookupService != null || !string.IsNullOrEmpty(item.LookupServiceKey); + public static bool IsLookup(this IEditorItem item) => item.Lookup != null || !string.IsNullOrEmpty(item.LookupServiceKey); /// /// 判断当前 IEditorItem 实例是否可以编辑 diff --git a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs index 9c1c0687134..30a057208d1 100644 --- a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs @@ -172,17 +172,20 @@ public static List ToSearches(this IEnumerable colu return searches; } - internal static RenderFragment RenderValue(this ITableColumn col, TItem item) => builder => + /// + /// 当前单元格方法 + /// + /// + /// + /// + /// + /// + public static RenderFragment RenderValue(this ITableColumn col, TItem item, ILookupService lookupService) => builder => { var val = col.GetItemValue(item); - if (col.Lookup != null && val != null) + if (col.IsLookup() && val != null) { - // 转化 Lookup 数据源 - var lookupVal = col.Lookup.FirstOrDefault(l => l.Value.Equals(val.ToString(), col.LookupStringComparison)); - if (lookupVal != null) - { - builder.AddContent(10, col.RenderTooltip(lookupVal.Text, item)); - } + builder.AddContent(10, col.RenderTooltip(val.ToString(), item, lookupService)); } else if (val is bool v1) { @@ -218,7 +221,7 @@ internal static RenderFragment RenderValue(this ITableColumn col, TItem i { content = val?.ToString(); } - builder.AddContent(30, col.RenderTooltip(content, item)); + builder.AddContent(30, col.RenderTooltip(content, item, lookupService)); } } }; @@ -243,7 +246,7 @@ internal static RenderFragment RenderColor(this ITableColumn col, TItem i builder.CloseElement(); }; - private static RenderFragment RenderTooltip(this ITableColumn col, string? text, TItem item) => pb => + private static RenderFragment RenderTooltip(this ITableColumn col, string? text, TItem item, ILookupService lookupService) => pb => { if (col.GetShowTips()) { @@ -254,6 +257,14 @@ private static RenderFragment RenderTooltip(this ITableColumn col, string { pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func>(() => col.GetTooltipTextCallback(item))); } + else if (col.IsLookup()) + { + pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func>(async () => + { + var lookup = col.Lookup ?? await col.GetLookupService(lookupService).GetItemsAsync(col.LookupServiceKey, col.LookupServiceData); + return lookup?.FirstOrDefault(l => string.Equals(l.Value, text, col.LookupStringComparison))?.Text ?? text; + })); + } else { pb.AddAttribute(11, nameof(Tooltip.Title), tooltipText); diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index bece13e9b74..857c74d238f 100644 --- a/test/UnitTest/Components/TableTest.cs +++ b/test/UnitTest/Components/TableTest.cs @@ -7665,6 +7665,106 @@ public async Task GetValue_LookupServiceKey() var table = cut.FindComponent(); await cut.InvokeAsync(() => table.Instance.QueryAsync()); + + col.SetParametersAndRender(pb => + { + pb.Add(a => a.ShowTips, true); + pb.Add(a => a.Lookup, null); + pb.Add(a => a.LookupService, new MockLookupServiceAsync()); + }); + await cut.InvokeAsync(() => table.Instance.QueryAsync()); + cut.WaitForElement("[data-bs-original-title=\"LookupService-Test-True-async\"]"); + } + + [Fact] + public async Task GetValue_LookupServiceKey_Null() + { + var localizer = Context.Services.GetRequiredService>(); + var cut = Context.RenderComponent(pb => + { + pb.AddChildContent(pb => + { + pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer)); + pb.Add(a => a.TableColumns, foo => builder => + { + builder.OpenComponent>(0); + builder.AddAttribute(1, "Field", true); + builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Complete", typeof(bool))); + builder.AddAttribute(3, "LookupServiceKey", "null"); + builder.AddAttribute(4, "LookupServiceData", true); + builder.AddAttribute(5, "ShowTips", true); + builder.AddAttribute(6, "LookupService", new MockLookupServiceAsync()); + builder.CloseComponent(); + }); + }); + }); + + var table = cut.FindComponent(); + await cut.InvokeAsync(() => table.Instance.QueryAsync()); + cut.WaitForElement("[data-bs-original-title=\"True\"]"); + } + + [Fact] + public async Task GetValue_LookupServiceKey_NullText() + { + var localizer = Context.Services.GetRequiredService>(); + var cut = Context.RenderComponent(pb => + { + pb.AddChildContent(pb => + { + pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer)); + pb.Add(a => a.TableColumns, foo => builder => + { + builder.OpenComponent>(0); + builder.AddAttribute(1, "Field", true); + builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Complete", typeof(bool))); + builder.AddAttribute(3, "LookupServiceKey", "null-text"); + builder.AddAttribute(4, "LookupServiceData", true); + builder.AddAttribute(5, "ShowTips", true); + builder.AddAttribute(6, "LookupService", new MockLookupServiceAsync()); + builder.CloseComponent(); + }); + }); + }); + + var table = cut.FindComponent(); + await cut.InvokeAsync(() => table.Instance.QueryAsync()); + cut.WaitForElement("[data-bs-original-title=\"True\"]"); + } + class MockLookupServiceAsync : LookupServiceBase + { + public override IEnumerable? GetItemsByKey(string? key, object? data) => null; + + public override async Task?> GetItemsByKeyAsync(string? key, object? data) + { + await Task.Delay(300); + + IEnumerable? ret = null; + + if (key == "test") + { + ret = new SelectedItem[] + { + new("True", "LookupService-Test-True-async"), + new("False", "LookupService-Test-False-async") + }; + } + + if (key == "null") + { + ret = null; + } + + if (key == "null-text") + { + ret = new SelectedItem[] + { + new("Fake-True", "Fake-True"), + new("Fake-False", "Fake-False") + }; + } + return ret; + } } [Fact]