我在这里有一个从 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 按需获取值?另外,有没有办法影响选项在下拉列表中的显示方式?