我在这里有一个Autocomplete的示例,该示例取自MudBlazor组件库文档,该文档提供了我在预定义的可能值列表中键入的选项:
<MudAutocomplete T="string" Label="Select US state" @bind-Value="selectedValue" SearchFunc="@Search" />
@code {
private string selectedValue;
private string[] states =
{
"Alabama", "Alaska", "American Samoa", "Arizona",
"Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Federated States of Micronesia",
"Florida", "Georgia", "Guam", "Hawaii", "Idaho",
"Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Marshall Islands", "Maryland",
"Massachusetts", "Michigan", "Minnesota", "Mississippi",
"Missouri", "Montana", "Nebraska", "Nevada",
"New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Northern Mariana Islands", "Ohio",
"Oklahoma", "Oregon", "Palau", "Pennsylvania", "Puerto Rico",
"Rhode Island", "South Carolina", "South Dakota", "Tennessee",
"Texas", "Utah", "Vermont", "Virgin Island", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming",
};
private Task<IEnumerable<string>> Search(string value)
{
return Task.FromResult(states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)));
}
}
这正是我的项目所需要的。但是,当用户键入内容时,如何从Web api中按需获取值?另外,有没有办法影响下拉列表中选项的显示方式?
答案 0 :(得分:1)
使用“搜索”方法提出请求。将return Task.FromResult(states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)));
行替换为对API的调用结果。