Skip to content

Commit ccdc9ae

Browse files
ArgoZhangeramosr16
andauthored
refactor(Table): Lookup on TableColumn not work (#4940)
* chore: bump version 9.19-beta06 * feat: 增加 Lookup 支持 Co-Authored-By: Ernesto Ramos Rio <eramosr14@gmail.com> * refactor: 精简参数 Co-Authored-By: Ernesto Ramos Rio <eramosr14@gmail.com> * refactor: 重构代码 Co-Authored-By: Ernesto Ramos Rio <eramosr14@gmail.com> --------- Co-authored-by: Ernesto Ramos Rio <eramosr14@gmail.com>
1 parent d123c74 commit ccdc9ae

File tree

4 files changed

+62
-49
lines changed

4 files changed

+62
-49
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.1.9-beta05</Version>
4+
<Version>9.1.9-beta06</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/LookupContent.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
55

66
using Microsoft.AspNetCore.Components.Rendering;
7-
using System.ComponentModel;
87

98
namespace BootstrapBlazor.Components;
109

1110
internal class LookupContent : ComponentBase
1211
{
12+
/// <summary>
13+
/// 获得/设置 <see cref="ILookupService"/> 服务实例
14+
/// </summary>
15+
[Parameter]
16+
public IEnumerable<SelectedItem>? Lookup { get; set; }
17+
1318
/// <summary>
1419
/// 获得/设置 <see cref="ILookupService"/> 服务实例
1520
/// </summary>
@@ -76,8 +81,16 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
7681

7782
private async Task<List<SelectedItem>> GetLookupItemsAsync()
7883
{
79-
var lookupService = LookupService ?? InjectLookupService;
80-
var items = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData);
84+
IEnumerable<SelectedItem>? items;
85+
if (Lookup != null)
86+
{
87+
items = Lookup;
88+
}
89+
else
90+
{
91+
var lookupService = LookupService ?? InjectLookupService;
92+
items = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData);
93+
}
8194
return items?.ToList() ?? [];
8295
}
8396
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ protected RenderFragment GetValue(ITableColumn col, TItem item) => builder =>
12961296
}
12971297
else
12981298
{
1299-
builder.AddContent(20, col.RenderValue(item, LookupService));
1299+
builder.AddContent(20, col.RenderValue(item));
13001300
}
13011301
};
13021302
#endregion

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

+44-44
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,13 @@ public static List<IFilterAction> ToSearches(this IEnumerable<ITableColumn> colu
178178
/// <typeparam name="TItem"></typeparam>
179179
/// <param name="col"></param>
180180
/// <param name="item"></param>
181-
/// <param name="lookupService"></param>
182181
/// <returns></returns>
183-
public static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item, ILookupService lookupService) => builder =>
182+
public static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item) => builder =>
184183
{
185184
var val = col.GetItemValue(item);
186185
if (col.IsLookup() && val != null)
187186
{
188-
builder.AddContent(10, col.RenderTooltip(val.ToString(), item, lookupService));
187+
builder.AddContent(10, col.RenderLookupContent(val.ToString(), item));
189188
}
190189
else if (val is bool v1)
191190
{
@@ -221,7 +220,7 @@ public static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem ite
221220
{
222221
content = val?.ToString();
223222
}
224-
builder.AddContent(30, col.RenderTooltip(content, item, lookupService));
223+
builder.AddContent(30, col.RenderLookupContent(content, item));
225224
}
226225
}
227226
};
@@ -246,58 +245,59 @@ internal static RenderFragment RenderColor<TItem>(this ITableColumn col, TItem i
246245
builder.CloseElement();
247246
};
248247

