Skip to content

Commit a238746

Browse files
committed
refactor: 增加虚拟化逻辑
1 parent abb0d75 commit a238746

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<div class="dropdown-menu-body dropdown-virtual">
2525
@if (OnQueryAsync == null)
2626
{
27-
<Virtualize ItemSize="RowHeight" OverscanCount="OverscanCount" Items="@GetVirtualItems()" ChildContent="RenderRow">
27+
<Virtualize ItemSize="RowHeight" OverscanCount="OverscanCount" Items="@Rows" ChildContent="RenderRow">
2828
</Virtualize>
2929
}
3030
else

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,32 +174,18 @@ private async Task OnClickItem(TValue val)
174174
private List<TValue> Rows => _filterItems ?? [.. Items];
175175

176176
private int _totalCount;
177-
private TValue? _itemsCache;
178-
private ItemsProviderResult<TValue> _result;
179177

180178
private async ValueTask<ItemsProviderResult<TValue>> LoadItems(ItemsProviderRequest request)
181179
{
182-
// 有搜索条件时使用原生请求数量
183-
// 有总数时请求剩余数量
184180
var count = _totalCount == 0 ? request.Count : Math.Min(request.Count, _totalCount - request.StartIndex);
185-
var data = await OnQueryAsync(new() { StartIndex = request.StartIndex, Count = count });
181+
var data = await OnQueryAsync(new() { StartIndex = request.StartIndex, Count = count, SearchText = _searchText });
186182

187-
_itemsCache = default;
188183
_totalCount = data.TotalCount;
189184
var items = data.Items ?? [];
190-
_result = new ItemsProviderResult<TValue>(items, _totalCount);
191-
return _result;
185+
return new ItemsProviderResult<TValue>(items, _totalCount);
192186
}
193187

194-
private List<TValue> GetVirtualItems()
195-
{
196-
var items = new List<TValue>();
197-
if (Items != null)
198-
{
199-
items.AddRange(Items);
200-
}
201-
return items;
202-
}
188+
private string? _searchText;
203189

204190
/// <summary>
205191
/// Triggers the filter method.
@@ -208,6 +194,14 @@ private List<TValue> GetVirtualItems()
208194
[JSInvokable]
209195
public override async Task TriggerFilter(string val)
210196
{
197+
if (OnQueryAsync != null)
198+
{
199+
_searchText = val;
200+
await _virtualizeElement.RefreshDataAsync();
201+
StateHasChanged();
202+
return;
203+
}
204+
211205
if (OnCustomFilter != null)
212206
{
213207
var items = await OnCustomFilter(val);
@@ -226,7 +220,7 @@ public override async Task TriggerFilter(string val)
226220
_filterItems = [.. items];
227221
}
228222

229-
if (DisplayCount != null)
223+
if (!IsVirtualize && DisplayCount != null)
230224
{
231225
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
232226
}

0 commit comments

Comments
 (0)