我在表单上使用 MudAutocomplete 控件。现在,当用户单击网格上的编辑图标时,我将显示一个弹出屏幕,其中包含一个预先填写所选记录数据的表单。现在,所有其他控件都预先填充了绑定值,但对于 MudAutocomplete 控件,它不会发生。请在这里帮助我如何实现此功能。
问问题
222 次
1 回答
0
这是一个简单的片段,展示了如何将值加载到自动完成中。您可以在https://try.mudblazor.com/snippet/cOcFbFbUhRRLfdsj使用该片段
<MudGrid>
<MudItem xs="12" sm="6">
<MudButton OnClick="LoadFormData">
Load Form Data
</MudButton>
</MudItem>
<MudItem xs="12" sm="6" md="4">
<MudAutocomplete T="string" Label="US States" @bind-Value="_value"
SearchFunc="@Search1" Variant="Variant.Outlined" />
</MudItem>
</MudGrid>
@code {
string _value = null;
private void LoadFormData()
{
_value="New Mexico";
StateHasChanged();
}
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 async Task<IEnumerable<string>> Search1(string value)
{
// In real life use an asynchronous function for fetching data from an api.
await Task.Delay(5);
// if text is null or empty, show complete list
if (string.IsNullOrEmpty(value))
return states;
return states.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}
}
当您单击“加载表单数据”时,自动完成功能会预先填充该值。如果这不能解决您的问题,您需要创建一个http://try.mudblazor.com片段并将其发布在此处,以便我们查看您的问题。只要确保你保持简单。
于 2021-11-16T20:26:27.573 回答