249-
private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string? text, TItem item, ILookupService lookupService) => pb =>
248+
private static RenderFragment RenderLookupContent<TItem>(this ITableColumn col, string? text, TItem item) => pb =>
250249
{
251250
if (col.GetShowTips())
252251
{
253-
pb.OpenComponent<Tooltip>(0);
254-
pb.SetKey(item);
255-
var tooltipText = text;
256-
if (col.GetTooltipTextCallback != null)
257-
{
258-
pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func<Task<string?>>(() => col.GetTooltipTextCallback(item)));
259-
}
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-
}
268-
else
269-
{
270-
pb.AddAttribute(11, nameof(Tooltip.Title), tooltipText);
271-
}
272-
pb.AddAttribute(12, "class", "text-truncate d-block");
273-
if (col.IsMarkupString)
274-
{
275-
pb.AddAttribute(13, nameof(Tooltip.ChildContent), new RenderFragment(builder => builder.AddMarkupContent(0, text)));
276-
pb.AddAttribute(14, nameof(Tooltip.IsHtml), true);
277-
}
278-
else
279-
{
280-
pb.AddAttribute(15, nameof(Tooltip.ChildContent), new RenderFragment(builder => builder.AddContent(0, text)));
281-
}
282-
pb.CloseComponent();
252+
pb.AddContent(10, col.RenderTooltip(text, item));
253+
}
254+
else
255+
{
256+
pb.AddContent(20, col.RenderContent(text));
257+
}
258+
};
259+
260+
private static RenderFragment RenderTooltip<TItem>(this ITableColumn col, string? text, TItem item) => pb =>
261+
{
262+
pb.OpenComponent<Tooltip>(0);
263+
pb.SetKey(item);
264+
if (col.GetTooltipTextCallback != null)
265+
{
266+
pb.AddAttribute(10, nameof(Tooltip.GetTitleCallback), new Func<Task<string?>>(() => col.GetTooltipTextCallback(item)));
283267
}
284-
else if (col.IsLookup())
268+
else
269+
{
270+
pb.AddAttribute(11, nameof(Tooltip.Title), text);
271+
}
272+
if (col.IsMarkupString)
273+
{
274+
pb.AddAttribute(12, nameof(Tooltip.IsHtml), true);
275+
}
276+
pb.AddAttribute(13, "class", "text-truncate d-block");
277+
pb.AddAttribute(14, nameof(Tooltip.ChildContent), col.RenderContent(text));
278+
pb.CloseComponent();
279+
};
280+
281+
private static RenderFragment RenderContent(this ITableColumn col, string? text) => pb =>
282+
{
283+
if (col.IsLookup())
285284
{
286-
pb.OpenComponent<LookupContent>(20);
287-
pb.AddAttribute(21, nameof(LookupContent.LookupService), col.LookupService);
288-
pb.AddAttribute(22, nameof(LookupContent.LookupServiceKey), col.LookupServiceKey);
289-
pb.AddAttribute(23, nameof(LookupContent.LookupServiceData), col.LookupServiceData);
290-
pb.AddAttribute(24, nameof(LookupContent.LookupStringComparison), col.LookupStringComparison);
291-
pb.AddAttribute(25, nameof(LookupContent.Value), text);
285+
pb.OpenComponent<LookupContent>(100);
286+
pb.AddAttribute(101, nameof(LookupContent.LookupService), col.LookupService);
287+
pb.AddAttribute(102, nameof(LookupContent.LookupServiceKey), col.LookupServiceKey);
288+
pb.AddAttribute(103, nameof(LookupContent.LookupServiceData), col.LookupServiceData);
289+
pb.AddAttribute(104, nameof(LookupContent.LookupStringComparison), col.LookupStringComparison);
290+
pb.AddAttribute(105, nameof(LookupContent.Lookup), col.Lookup);
291+
pb.AddAttribute(106, nameof(LookupContent.Value), text);
292292
pb.CloseComponent();
293293
}
294294
else if (col.IsMarkupString)
295295
{
296-
pb.AddMarkupContent(30, text);
296+
pb.AddMarkupContent(110, text);
297297
}
298298
else
299299
{
300-
pb.AddContent(40, text);
300+
pb.AddContent(120, text);
301301
}
302302
};
303303

0 commit comments

Comments
 (0)