
Select
When there are too many options, use the drop-down menu to present and select content
Drop-down selection boxes are available in a variety of colors
Demo
In this example, the first drop-down box does not have a Value
bidirectional binding, so you only change when you select different options, and the remaining drop-down boxes share the same data sourceItems
and bind Value
values in both directions, changing together when you select different options
Selector unavailable state
Demo
The options in the drop-down box disable theexample
The values in the text box change as you change the drop-down option by binding the Model.Name
property to the component with Select
.
Demo
The second drop-down box dynamically populates the content by selecting the different options for the first drop-down box.
Demo
When the drop-down box is not selected, it is blocked when the submit button is clicked.
Demo
Alternatives are presented in groups
Demo
The component binding value is an example of a Guid structure
Demo
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 of the [The portal]
Hard-coded writing directly inside the Select
component is suitable for static data drop-down boxes
Demo
an example of a type enumerated a select
component binding
Demo
Enumeration When the binding value is an empty enumeration type, the component automatically adds preferences internally through the PlaceHolder
value, and when the PlaceHolder
value is not set, please select ... as a preference, and this example binds the EnumEducation
enumeration type data
Setting PalceHolder
is not valid when the binding value is an enumerated type
an example of the Select
component binding int?
type
Demo
When the first option is selected, the binding value SelectIntItem
to null
an example of the bool?
type of select
component binding
Demo
Can be used for the empty boolean type more than in the conditional search box
When the first option is selected, the binding value SelectIntItem
to null
By setting the ItemTemplate
you can customize the option rendering style
Demo
Controls whether the search box is displayed by setting the ShowSearch
property, which is not displayed by default false
Demo
By default, the component will maintain the SearchText
. You can set the AutoClearSearchText
parameter to control the automatic emptying of the search content when click the select item
Block changes to the current value by setting the OnBeforeSelectedItemChange
delegate.
Demo
Display data of Timezone
Demo
Attributes
Event
@page "/selects"
@inject DialogService Dialog
<h3>Select</h3>
<h4>When there are too many options, use the drop-down menu to present and select content</h4>
<DemoBlock Title="Select drop-down selection box" Introduction="Drop-down selection boxes are available in a variety of colors" Name="Normal">
<p>In this example, the first drop-down box does not have a <code>Value</code> bidirectional binding, so you only change when you select different options, and the remaining drop-down boxes share the same data source<code>Items</code> and bind <code>Value</code> values in both directions, changing together when you select different options</p>
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Primary" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Success" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Danger" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Warning" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Info" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Secondary" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<Select Color="Color.Dark" Items="Items" OnSelectedItemChanged="OnItemChanged" @bind-Value="Model.Name"></Select>
</div>
</div>
<BlockLogger @ref="Trace" class="mt-3" />
</DemoBlock>
<DemoBlock Title="Select disables the drop-down box" Introduction="Selector unavailable state" Name="Disable">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Primary" Items="Items" IsDisabled="true"></Select>
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Success" Items="Items" IsDisabled="true"></Select>
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Danger" Items="Items" IsDisabled="true"></Select>
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Warning" Items="Items" IsDisabled="true"></Select>
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Info" Items="Items" IsDisabled="true"></Select>
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Secondary" Items="Items" IsDisabled="true"></Select>
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Dark" Items="Items" IsDisabled="true"></Select>
</div>
</div>
<p class="mt-3">The options in the drop-down box <b>disable the</b>example</p>
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items4" />
</div>
</div>
</DemoBlock>
<DemoBlock Title="Select two-way binding" Introduction="The values in the text box change as you change the drop-down option by binding the <code>Model.Name</code> property to the component with <code>Select</code> ." Name="Binding">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name"></Select>
</div>
<div class="col-12 col-sm-6">
<BootstrapInput readonly @bind-Value="Model.Name" />
</div>
</div>
</DemoBlock>
<DemoBlock Title="Select cascading binding" Introduction="The second drop-down box dynamically populates the content by selecting the different options for the first drop-down box." Name="Cascading">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items3" OnSelectedItemChanged="OnCascadeBindSelectClick" />
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items2" />
</div>
<div class="col-12">
<Button Text="In Dialog" OnClickWithoutRender="OnShowDialog" />
</div>
</div>
</DemoBlock>
<DemoBlock Title="Select client validation" Introduction="When the drop-down box is not selected, it is blocked when the submit button is clicked." Name="ClientValidation">
<ValidateForm Model="BindModel">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select @bind-Value="BindModel.Name">
<Options>
<SelectOption Text="Please select ..." Value="" />
<SelectOption Text="Beijing" Value="1" />
<SelectOption Text="Shanghai" Value="2" />
<SelectOption Text="Guangzhou" Value="3" />
</Options>
</Select>
</div>
<div class="col-12 col-sm-6 align-self-end">
<Button ButtonType="ButtonType.Submit">Submit</Button>
</div>
</div>
</ValidateForm>
</DemoBlock>
<DemoBlock Title="Grouping" Introduction="Alternatives are presented in groups" Name="Group">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Primary" Items="GroupItems">
</Select>
</div>
</div>
</DemoBlock>
<DemoBlock Title="The binding generic is the Guid structure" Introduction="The component binding value is an example of a Guid structure" Name="Guid">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select Color="Color.Primary" Items="GuidItems" @bind-Value="CurrentGuid" />
</div>
<div class="col-12 col-sm-6">
<div class="form-control">@CurrentGuid</div>
</div>
</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="DispalyLabel">
<p>The pre-label explicit rules are consistent with the <code>BootstrapInput</code> component of the of the <a href='inputs'>[The portal]</a></p>
<Divider Text="Bidirectional binding displays labels" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<ValidateForm Model="Model">
<div class="row g-3">
<div class="col-12">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name" />
</div>
</div>
</ValidateForm>
<Divider Text="Bidirectional bindings show that bidirectional bindings do not display labels" Alignment="Alignment.Left" style="margin: 2rem 0;" />
<div class="row g-3">
<div class="col-12">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name" />
</div>
</div>
<Divider Text="Customize DisplayText" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<div class="row g-3">
<div class="col-12">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name" DisplayText="Custom city" ShowLabel="true" />
</div>
</div>
</DemoBlock>
<DemoBlock Title="Static data" Introduction="Hard-coded writing directly inside the <code>Select</code> component is suitable for static data drop-down boxes" Name="Static">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string">
<Options>
<SelectOption Text="Please select ..." Value="1" />
<SelectOption Text="Beijing" Value="2" Active="true" />
<SelectOption Text="Shanghai" Value="3" />
</Options>
</Select>
</div>
</div>
</DemoBlock>
<DemoBlock Title="Enumerate the data" Introduction="an example of a type enumerated a <code>select</code> component binding" Name="Enum">
<p>Enumeration When the binding value is an empty enumeration type, the component automatically adds preferences internally through the <code>PlaceHolder</code> value, and when the <code>PlaceHolder</code> value is not set, <b>please select ... </b>as a preference, and this example binds <code>the EnumEducation</code> enumeration type data</p>
<p>Setting <code>PalceHolder</code> is not valid when the binding value is an enumerated type</p>
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select @bind-Value="SelectedEnumItem1" PlaceHolder="Not selected" ShowLabel="true" DisplayText="Can be empty">
</Select>
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="SelectedEnumItem1" ShowLabel="true" DisplayText="Can be empty">
</Select>
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="SelectedEnumItem" ShowLabel="true" DisplayText="Not empty">
</Select>
</div>
<div class="col-12 col-sm-6 align-self-end">
<div class="form-control">@SelectedEnumItem</div>
</div>
</div>
</DemoBlock>
<DemoBlock Title="The binding can be an empty type" Introduction="<code>an example of the Select</code> component binding <code>int?</code> type" Name="Nullable">
<p>When the first option is selected, the binding value <code>SelectIntItem</code> to <code>null</code></p>
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select Items="NullableIntItems" @bind-Value="SelectedIntItem">
</Select>
</div>
<div class="col-12 col-sm-6">
<div class="form-control">@GetSelectedIntItemString()</div>
</div>
</div>
</DemoBlock>
<DemoBlock Title="The binding can be an empty Boolean type" Introduction="an example of the <code>bool?</code> type of <code>select</code> component binding" Name="NullableBoolean">
<p>Can be used for the empty boolean type more than in the conditional search box</p>
<p>When the first option is selected, the binding value <code>SelectIntItem</code> to <code>null</code></p>
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select Items="NullableBoolItems" @bind-Value="SelectedBoolItem">
</Select>
</div>
<div class="col-12 col-sm-6">
<div class="form-control">@GetSelectedBoolItemString()</div>
</div>
</div>
</DemoBlock>
<DemoBlock Title="Custom option templates" Introduction="By setting the <code>ItemTemplate</code> you can customize the option rendering style" Name="CunstomTemplate">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items">
<ItemTemplate>
<div class="dropdown-item-demo">
<i class="fa fa-fa"></i>
<span>@context.Text</span>
<i class="fa fa-star"></i>
</div>
</ItemTemplate>
</Select>
</div>
</div>
</DemoBlock>
<DemoBlock Title="Drop-down box with search box" Introduction="Controls whether the search box is displayed by setting the <code>ShowSearch</code> property, which is not displayed by default false" Name="ShowSearch">
<p>By default, the component will maintain the <code>SearchText</code>. You can set the <code>AutoClearSearchText</code> parameter to control the automatic emptying of the search content when click the select item</p>
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items" ShowSearch="true" />
</div>
<div class="col-12 col-sm-6">
<Select TValue="string" Items="StringItems" ShowSearch="true" AutoClearSearchText="true" />
</div>
</div>
</DemoBlock>
<DemoBlock Title="Drop-down box with confirmation" Introduction="Block changes to the current value by setting the <code>OnBeforeSelectedItemChange</code> delegate." Name="ConfirmSelect">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items" OnBeforeSelectedItemChange="@OnBeforeSelectedItemChange"
SwalTitle="The drop-down box value changes" SwalContent="Are you sure you want to change the option value?" SwalFooter="Click Confirm to change the option value and select Cancel to leave the value unchanged" />
</div>
</div>
</DemoBlock>
<DemoBlock Title="Timezone" Introduction="Display data of Timezone" Name="Timezone">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select TValue="string" Value="@TimezoneId" Items="TimezoneItems" OnValueChanged="OnTimezoneValueChanged" />
</div>
<div class="col-12 col-sm-6">
<Display TValue="TimeSpan" Value="@TimezoneValue" />
</div>
</div>
</DemoBlock>
<AttributeTable Items="@GetAttributes()" />
<EventTable Items="@GetEvents()" />
// 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;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
namespace BootstrapBlazor.Shared.Samples;
/// <summary>
/// 下拉框操作类
/// </summary>
public sealed partial class Selects
{
/// <summary>
///
/// </summary>
private Foo Model { get; set; } = new Foo();
/// <summary>
///
/// </summary>
private Foo BindModel { get; set; } = new Foo() { Name = "" };
/// <summary>
/// 获得/设置 Logger 实例
/// </summary>
private BlockLogger? Trace { get; set; }
[Inject]
[NotNull]
private IStringLocalizer<Selects>? Localizer { get; set; }
/// <summary>
/// 获得 默认数据集合
/// </summary>
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
/// <summary>
/// 获得 默认数据集合
/// </summary>
private readonly IEnumerable<SelectedItem> Items4 = new[]
{
new SelectedItem ("Beijing", "北京") { IsDisabled = true},
new SelectedItem ("Shanghai", "上海") { Active = true },
new SelectedItem ("Guangzhou", "广州")
};
/// <summary>
/// 获得 默认数据集合
/// </summary>
private readonly IEnumerable<SelectedItem> StringItems = new[]
{
new SelectedItem ("1", "1"),
new SelectedItem ("12", "12"),
new SelectedItem ("123", "123"),
new SelectedItem ("1234", "1234"),
new SelectedItem ("a", "a"),
new SelectedItem ("ab", "ab"),
new SelectedItem ("abc", "abc"),
new SelectedItem ("abcd", "abcd"),
new SelectedItem ("abcde", "abcde")
};
/// <summary>
/// 获得 默认数据集合
/// </summary>
private readonly IEnumerable<SelectedItem> Items3 = new SelectedItem[]
{
new SelectedItem ("", "请选择 ..."),
new SelectedItem ("Beijing", "北京") { Active = true },
new SelectedItem ("Shanghai", "上海"),
new SelectedItem ("Hangzhou", "杭州")
};
private readonly IEnumerable<SelectedItem> GroupItems = new SelectedItem[]
{
new SelectedItem ("Jilin", "吉林") { GroupName = "东北"},
new SelectedItem ("Liaoning", "辽宁") {GroupName = "东北", Active = true },
new SelectedItem ("Beijing", "北京") { GroupName = "华中"},
new SelectedItem ("Shijiazhuang", "石家庄") { GroupName = "华中"},
new SelectedItem ("Shanghai", "上海") {GroupName = "华东", Active = true },
new SelectedItem ("Ningbo", "宁波") {GroupName = "华东", Active = true }
};
private Guid CurrentGuid { get; set; }
private readonly IEnumerable<SelectedItem> GuidItems = new SelectedItem[]
{
new SelectedItem(Guid.NewGuid().ToString(), "Guid1"),
new SelectedItem(Guid.NewGuid().ToString(), "Guid2")
};
/// <summary>
/// 下拉选项改变时调用此方法
/// </summary>
/// <param name="item"></param>
private Task OnItemChanged(SelectedItem item)
{
Trace?.Log($"SelectedItem Text: {item.Text} Value: {item.Value} Selected");
StateHasChanged();
return Task.CompletedTask;
}
/// <summary>
/// 级联绑定菜单
/// </summary>
/// <param name="item"></param>
private async Task OnCascadeBindSelectClick(SelectedItem item)
{
// 模拟异步通讯切换线程
await Task.Delay(10);
if (item.Value == "Beijing")
{
Items2 = new SelectedItem[]
{
new SelectedItem("1","朝阳区") { Active = true},
new SelectedItem("2","海淀区"),
};
}
else if (item.Value == "Shanghai")
{
Items2 = new SelectedItem[]
{
new SelectedItem("1","静安区"),
new SelectedItem("2","黄浦区") { Active = true } ,
};
}
else
{
Items2 = Enumerable.Empty<SelectedItem>();
}
StateHasChanged();
}
private Task OnShowDialog() => Dialog.Show(new DialogOption()
{
Title = "弹窗中使用级联下拉框",
Component = BootstrapDynamicComponent.CreateComponent<CustomerSelectDialog>()
});
private IEnumerable<SelectedItem>? Items2 { get; set; }
private IEnumerable<SelectedItem> NullableIntItems { get; set; } = new SelectedItem[]
{
new SelectedItem() { Text = "Item 1", Value = "" },
new SelectedItem() { Text = "Item 2", Value = "2" },
new SelectedItem() { Text = "Item 3", Value = "3" }
};
private int? SelectedIntItem { get; set; }
private string GetSelectedIntItemString()
{
return SelectedIntItem.HasValue ? SelectedIntItem.Value.ToString() : "null";
}
private IEnumerable<SelectedItem> NullableBoolItems { get; set; } = new SelectedItem[]
{
new SelectedItem() { Text = "空值", Value = "" },
new SelectedItem() { Text = "True 值", Value = "true" },
new SelectedItem() { Text = "False 值", Value = "false" }
};
private bool? SelectedBoolItem { get; set; }
private string GetSelectedBoolItemString()
{
return SelectedBoolItem.HasValue ? SelectedBoolItem.Value.ToString() : "null";
}
private static Task<bool> OnBeforeSelectedItemChange(SelectedItem item)
{
return Task.FromResult(true);
}
private EnumEducation SelectedEnumItem { get; set; } = EnumEducation.Primary;
private EnumEducation? SelectedEnumItem1 { get; set; }
[NotNull]
private IEnumerable<SelectedItem>? TimezoneItems { get; set; }
private string? TimezoneId { get; set; }
[NotNull]
private TimeSpan TimezoneValue { get; set; }
private Task OnTimezoneValueChanged(string timezoneId)
{
TimezoneId = timezoneId;
TimezoneValue = TimeZoneInfo.GetSystemTimeZones().First(i => i.Id == timezoneId).BaseUtcOffset;
StateHasChanged();
return Task.CompletedTask;
}
/// <summary>
///
/// </summary>
protected override void OnInitialized()
{
TimeZoneInfo.ClearCachedData();
TimezoneItems = TimeZoneInfo.GetSystemTimeZones().Select(i => new SelectedItem(i.Id, i.DisplayName));
TimezoneId = TimeZoneInfo.Local.Id;
TimezoneValue = TimeZoneInfo.Local.BaseUtcOffset;
}
/// <summary>
/// 获得事件方法
/// </summary>
/// <returns></returns>
private IEnumerable<EventItem> GetEvents() => new EventItem[]
{
new EventItem()
{
Name = "OnSelectedItemChanged",
Description = Localizer["OnSelectedItemChanged"],
Type = "Func<SelectedItem, Task>"
},
new EventItem()
{
Name = "OnBeforeSelectedItemChange",
Description = Localizer["OnBeforeSelectedItemChange"],
Type = "Func<SelectedItem, Task<bool>>"
}
};
/// <summary>
/// 获得属性方法
/// </summary>
/// <returns></returns>
private IEnumerable<AttributeItem> GetAttributes() => new AttributeItem[]
{
// TODO: 移动到数据库中
new AttributeItem() {
Name = "ShowLabel",
Description = Localizer["ShowLabel"],
Type = "bool",
ValueList = "true|false",
DefaultValue = "true"
},
new AttributeItem() {
Name = "ShowSearch",
Description = Localizer["ShowSearch"],
Type = "bool",
ValueList = "true|false",
DefaultValue = "false"
},
new AttributeItem() {
Name = "DisplayText",
Description = Localizer["DisplayText"],
Type = "string",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "Class",
Description = Localizer["Class"],
Type = "string",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "Color",
Description = Localizer["Color"],
Type = "Color",
ValueList = "Primary / Secondary / Success / Danger / Warning / Info / Dark",
DefaultValue = "Primary"
},
new AttributeItem() {
Name = "IsDisabled",
Description = Localizer["IsDisabled"],
Type = "boolean",
ValueList = "true / false",
DefaultValue = "false"
},
new AttributeItem() {
Name = "Items",
Description = Localizer["Items"],
Type = "IEnumerable<SelectedItem>",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "SelectItems",
Description = Localizer["SelectItems"],
Type = "RenderFragment",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "ItemTemplate",
Description = Localizer["ItemTemplate"],
Type = "RenderFragment<SelectedItem>",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "ChildContent",
Description = Localizer["ChildContent"],
Type = "RenderFragment",
ValueList = " — ",
DefaultValue = " — "
},
new AttributeItem() {
Name = "Category",
Description = Localizer["Category"],
Type = "SwalCategory",
ValueList = " — ",
DefaultValue = " SwalCategory.Information "
},
new AttributeItem() {
Name = "Content",
Description = Localizer["Content"],
Type = "string?",
ValueList = " — ",
DefaultValue = Localizer["ContentDefaultValue"]!
}
};
}