Skip to content

Commit 29bb8a0

Browse files
ArgoZhangAxxbis
andauthored
feat(Table): render tooltip support LookupService GetItemByKeyAsync (#4926)
* refactor: 复用 IsLookup 扩展方法 Co-Authored-By: ZhYan <[email protected]> * refactor: 表格支持 Lookup 异步获取数据 * refactor: Tooltip 支持异步 Lookup * refactor: 增加 Lookup 异步支持 Co-Authored-By: ZhYan <[email protected]> * test: 更新单元测试 Co-Authored-By: ZhYan <[email protected]> * refactor: 代码重构 --------- Co-Authored-By: ZhYan <[email protected]>
1 parent 8de9a2e commit 29bb8a0

File tree

6 files changed

+124
-21
lines changed

6 files changed

+124
-21
lines changed

src/BootstrapBlazor/Components/Filters/TableFilter.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ else
4444
{
4545
<EnumFilter Type="Column.PropertyType" Count="_count"></EnumFilter>
4646
}
47-
else if (IsLookup)
47+
else if (Column.IsLookup())
4848
{
4949
<LookupFilter Lookup="Column.Lookup" LookupService="Column.LookupService" LookupServiceKey="Column.LookupServiceKey" LookupServiceData="Column.LookupServiceData" LookupStringComparison="Column.LookupStringComparison" Type="Column.PropertyType" IsShowSearch="Column.ShowSearchWhenSelect"></LookupFilter>
5050
}

src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,4 @@ private void OnClickMinus()
256256
_count--;
257257
}
258258
}
259-
260-
private bool IsLookup => Column.Lookup != null || !string.IsNullOrEmpty(Column.LookupServiceKey);
261259
}

src/BootstrapBlazor/Components/Table/Table.razor.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,7 @@ protected RenderFragment GetValue(ITableColumn col, TItem item) => builder =>
12961296
}
12971297
else
12981298
{
1299-
if (col.Lookup == null && !string.IsNullOrEmpty(col.LookupServiceKey))
1300-
{
1301-
// 未设置 Lookup
1302-
// 设置 LookupService 键值
1303-
col.Lookup = LookupService.GetItemsByKey(col.LookupServiceKey, col.LookupServiceData);
1304-
}
1305-
builder.AddContent(20, col.RenderValue(item));
1299+
builder.AddContent(20, col.RenderValue(item, LookupService));
13061300
}
13071301
};
13081302
#endregion

src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class IEditorItemExtensions
1717
/// </summary>
1818
/// <param name="item"></param>
1919
/// <returns></returns>
20-
public static bool IsLookup(this IEditorItem item) => item.Lookup != null || item.LookupService != null || !string.IsNullOrEmpty(item.LookupServiceKey);
20+
public static bool IsLookup(this IEditorItem item) => item.Lookup != null || !string.IsNullOrEmpty(item.LookupServiceKey);
2121

2222
/// <summary>
2323
/// 判断当前 IEditorItem 实例是否可以编辑

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,20 @@ public static List<IFilterAction> ToSearches(this IEnumerable<ITableColumn> colu
172172
return searches;
173173
}
174174

175-
internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item) => builder =>
175+
/// <summary>
176+
/// 当前单元格方法
177+
/// </summary>
178+
/// <typeparam name="TItem"></typeparam>
179+
/// <param name="col"></param>
180+
/// <param name="item"></param>
181+
/// <param name="lookupService"></param>
182+
/// <returns></returns>
183+
public static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item, ILookupService lookupService) => builder =>
176184
{
177185
var val = col.GetItemValue(item);
178-
if (col.Lookup != null && val != null)
186+
if (col.IsLookup() && val != null)
179187
{
180-
// 转化 Lookup 数据源
181-
var lookupVal = col.Lookup.FirstOrDefault(l => l.Value.Equals(val.ToString(), col.LookupStringComparison));
182-
if (lookupVal != null)
183-
{
184-
builder.AddContent(10, col.RenderTooltip(lookupVal.Text, item));
185-
}
188+
builder.AddContent(10, col.RenderTooltip(val.ToString(), item, lookupService));
186189
}
187190
else if (val is bool v1)
188191
{
@@ -218,7 +221,7 @@ internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem i
218221
{
219222
content = val?.ToString();
220223
}
221-
builder.AddContent(30, col.RenderTooltip(content, item));
224+
builder.AddContent(30, col.RenderTooltip(content, item, lookupService));
222225
}
223226
}
224227
};
@@ -243,7 +246,7 @@ internal static RenderFragment RenderColor<TItem>(this ITableColumn col, TItem i
243246
builder.CloseElement();
244247
};
245248

