Skip to content

Commit

Permalink
feat(ILookupService): add GetItemsByKeyAsync method (#4816)
Browse files Browse the repository at this point in the history
* refactor: 增加 GetItemsByKeyAsync 方法

* refactor: 增加 GetItemsAsync 扩展方法

* refactor: 改造 Lookup 使用方法支持异步方法

* refactor: IEditorItem 增加 LookupService 属性

* refactor: 改造 EditorForm 支持异步字典

* refactor: 改造 LookupFilter 支持异步 Lookup

* test: 跟新 EditorForm 单元测试支持异步 Lookup

* test: 更新单元测试支持异步 Lookup

* test: 更新单元测试

* test: 更新单元测试

* test: 更新单元测试

* test: 更新单元测试

* test: 更新 EditorForm 单元测试

* test: 更新单元测试

* test: 增加单元测试

* chore: bump version 9.1.3-beta02
  • Loading branch information
ArgoZhang authored Dec 10, 2024
1 parent a85cd0c commit dc943bb
Show file tree
Hide file tree
Showing 27 changed files with 358 additions and 103 deletions.
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Attributes/AutoGenerateColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ public class AutoGenerateColumnAttribute : AutoGenerateBaseAttribute, ITableColu
/// </summary>
public object? LookupServiceData { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
ILookupService? IEditorItem.LookupService { get; set; }

/// <summary>
/// 获得/设置 单元格回调方法
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.1.3-beta01</Version>
<Version>9.1.3-beta02</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions src/BootstrapBlazor/Components/EditorForm/EditorForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
@FieldItems?.Invoke(Model)
</CascadingValue>
<CascadingValue Value="this" Name="EditorForm">
<RenderTemplate OnRenderAsync="OnRenderAsync">
@{
ResetItems();
}
<RenderTemplate>
@if (ShowUnsetGroupItemsOnTop)
{
if (UnsetGroupItems.Any())
Expand Down
112 changes: 63 additions & 49 deletions src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,25 @@ public partial class EditorForm<TModel> : IShowLabel
/// </summary>
private readonly List<IEditorItem> _editorItems = [];

/// <summary>
/// 获得/设置 渲染的编辑项集合
/// </summary>
[NotNull]
private List<IEditorItem>? _formItems = null;
private IEnumerable<IEditorItem> UnsetGroupItems => RenderItems.Where(i => string.IsNullOrEmpty(i.GroupName) && i.IsVisible(ItemChangedType, IsSearch.Value));

private IEnumerable<IEditorItem> UnsetGroupItems => _formItems.Where(i => string.IsNullOrEmpty(i.GroupName) && i.IsVisible(ItemChangedType, IsSearch.Value));

private IEnumerable<KeyValuePair<string, IOrderedEnumerable<IEditorItem>>> GroupItems => _formItems
private IEnumerable<KeyValuePair<string, IOrderedEnumerable<IEditorItem>>> GroupItems => RenderItems
.Where(i => !string.IsNullOrEmpty(i.GroupName) && i.IsVisible(ItemChangedType, IsSearch.Value))
.GroupBy(i => i.GroupOrder).OrderBy(i => i.Key)
.Select(i => new KeyValuePair<string, IOrderedEnumerable<IEditorItem>>(i.First().GroupName!, i.OrderBy(x => x.Order)));

private bool _inited;
private List<IEditorItem>? _itemsCache;

private List<IEditorItem> RenderItems
{
get
{
_itemsCache ??= GetRenderItems();
return _itemsCache;
}
}

/// <summary>
/// OnInitialized 方法
/// </summary>
Expand All @@ -217,7 +223,6 @@ protected override void OnInitialized()

// 统一设置所有 IEditorItem 的 PlaceHolder
PlaceHolderText ??= Localizer[nameof(PlaceHolderText)];

IsSearch ??= false;
}

Expand All @@ -230,66 +235,75 @@ protected override void OnParametersSet()

// 为空时使用级联参数 ValidateForm 的 ShowLabel
ShowLabel ??= ValidateForm?.ShowLabel;
_formItems = null;
_itemsCache = null;
}

private bool _inited;

private Task OnRenderAsync(bool firstRender)
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="firstRender"></param>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
foreach (var item in RenderItems)
{
if (item.Lookup == null && !string.IsNullOrEmpty(item.LookupServiceKey))
{
var lookupServcie = item.LookupService ?? LookupService;
item.Lookup = await lookupServcie.GetItemsAsync(item.LookupServiceKey, item.LookupServiceData);
}
}
_inited = true;
StateHasChanged();
}
return Task.CompletedTask;
}

