Skip to content

Commit cb4bd6f

Browse files
committed
refactor: 增加 LookupService 扩展方法
1 parent c4901f2 commit cb4bd6f

File tree

5 files changed

+16
-40
lines changed

5 files changed

+16
-40
lines changed

src/BootstrapBlazor/Components/EditorForm/EditorForm.razor

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@
4040
</div>
4141
}
4242

43-
@if (!_inited)
44-
{
45-
<div class="ef-loading">
46-
<Spinner Color="Color.Primary" />
47-
</div>
48-
}
43+
<div class="ef-loading">
44+
<Spinner Color="Color.Primary" />
45+
</div>
4946
</div>
5047

5148
@code

src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ public partial class EditorForm<TModel> : IShowLabel
191191
.GroupBy(i => i.GroupOrder).OrderBy(i => i.Key)
192192
.Select(i => new KeyValuePair<string, IOrderedEnumerable<IEditorItem>>(i.First().GroupName!, i.OrderBy(x => x.Order)));
193193

194-
private bool _inited;
195194
private List<IEditorItem>? _itemsCache;
196195

197196
private List<IEditorItem> RenderItems
@@ -238,29 +237,6 @@ protected override void OnParametersSet()
238237
_itemsCache = null;
239238
}
240239

241-
/// <summary>
242-
/// <inheritdoc/>
243-
/// </summary>
244-
/// <param name="firstRender"></param>
245-
protected override async Task OnAfterRenderAsync(bool firstRender)
246-
{
247-
await base.OnAfterRenderAsync(firstRender);
248-
249-
if (firstRender)
250-
{
251-
foreach (var item in RenderItems)
252-
{
253-
if (item.Lookup == null && !string.IsNullOrEmpty(item.LookupServiceKey))
254-
{
255-
var lookupServcie = item.LookupService ?? LookupService;
256-
item.Lookup = await lookupServcie.GetItemsAsync(item.LookupServiceKey, item.LookupServiceData);
257-
}
258-
}
259-
_inited = true;
260-
StateHasChanged();
261-
}
262-
}
263-
264240
private List<IEditorItem> GetRenderItems()
265241
{
266242
var items = new List<IEditorItem>();
@@ -315,7 +291,7 @@ private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>
315291
else
316292
{
317293
item.PlaceHolder ??= PlaceHolderText;
318-
builder.CreateComponentByFieldType(this, item, Model, ItemChangedType, IsSearch.Value);
294+
builder.CreateComponentByFieldType(this, item, Model, ItemChangedType, IsSearch.Value, item.GetLookupService(LookupService));
319295
}
320296
};
321297

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,12 +1141,6 @@ private async Task ProcessFirstRender()
11411141
Columns.Clear();
11421142
Columns.AddRange(cols.OrderFunc());
11431143

1144-
// 准备 Lookup 数据
1145-
foreach (var column in Columns)
1146-
{
1147-
column.Lookup ??= await LookupService.GetItemsAsync(column.LookupServiceKey, column.LookupServiceData);
1148-
}
1149-
11501144
// 查看是否开启列宽序列化
11511145
_clientColumnWidths = await ReloadColumnWidthFromBrowserAsync();
11521146
ResetColumnWidth();
@@ -1329,7 +1323,7 @@ RenderFragment RenderTemplate() => col.Template == null
13291323
: col.Template(item);
13301324

13311325
RenderFragment RenderEditTemplate() => col.EditTemplate == null
1332-
? new RenderFragment(builder => builder.CreateComponentByFieldType(this, col, item, changedType, false))
1326+
? new RenderFragment(builder => builder.CreateComponentByFieldType(this, col, item, changedType, false, col.GetLookupService(LookupService)))
13331327
: col.EditTemplate(item);
13341328
}
13351329

@@ -1371,7 +1365,7 @@ void SetDynamicEditTemplate()
13711365
parameters.Add(new(nameof(ValidateBase<string>.OnValueChanged), onValueChanged.Invoke(d, col, (model, column, val) => DynamicContext.OnValueChanged(model, column, val))));
13721366
col.ComponentParameters = parameters;
13731367
}
1374-
builder.CreateComponentByFieldType(this, col, row, changedType, false);
1368+
builder.CreateComponentByFieldType(this, col, row, changedType, false, col.GetLookupService(LookupService));
13751369
};
13761370
}
13771371

src/BootstrapBlazor/Extensions/IEditorItemExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,12 @@ bool ComplexCanWrite()
110110
internal static bool GetIgnore(this IEditorItem col) => col.Ignore ?? false;
111111

112112
internal static bool GetReadonly(this IEditorItem col) => col.Readonly ?? false;
113+
114+
/// <summary>
115+
/// 获得 ILookupService 实例
116+
/// </summary>
117+
/// <param name="item"></param>
118+
/// <param name="service"></param>
119+
/// <returns></returns>
120+
public static ILookupService GetLookupService(this IEditorItem item, ILookupService service) => item.LookupService ?? service;
113121
}

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ public static void CreateDisplayByFieldType(this RenderTreeBuilder builder, IEdi
459459
/// <param name="item"></param>
460460
/// <param name="changedType"></param>
461461
/// <param name="isSearch"></param>
462-
public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, ItemChangedType changedType = ItemChangedType.Update, bool isSearch = false)
462+
/// <param name="lookUpService"></param>
463+
public static void CreateComponentByFieldType(this RenderTreeBuilder builder, ComponentBase component, IEditorItem item, object model, ItemChangedType changedType = ItemChangedType.Update, bool isSearch = false, ILookupService? lookUpService = null)
463464
{
464465
var fieldType = item.PropertyType;
465466
var fieldName = item.GetFieldName();

0 commit comments

Comments
 (0)