246-
private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string? text, TItem item) => pb =>
249+
private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string? text, TItem item, ILookupService lookupService) => pb =>
247250
{
248251
if (col.GetShowTips())
249252
{
@@ -254,6 +257,14 @@ private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string
254257
{
255258
pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func<Task<string?>>(() => col.GetTooltipTextCallback(item)));
256259
}
260+
else if (col.IsLookup())
261+
{
262+
pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func<Task<string?>>(async () =>
263+
{
264+
var lookup = col.Lookup ?? await col.GetLookupService(lookupService).GetItemsAsync(col.LookupServiceKey, col.LookupServiceData);
265+
return lookup?.FirstOrDefault(l => string.Equals(l.Value, text, col.LookupStringComparison))?.Text ?? text;
266+
}));
267+
}
257268
else
258269
{
259270
pb.AddAttribute(11, nameof(Tooltip.Title), tooltipText);

test/UnitTest/Components/TableTest.cs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7665,6 +7665,106 @@ public async Task GetValue_LookupServiceKey()
76657665

76667666
var table = cut.FindComponent<MockTable>();
76677667
await cut.InvokeAsync(() => table.Instance.QueryAsync());
7668+
7669+
col.SetParametersAndRender(pb =>
7670+
{
7671+
pb.Add(a => a.ShowTips, true);
7672+
pb.Add(a => a.Lookup, null);
7673+
pb.Add(a => a.LookupService, new MockLookupServiceAsync());
7674+
});
7675+
await cut.InvokeAsync(() => table.Instance.QueryAsync());
7676+
cut.WaitForElement("[data-bs-original-title=\"LookupService-Test-True-async\"]");
7677+
}
7678+
7679+
[Fact]
7680+
public async Task GetValue_LookupServiceKey_Null()
7681+
{
7682+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
7683+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
7684+
{
7685+
pb.AddChildContent<MockTable>(pb =>
7686+
{
7687+
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
7688+
pb.Add(a => a.TableColumns, foo => builder =>
7689+
{
7690+
builder.OpenComponent<TableColumn<Foo, bool>>(0);
7691+
builder.AddAttribute(1, "Field", true);
7692+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Complete", typeof(bool)));
7693+
builder.AddAttribute(3, "LookupServiceKey", "null");
7694+
builder.AddAttribute(4, "LookupServiceData", true);
7695+
builder.AddAttribute(5, "ShowTips", true);
7696+
builder.AddAttribute(6, "LookupService", new MockLookupServiceAsync());
7697+
builder.CloseComponent();
7698+
});
7699+
});
7700+
});
7701+
7702+
var table = cut.FindComponent<MockTable>();
7703+
await cut.InvokeAsync(() => table.Instance.QueryAsync());
7704+
cut.WaitForElement("[data-bs-original-title=\"True\"]");
7705+
}
7706+
7707+
[Fact]
7708+
public async Task GetValue_LookupServiceKey_NullText()
7709+
{
7710+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
7711+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
7712+
{
7713+
pb.AddChildContent<MockTable>(pb =>
7714+
{
7715+
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
7716+
pb.Add(a => a.TableColumns, foo => builder =>
7717+
{
7718+
builder.OpenComponent<TableColumn<Foo, bool>>(0);
7719+
builder.AddAttribute(1, "Field", true);
7720+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Complete", typeof(bool)));
7721+
builder.AddAttribute(3, "LookupServiceKey", "null-text");
7722+
builder.AddAttribute(4, "LookupServiceData", true);
7723+
builder.AddAttribute(5, "ShowTips", true);
7724+
builder.AddAttribute(6, "LookupService", new MockLookupServiceAsync());
7725+
builder.CloseComponent();
7726+
});
7727+
});
7728+
});
7729+
7730+
var table = cut.FindComponent<MockTable>();
7731+
await cut.InvokeAsync(() => table.Instance.QueryAsync());
7732+
cut.WaitForElement("[data-bs-original-title=\"True\"]");
7733+
}
7734+
class MockLookupServiceAsync : LookupServiceBase
7735+
{
7736+
public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data) => null;
7737+
7738+
public override async Task<IEnumerable<SelectedItem>?> GetItemsByKeyAsync(string? key, object? data)
7739+
{
7740+
await Task.Delay(300);
7741+
7742+
IEnumerable<SelectedItem>? ret = null;
7743+
7744+
if (key == "test")
7745+
{
7746+
ret = new SelectedItem[]
7747+
{
7748+
new("True", "LookupService-Test-True-async"),
7749+
new("False", "LookupService-Test-False-async")
7750+
};
7751+
}
7752+
7753+
if (key == "null")
7754+
{
7755+
ret = null;
7756+
}
7757+
7758+
if (key == "null-text")
7759+
{
7760+
ret = new SelectedItem[]
7761+
{
7762+
new("Fake-True", "Fake-True"),
7763+
new("Fake-False", "Fake-False")
7764+
};
7765+
}
7766+
return ret;
7767+
}
76687768
}
76697769

76707770
[Fact]

0 commit comments

Comments
 (0)