private void ResetItems()
private List<IEditorItem> GetRenderItems()
{
if (_formItems == null)
var items = new List<IEditorItem>();
if (Items != null)
{
_formItems = [];
if (Items != null)
{
_formItems.AddRange(Items.Where(i => !i.GetIgnore() && !string.IsNullOrEmpty(i.GetFieldName())));
}
else
items.AddRange(Items.Where(i => !i.GetIgnore() && !string.IsNullOrEmpty(i.GetFieldName())));
}
else
{
// 如果 EditorItems 有值表示 用户自定义列
if (AutoGenerateAllItem)
{
// 如果 EditorItems 有值表示 用户自定义列
if (AutoGenerateAllItem)
{
// 获取绑定模型所有属性
var items = Utility.GetTableColumns<TModel>(defaultOrderCallback: ColumnOrderCallback).ToList();
// 获取绑定模型所有属性
var columns = Utility.GetTableColumns<TModel>(defaultOrderCallback: ColumnOrderCallback).ToList();

// 通过设定的 FieldItems 模板获取项进行渲染
foreach (var el in _editorItems)
// 通过设定的 FieldItems 模板获取项进行渲染
foreach (var el in _editorItems)
{
var item = columns.FirstOrDefault(i => i.GetFieldName() == el.GetFieldName());
if (item != null)
{
var item = items.FirstOrDefault(i => i.GetFieldName() == el.GetFieldName());
if (item != null)
// 过滤掉不编辑与不可见的列
if (el.GetIgnore() || !el.IsVisible(ItemChangedType, IsSearch.Value) || string.IsNullOrEmpty(el.GetFieldName()))
{
// 过滤掉不编辑与不可见的列
if (el.GetIgnore() || !el.IsVisible(ItemChangedType, IsSearch.Value))
{
items.Remove(item);
}
else
{
// 设置只读属性与列模板
item.CopyValue(el);
}
columns.Remove(item);
}
else
{
// 设置只读属性与列模板
item.CopyValue(el);
}
}
_formItems.AddRange(items.Where(i => !string.IsNullOrEmpty(i.GetFieldName())));
}
else
{
_formItems.AddRange(_editorItems.Where(i => !i.GetIgnore()
&& !string.IsNullOrEmpty(i.GetFieldName())
&& i.IsVisible(ItemChangedType, IsSearch.Value)));
}
items.AddRange(columns);
}
else
{
items.AddRange(_editorItems.Where(i => !i.GetIgnore()
&& !string.IsNullOrEmpty(i.GetFieldName())
&& i.IsVisible(ItemChangedType, IsSearch.Value)));
}
}
return items;
}

private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>
Expand All @@ -301,7 +315,7 @@ private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder =>
else
{
item.PlaceHolder ??= PlaceHolderText;
builder.CreateComponentByFieldType(this, item, Model, ItemChangedType, IsSearch.Value, LookupService);
builder.CreateComponentByFieldType(this, item, Model, ItemChangedType, IsSearch.Value);
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/BootstrapBlazor/Components/EditorForm/EditorItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ public class EditorItem<TValue> : ComponentBase, IEditorItem
[Parameter]
public object? LookupServiceData { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
[Parameter]
public ILookupService? LookupService { get; set; }

/// <summary>
/// 获得/设置 自定义验证集合
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public interface IEditorItem
/// </summary>
object? LookupServiceData { get; set; }

/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务实例
/// </summary>
ILookupService? LookupService { get; set; }

/// <summary>
/// 获得/设置 自定义验证集合
/// </summary>
Expand Down
15 changes: 9 additions & 6 deletions src/BootstrapBlazor/Components/Filters/LookupFilter.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
@namespace BootstrapBlazor.Components
@inherits FilterBase

@if (IsHeaderRow)
@if(Items != null)
{
<Select Items="@Items" StringComparison="LookupStringComparison" @bind-Value="@Value" OnSelectedItemChanged="_ => OnFilterValueChanged()" IsPopover="true" ShowSearch="IsShowSearch"></Select>
}
else
{
<Select Items="@Items" StringComparison="LookupStringComparison" @bind-Value="@Value" ShowSearch="IsShowSearch" ></Select>
if (IsHeaderRow)
{
<Select Items="@Items" StringComparison="LookupStringComparison" @bind-Value="@Value" OnSelectedItemChanged="_ => OnFilterValueChanged()" IsPopover="true" ShowSearch="IsShowSearch"></Select>
}
else
{
<Select Items="@Items" StringComparison="LookupStringComparison" @bind-Value="@Value" ShowSearch="IsShowSearch" ></Select>
}
}
56 changes: 40 additions & 16 deletions src/BootstrapBlazor/Components/Filters/LookupFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@ public partial class LookupFilter
/// <summary>
/// 获得/设置 相关枚举类型
/// </summary>
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
[Parameter]
[NotNull]
public IEnumerable<SelectedItem>? Lookup { get; set; }

/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务实例
/// </summary>
[Parameter]
[NotNull]
public ILookupService? LookupService { get; set; }

/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作,可以通过 <see cref="LookupServiceData"/> 传递自定义数据
/// </summary>
[Parameter]
[NotNull]
public string? LookupServiceKey { get; set; }

/// <summary>
/// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值自定义数据,通过 <see cref="LookupServiceKey"/> 指定键值
/// </summary>
[Parameter]
[NotNull]
public object? LookupServiceData { get; set; }

/// <summary>
/// 获得/设置 字典数据源字符串比较规则 默认 StringComparison.OrdinalIgnoreCase 大小写不敏感
/// </summary>
Expand All @@ -33,9 +51,7 @@ public partial class LookupFilter
/// <summary>
/// 获得/设置 相关枚举类型
/// </summary>
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
[Parameter]
[NotNull]
public Type? Type { get; set; }
Expand All @@ -50,18 +66,17 @@ public partial class LookupFilter
[NotNull]
private IStringLocalizer<TableFilter>? Localizer { get; set; }

[Inject]
[NotNull]
private ILookupService? InjectLookupService { get; set; }

/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();

if (Lookup == null)
{
throw new InvalidOperationException("the Parameter Lookup must be set.");
}

if (Type == null)
{
throw new InvalidOperationException("the Parameter Type must be set.");
Expand All @@ -76,19 +91,28 @@ protected override void OnInitialized()
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnParametersSet()
protected override async Task OnParametersSetAsync()
{
base.OnParametersSet();
await base.OnParametersSetAsync();

if (Items == null)
{
var items = new List<SelectedItem>
var items = new List<SelectedItem>
{
new("", Localizer["EnumFilter.AllText"].Value)
};
if (Lookup != null)
{
items.AddRange(Lookup);
Items = items;
}
else if (!string.IsNullOrEmpty(LookupServiceKey))
{
var lookupService = LookupService ?? InjectLookupService;
var lookup = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData);
if (lookup != null)
{
items.AddRange(lookup);
}
}
Items = items;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Filters/TableFilter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ else
}
else if (IsLookup)
{
<LookupFilter Lookup="_lookup.Value" LookupStringComparison="Column.LookupStringComparison" Type="Column.PropertyType" IsShowSearch="Column.ShowSearchWhenSelect"></LookupFilter>
<LookupFilter Lookup="Column.Lookup" LookupService="Column.LookupService" LookupServiceKey="Column.LookupServiceKey" LookupServiceData="Column.LookupServiceData" LookupStringComparison="Column.LookupStringComparison" Type="Column.PropertyType" IsShowSearch="Column.ShowSearchWhenSelect"></LookupFilter>
}
else
{
Expand Down
13 changes: 0 additions & 13 deletions src/BootstrapBlazor/Components/Filters/TableFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ public partial class TableFilter : IFilter
/// </summary>
[Parameter]
[NotNull]
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
public ITableColumn? Column { get; set; }

/// <summary>
Expand Down Expand Up @@ -132,20 +130,11 @@ public partial class TableFilter : IFilter
[NotNull]
private IIconTheme? IconTheme { get; set; }

[Inject]
[NotNull]
private ILookupService? LookupService { get; set; }

/// <summary>
/// 组件步长
/// </summary>
private string? _step;

/// <summary>
/// 外键数据源集合
/// </summary>
private Lazy<IEnumerable<SelectedItem>?> _lookup = default!;

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand All @@ -156,8 +145,6 @@ protected override void OnInitialized()
_title = Column.GetDisplayName();
FieldKey = Column.GetFieldName();
Column.Filter = this;

_lookup = new(() => Column.Lookup ?? LookupService.GetItemsByKey(Column.LookupServiceKey, Column.LookupServiceData));
_step = Column.Step;
}

Expand Down
Loading

0 comments on commit dc943bb

Please sign in to comment.