繁体   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