From 27b95b23263e75158289c34f7cf26f63113c3b77 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Dec 2024 11:57:07 +0800 Subject: [PATCH 1/6] =?UTF-8?q?refactor:=20=E5=A4=8D=E7=94=A8=20IsLookup?= =?UTF-8?q?=20=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: ZhYan <19183404+Axxbis@users.noreply.github.com> --- src/BootstrapBlazor/Components/Filters/TableFilter.razor | 2 +- src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs | 2 -- src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) 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/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 实例是否可以编辑 From 5232b64c9eb14eb4fb37ab31dc20d4fa85d37d92 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Dec 2024 12:06:32 +0800 Subject: [PATCH 2/6] =?UTF-8?q?refactor:=20=E8=A1=A8=E6=A0=BC=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Lookup=20=E5=BC=82=E6=AD=A5=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Table/Table.razor.cs | 6 ------ .../Extensions/ITableColumnExtensions.cs | 13 ++++++------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 17b7af3f915..5d92bba901d 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -1296,12 +1296,6 @@ 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)); } }; diff --git a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs index 9c1c0687134..685bb3161f6 100644 --- a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs @@ -175,14 +175,9 @@ public static List ToSearches(this IEnumerable colu internal static RenderFragment RenderValue(this ITableColumn col, TItem item) => 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)); } else if (val is bool v1) { @@ -253,6 +248,10 @@ private static RenderFragment RenderTooltip(this ITableColumn col, string if (col.GetTooltipTextCallback != null) { pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func>(() => col.GetTooltipTextCallback(item))); + } + else if(col.IsLookup()) + { + } else { From 4a57e8307721b4261b6af5762f0029d89ed70e43 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Dec 2024 13:11:15 +0800 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20Tooltip=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=20Lookup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Table/Table.razor.cs | 2 +- .../Extensions/ITableColumnExtensions.cs | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 5d92bba901d..2d3f3ecf514 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -1296,7 +1296,7 @@ protected RenderFragment GetValue(ITableColumn col, TItem item) => builder => } else { - builder.AddContent(20, col.RenderValue(item)); + builder.AddContent(20, col.RenderValue(item, LookupService)); } }; #endregion diff --git a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs index 685bb3161f6..ed1abcc3cc1 100644 --- a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs @@ -172,12 +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.IsLookup() && val != null) { - builder.AddContent(10, col.RenderTooltip(val.ToString(), item)); + builder.AddContent(10, col.RenderTooltip(val.ToString(), item, lookupService)); } else if (val is bool v1) { @@ -213,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)); } } }; @@ -238,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()) { @@ -249,9 +257,13 @@ private static RenderFragment RenderTooltip(this ITableColumn col, string { pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func>(() => col.GetTooltipTextCallback(item))); } - else if(col.IsLookup()) + else if (col.IsLookup()) { - + pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func>(async () => + { + var lookup = await col.GetLookupService(lookupService).GetItemsAsync(col.LookupServiceKey, col.LookupServiceData); + return lookup?.FirstOrDefault(l => l.Value.Equals(text, col.LookupStringComparison))?.Text ?? text; + })); } else { From 61ce633291fc51ee5e35e74a59f526b9742b8a4f Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Dec 2024 13:39:55 +0800 Subject: [PATCH 4/6] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20Lookup=20?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: ZhYan <19183404+Axxbis@users.noreply.github.com> --- src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs index ed1abcc3cc1..1bd703e6d94 100644 --- a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs @@ -261,7 +261,7 @@ private static RenderFragment RenderTooltip(this ITableColumn col, string { pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func>(async () => { - var lookup = await col.GetLookupService(lookupService).GetItemsAsync(col.LookupServiceKey, col.LookupServiceData); + var lookup = col.Lookup ?? await col.GetLookupService(lookupService).GetItemsAsync(col.LookupServiceKey, col.LookupServiceData); return lookup?.FirstOrDefault(l => l.Value.Equals(text, col.LookupStringComparison))?.Text ?? text; })); } From 62022a0266cadbf20a16e2b93a496d02cf476d7e Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Dec 2024 13:40:17 +0800 Subject: [PATCH 5/6] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: ZhYan <19183404+Axxbis@users.noreply.github.com> --- test/UnitTest/Components/TableTest.cs | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) 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] From b8e97d337d63127aae5e12d650597dd86e1035eb Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 23 Dec 2024 13:45:53 +0800 Subject: [PATCH 6/6] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E9=87=8D?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs index 1bd703e6d94..30a057208d1 100644 --- a/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs @@ -262,7 +262,7 @@ private static RenderFragment RenderTooltip(this ITableColumn col, string 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 => l.Value.Equals(text, col.LookupStringComparison))?.Text ?? text; + return lookup?.FirstOrDefault(l => string.Equals(l.Value, text, col.LookupStringComparison))?.Text ?? text; })); } else