Skip to content

Commit cc7e50e

Browse files
committed
refactor: 重构搜索数据集逻辑
1 parent 1a777be commit cc7e50e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
151151
/// </summary>
152152
/// <remarks>通过设置 <see cref="ShowSearch"/> 开启</remarks>
153153
[Parameter]
154-
public Func<string?, Task>? OnSearchAsync { get; set; }
154+
public Func<string?, Task<List<TreeViewItem<TItem>>>>? OnSearchAsync { get; set; }
155155

156156
/// <summary>
157157
/// 获得/设置 页面刷新是否重置已加载数据 默认 false
@@ -609,21 +609,25 @@ private async Task OnEnterAsync(string? searchText)
609609

610610
private Task OnEscAsync(string? searchText) => OnClickResetSearch();
611611

612+
private List<TreeViewItem<TItem>>? _searchItems;
613+
612614
private async Task OnClickSearch()
613615
{
614616
if (OnSearchAsync != null)
615617
{
616-
await OnSearchAsync(_searchText);
618+
_searchItems = await OnSearchAsync(_searchText);
619+
_rows = null;
620+
StateHasChanged();
617621
}
618622
}
619623

620-
private async Task OnClickResetSearch()
624+
private Task OnClickResetSearch()
621625
{
622626
_searchText = null;
623-
if (OnSearchAsync != null)
624-
{
625-
await OnSearchAsync(_searchText);
626-
}
627+
_searchItems = null;
628+
_rows = null;
629+
StateHasChanged();
630+
return Task.CompletedTask;
627631
}
628632

629633
/// <summary>
@@ -862,11 +866,13 @@ private List<TreeViewItem<TItem>> Rows
862866
get
863867
{
864868
// 扁平化数据集合
865-
_rows ??= Items.ToFlat<TItem>().ToList();
869+
_rows ??= GetItems().ToFlat<TItem>();
866870
return _rows;
867871
}
868872
}
869873

874+
private List<TreeViewItem<TItem>> GetItems() => _searchItems ?? Items;
875+
870876
private static string? GetTreeRowStyle(TreeViewItem<TItem> item)
871877
{
872878
var level = 0;

0 commit comments

Comments
 (0)