
AutoComplete
The input box autocompletes the function
By setting Items
data collection when the user types information that is automatically displayed
Demo
In this example, type 123 strings to display the viewing effect, automatically give the component initialization to the auto-prompt dataset, and the dataset does not change
By setting the IsLikeMatch
value settings to turn on fuzzy matching of collections, you control whether case is ignored by setting the IgnoreCase
Demo
In this example, type the abc string to display the viewing effect and select all matches in the collection that contain abc and have the same case
By setting the NoDataTip
value setting automatically completes the custom prompt message that appears when the data is not found
Demo
In this example, type 567 strings because the autocomplete information center does not display custom prompt information - the data you want is not found
By setting up ValueChanged
callback method reorganizes the data collection based on the data entered by the user before prompting for information
Demo
In this example, type any string to display the viewing effect, and automatically complete the component's dynamic changes from the newly obtained prompt dataset based on the string you type
When a component binds in both directions, it automatically determines whether label text is displayed based on the conditions
Demo
The pre-label explicit rules are consistent with the BootstrapInput
component of the [portal]
Set Debounce
value turn on debounce
Demo
In this example, please type any string to display the viewing effect. Within the anti shake time, the auto completion component will only send the results to the back end after the last entry, which will greatly improve the performance
Click the dropdown item or Enter trigger the callback
Demo
Attributes
@page "/autocompletes"
@inject IStringLocalizer<AutoCompletes> Localizer
<h3>AutoComplete</h3>
<h4>The input box autocompletes the function</h4>
<DemoBlock Title="Basic usage" Introduction="By setting <code>Items</code> data collection when the user types information that is automatically displayed" Name="Normal">
<p>In this example, type 123 strings to display the viewing effect, automatically give the component initialization to the auto-prompt dataset, and the dataset does not change</p>
<div style="width: 200px;">
<AutoComplete Items="@StaticItems" IsSelectAllTextOnFocus="true" />
</div>
</DemoBlock>
<DemoBlock Title="Blur queries and ignore case" Introduction="By setting the <code>IsLikeMatch</code> value settings to turn on fuzzy matching of collections, you control whether case is ignored by setting the <code>IgnoreCase</code>" Name="LikeMatch">
<p>In this example, type the abc string to display the viewing effect and select all matches in the collection that contain abc and have the same case</p>
<div style="width: 200px;">
<AutoComplete Items="@StaticItems" IsLikeMatch="true" IgnoreCase="false" />
</div>
</DemoBlock>
<DemoBlock Title="Custom prompt message" Introduction="By setting the <code>NoDataTip</code> value setting automatically completes the custom prompt message that appears when the data is not found" Name="NoDataTip">
<p>In this example, type 567 strings because the autocomplete information center does not display custom prompt information - <code>the data you want is not found</code></p>
<div style="width: 200px;">
<AutoComplete Items="@StaticItems" NoDataTip="There is nothing" />
</div>
</DemoBlock>
<DemoBlock Title="Custom candidates" Introduction="By setting up <code>ValueChanged</code> callback method reorganizes the data collection based on the data entered by the user before prompting for information" Name="ValueChanged">
<p>In this example, type any string to display the viewing effect, and automatically complete the component's dynamic changes from the newly obtained prompt dataset based on the string you type</p>
<div style="width: 200px;">
<AutoComplete Items="@Items" ValueChanged="@OnValueChanged" />
</div>
</DemoBlock>
<DemoBlock Title="The label is displayed" Introduction="When a component binds in both directions, it automatically determines whether label text is displayed based on the conditions" Name="ShowLabel">
<p>The pre-label explicit rules are consistent with the <code>BootstrapInput</code> component of the <a href='inputs'>[portal]</a></p>
<Divider Text="Bidirectional binding displays labels" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<ValidateForm Model="@Model">
<AutoComplete Items="@StaticItems" @bind-Value="@Model.Name" ShowLabel="true" />
</ValidateForm>
<Divider Text="Bidirectional bindings do not display labels" Alignment="Alignment.Left" style="margin: 2rem 0;" />
<AutoComplete Items="@StaticItems" @bind-Value="@Model.Name" ShowLabel="false" />
<Divider Text="Customize DisplayText" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<AutoComplete Items="@StaticItems" @bind-Value="@Model.Name" DisplayText="Custom city" ShowLabel="true" />
</DemoBlock>
<DemoBlock Title="Debounce" Introduction="Set <code>Debounce</code> value turn on debounce" Name="Debounce">
<p>In this example, please type any string to display the viewing effect. Within the anti shake time, the auto completion component will only send the results to the back end after the last entry, which will greatly improve the performance</p>
<div style="width: 200px;">
<AutoComplete Items="@Items" ValueChanged="@OnValueChanged" Debounce="500" />
</div>
</DemoBlock>
<DemoBlock Title="OnSelectedItemChanged" Introduction="Click the dropdown item or <kbd>Enter</kbd> trigger the callback" Name="OnSelectedItemChanged">
<AutoComplete Items="@StaticItems" OnSelectedItemChanged="OnSelectedItemChanged"></AutoComplete>
<BlockLogger @ref="Trace" class="mt-3" />
</DemoBlock>
<AttributeTable Items="@GetAttributes()" />
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components;
using BootstrapBlazor.Shared.Common;
using BootstrapBlazor.Shared.Components;
namespace BootstrapBlazor.Shared.Samples;
/// <summary>
///
/// </summary>
public sealed partial class AutoCompletes
{
private readonly List<string> _items = new();
private IEnumerable<string> Items => _items;
private Foo Model { get; set; } = new Foo() { Name = "" };
private static List<string> StaticItems => new() { "1", "12", "123", "1234", "12345", "123456", "abc", "abcdef", "ABC", "aBcDeFg", "ABCDEFG" };
[NotNull]
private BlockLogger? Trace { get; set; }
private Task OnSelectedItemChanged(string val)
{
Trace.Log($"Value: {val}");
return Task.CompletedTask;
}
private Task OnValueChanged(string val)
{
_items.Clear();
_items.Add($"{val}@163.com");
_items.Add($"{val}@126.com");
_items.Add($"{val}@sina.com");
_items.Add($"{val}@hotmail.com");
return Task.CompletedTask;
}
/// <summary>
/// 获得属性方法
/// </summary>
/// <returns></returns>
private IEnumerable<AttributeItem> GetAttributes() => new AttributeItem[]
{
// TODO: 移动到数据库中
new AttributeItem() {
Name = "ShowLabel",
Description = Localizer["Att1"],
Type = "bool",
ValueList = "true|false",
DefaultValue = "true"
},
new AttributeItem() {
Name = "ChildContent",
Description = Localizer["Att2"],
Type = "RenderFragment",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "Items",
Description = Localizer["Att3"],
Type = "IEnumerable<string>",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "NoDataTip",
Description = Localizer["Att4"],
Type = "string",
ValueList = " — ",
DefaultValue = Localizer["Att4DefaultValue"]!
},
new AttributeItem() {
Name = "DisplayCount",
Description = Localizer["Att5"],
Type = "int?",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "ValueChanged",
Description = Localizer["Att6"],
Type = "Action<string>",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "IsLikeMatch",
Description = Localizer["Att7"],
Type = "bool",
ValueList = "true|false",
DefaultValue = "false"
},
new AttributeItem()
{
Name = "IgnoreCase",
Description = Localizer["Att8"],
Type = "bool",
ValueList = "true|false",
DefaultValue = "true"
},
new AttributeItem()
{
Name = "OnCustomFilter",
Description = Localizer["Att9"],
Type = "Func<string, Task<IEnumerable<string>>>",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem()
{
Name = "Debounce",
Description = Localizer["Debounce"],
Type = "int",
ValueList = " — ",
DefaultValue = "0"
},
new AttributeItem()
{
Name = nameof(AutoComplete.SkipEnter),
Description = Localizer[nameof(AutoComplete.SkipEnter)],
Type = "bool",
ValueList = "true/false",
DefaultValue = "false"
},
new AttributeItem()
{
Name = nameof(AutoComplete.SkipEsc),
Description = Localizer[nameof(AutoComplete.SkipEsc)],
Type = "bool",
ValueList = "true/false",
DefaultValue = "false"
},
new AttributeItem()
{
Name = nameof(AutoComplete.OnValueChanged),
Description = Localizer[nameof(AutoComplete.OnValueChanged)],
Type = "Func<string, Task>",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem()
{
Name = nameof(AutoComplete.OnSelectedItemChanged),
Description = Localizer[nameof(AutoComplete.OnSelectedItemChanged)],
Type = "Func<string, Task>",
ValueList = " — ",
DefaultValue = " — "
}
};
}