簡體   English   中英

Blazor WASM MudBlazor UI 自動完成大數據集/虛擬化

[英]Blazor WASM MudBlazor UI Autocomplete large data set/virtualization

我有一個 Blazor WASM 項目,我正在嘗試讓 MudBlazor 自動完成組件處理非常大的數據集。 api 端點最多可以返回 10,000 個結果,因此我不想在 OnInitializedAsync() 方法中加載所有結果。 我想使用 MudBlazor Autocomplete SearchFunc 將 go 輸出到 api 並返回一個列表,然后過濾結果。

程序.cs

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddMudServices();
builder.Services.AddScoped<CustomAuthorizationMessageHandler>();
//TEST CONFIGURATION
builder.Services.AddHttpClient("ServerAPI",
      client => client.BaseAddress = new Uri("https://localhost:7083"))
    .AddHttpMessageHandler<CustomAuthorizationMessageHandler>();

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
  .CreateClient("ServerAPI"));

await builder.Build().RunAsync();

AppPage.razor

public ProductMaster selectedProduct { get; set; } = new ProductMaster();

<MudAutocomplete 
            T="ProductMaster" 
            Label="Product" 
            @bind-Value="selectedProduct" 
            SearchFunc="@ProductSearch1"
            ToStringFunc="@(e => e == null ? "EMPTY" : $"{e.ProdDesc} {e.ProdCode}")"
            Variant="Variant.Outlined" 
            ShowProgressIndicator="true"
            ProgressIndicatorColor="Color.Default" /> 

private async Task<IEnumerable<ProductMaster>> ProductSearch1(string value)
        {
            var productList = await Http.GetFromJsonAsync<IList<ProductMaster>>($"/api/Orders/GetProducts/?companyNumber={Order.Company}&prodDesc={value}");

            if (string.IsNullOrEmpty(value))
                return productList;

            return productList.Where(x => x.ProdDesc.Contains(value, StringComparison.InvariantCultureIgnoreCase));
        }

問題是,在 ProductSearch1 方法中,http 請求永遠不會消失。 我不明白為什么它不會執行。 如果我在受保護的覆蓋異步任務 OnInitializedAsync()方法上加載 api 的所有結果,然后在用戶鍵入時過濾結果,它工作正常。 但是,這對我來說行不通,數據集太大了。

對此有什么想法嗎?

謝謝

我能夠通過將 IHttpClientFactory 的依賴項注入從 Scoped 更改為 Singleton 來解決此問題:

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>()
  .CreateClient("ServerAPI"));

builder.Services.AddSingleton(sp => sp.GetRequiredService<IHttpClientFactory>()
  .CreateClient("ServerAPI"));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM