
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 source Items
and bind Value
values in both directions, changing together when you select different options
<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" />
@code {
private BlockLogger? Trace { get; set; }
private Foo Model { get; set; } = new Foo();
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
private Task OnItemChanged(SelectedItem item)
{
Trace?.Log($"SelectedItem Text: {item.Text} Value: {item.Value} Selected");
StateHasChanged();
return Task.CompletedTask;
}
}
Selector unavailable state
Demo
The options in the drop-down box disable the example
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
<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">@((MarkupString)Localizer["SelectsDisableOption"].Value)</p>
<div class="row">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items4" />
</div>
</div>
@code {
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
private readonly IEnumerable<SelectedItem> Items4 = new[]
{
new SelectedItem ("Beijing", "北京") { IsDisabled = true},
new SelectedItem ("Shanghai", "上海") { Active = true },
new SelectedItem ("Guangzhou", "广州")
};
}
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
<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>
@code {
private Foo Model { get; set; } = new Foo();
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
}
The second drop-down box dynamically populates the content by selecting the different options for the first drop-down box.
Demo
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
@inject DialogService Dialog
<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="@Localizer["SelectsCascadingButtonText1"]" OnClickWithoutRender="OnShowDialog" />
</div>
</div>
@code {
private readonly IEnumerable<SelectedItem> Items3 = new SelectedItem[]
{
new SelectedItem ("", "请选择 ..."),
new SelectedItem ("Beijing", "北京") { Active = true },
new SelectedItem ("Shanghai", "上海"),
new SelectedItem ("Hangzhou", "杭州")
};
private IEnumerable<SelectedItem>? Items2 { get; set; }
private Task OnShowDialog() => Dialog.Show(new DialogOption()
{
Title = "弹窗中使用级联下拉框",
Component = BootstrapDynamicComponent.CreateComponent<CustomerSelectDialog>()
});
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();
}
}
When the drop-down box is not selected, it is blocked when the submit button is clicked.
Demo
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
<ValidateForm Model="BindModel">
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select @bind-Value="BindModel.Name">
<Options>
<SelectOption Text="@Localizer["SelectsOption1"]" Value="" />
<SelectOption Text="@Localizer["SelectsOption2"]" Value="1" />
<SelectOption Text="@Localizer["SelectsOption3"]" Value="2" />
<SelectOption Text="@Localizer["SelectsOption4"]" Value="3" />
</Options>
</Select>
</div>
<div class="col-12 col-sm-6 align-self-end">
<Button ButtonType="ButtonType.Submit">@Localizer["SelectsClientValidationButtonText2"]</Button>
</div>
</div>
</ValidateForm>
@code {
private Foo BindModel { get; set; } = new Foo() { Name = "" };
}
Alternatives are presented in groups
Demo
<div class="row">
<div class="col-12 col-sm-6">
<Select TValue="string" Color="Color.Primary" Items="GroupItems">
</Select>
</div>
</div>
@code {
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 }
};
}
The component binding value is an example of a Guid structure
Demo
<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>
@code {
private Guid CurrentGuid { get; set; }
private readonly IEnumerable<SelectedItem> GuidItems = new SelectedItem[]
{
new SelectedItem(Guid.NewGuid().ToString(), "Guid1"),
new SelectedItem(Guid.NewGuid().ToString(), "Guid2")
};
}
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]
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
<Divider Text="@Localizer["SelectsDispalyLabelDivider1"]" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<ValidateForm Model="Model">
<div class="row">
<div class="col-12">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name" />
</div>
</div>
</ValidateForm>
<Divider Text="@Localizer["SelectsDispalyLabelDivider2"]" Alignment="Alignment.Left" style="margin: 2rem 0;" />
<div class="row">
<div class="col-12">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name" />
</div>
</div>
<Divider Text="@Localizer["SelectsDispalyLabelDivider3"]" Alignment="Alignment.Left" style="margin: 2rem 0;"></Divider>
<div class="row">
<div class="col-12">
<Select Color="Color.Primary" Items="Items" @bind-Value="Model.Name" DisplayText="@Localizer["SelectsDispalyLabelSelectText"]" ShowLabel="true" />
</div>
</div>
@code {
private Foo Model { get; set; } = new Foo();
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
}
Hard-coded writing directly inside the Select
component is suitable for static data drop-down boxes
Demo
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
<div class="row">
<div class="col-12 col-sm-6">
<Select TValue="string">
<Options>
<SelectOption Text="@Localizer["SelectsOption1"]" Value="1" />
<SelectOption Text="@Localizer["SelectsOption2"]" Value="2" Active="true" />
<SelectOption Text="@Localizer["SelectsOption3"]" Value="3" />
</Options>
</Select>
</div>
</div>
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
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
<div class="row g-3">
<div class="col-12 col-sm-6">
<Select @bind-Value="SelectedEnumItem1" PlaceHolder="@Localizer["SelectsPlaceHolder"]" ShowLabel="true" DisplayText="@Localizer["SelectsEnumSelectText1"]">
</Select>
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="SelectedEnumItem1" ShowLabel="true" DisplayText="@Localizer["SelectsEnumSelectText1"]">
</Select>
</div>
<div class="col-12 col-sm-6">
<Select @bind-Value="SelectedEnumItem" ShowLabel="true" DisplayText="@Localizer["SelectsEnumSelectText2"]">
</Select>
</div>
<div class="col-12 col-sm-6 align-self-end">
<div class="form-control">@SelectedEnumItem</div>
</div>
</div>
@code {
private EnumEducation SelectedEnumItem { get; set; } = EnumEducation.Primary;
private EnumEducation? SelectedEnumItem1 { get; set; }
}
an example of the Select
component binding int?
type
Demo
When the first option is selected, the binding value SelectIntItem
to null
<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>
@code {
private int? SelectedIntItem { get; set; }
private string GetSelectedIntItemString()
{
return SelectedIntItem.HasValue ? SelectedIntItem.Value.ToString() : "null";
}
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" }
};
}
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
<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>
@code {
private bool? SelectedBoolItem { get; set; }
private string GetSelectedBoolItemString()
{
return SelectedBoolItem.HasValue ? SelectedBoolItem.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" }
};
}
By setting the ItemTemplate
you can customize the option rendering style
Demo
<div class="row">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items">
<ItemTemplate>
<div class="dropdown-item-demo">
<i class="fa-solid fa-font-awesome"></i>
<span>@context.Text</span>
<i class="fa-solid fa-star"></i>
</div>
</ItemTemplate>
</Select>
</div>
</div>
@code {
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
}
Controls whether the search box is displayed by setting the ShowSearch
property, which is not displayed by default false
Demo
<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" />
</div>
</div>
@code {
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
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")
};
}
Block changes to the current value by setting the OnBeforeSelectedItemChange
delegate.
Demo
@using BootstrapBlazor.Shared.Samples;
@inject IStringLocalizer<Selects> Localizer
<div class="row">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items" OnBeforeSelectedItemChange="@OnBeforeSelectedItemChange"
SwalTitle="@Localizer["SwalTitle"]" SwalContent="@Localizer["SwalContent"]" SwalFooter="@Localizer["SwalFooter"]" />
</div>
</div>
@code {
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
private static Task<bool> OnBeforeSelectedItemChange(SelectedItem item)
{
return Task.FromResult(true);
}
}
Custom UI
of display template
Demo
<div class="row">
<div class="col-12 col-sm-6">
<Select TValue="string" Items="Items">
<DisplayTemplate>
<i class="fa-solid fa-flag"></i>
<span>@context?.Text</span>
</DisplayTemplate>
</Select>
</div>
</div>
@code {
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
}
Display data of Timezone
Demo
<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>
@code {
[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;
}
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;
}
}
Set IsPopover
to true, use popover
render UI prevent The dropdown menu cannot be fully displayed because the parent container is set to overflow: hidden
Demo
<div class="row">
<div class="col-12 col-sm-6 overflow-hidden">
<Select TValue="string" Items="Items" IsPopover="true" ShowSearch="true" />
</div>
</div>
@code {
private IEnumerable<SelectedItem> Items { get; set; } = new[]
{
new SelectedItem ("Beijing", "北京"),
new SelectedItem ("Shanghai", "上海") { Active = true },
};
}
Attributes
Event
B station related video link
交流群