Skip to content

Select 选择器 ​

从一组选项中选择一个值。选项使用 []ui.Option,ui.Opts 可以快速从字符串创建选项。

在线体验 ​

展开菜单,选择一项;也可以输入搜索词或点击清空。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。

Select 选择器 · 在线体验真实 Go 组件
在独立页面打开 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import (
    "github.com/tangoui/tango/ui"
)

var language string

func page(c *ui.Context) {
    ui.Select(c, &language, ui.Opts("Go", "TypeScript", "Python")).
        Placeholder("Choose a language").
        Searchable().
        AllowClear()
}

常用参数与方法 ​

参数 / 方法说明
val *string当前选中项的 Value
opts []ui.Option可选项列表
Placeholder(string)未选择时的占位文本
Searchable()启用搜索
AllowClear()允许清空当前选择
Disabled(bool)禁用选择器
Changed() bool当前帧是否发生选择变化
Virtual(bool)控制普通选项列表是否启用虚拟滚动。省略时超过 100 项自动启用
ListHeight(float32)设置虚拟下拉面板的最大高度
DataVersion(int)在原地修改虚拟列表数据后使选项缓存失效

使用说明 ​

自定义选项时使用 ui.Option 的 Value 和 Label 区分存储值与显示文字。键盘 Enter 展开,方向键移动,Enter 选择,Escape 关闭。

虚拟下拉只构建视口附近的选项行,按切片身份、长度和版本缓存选项。原地修改选项的 Label、Value 或 Disabled 等字段后,递增 DataVersion,或传入新切片。普通选项列表可用 Virtual(true) 强制启用、用 Virtual(false) 关闭。分组、远程搜索和自定义选项行等功能会使用虚拟下拉。虚拟下拉的最大高度默认是 256,Element Plus 主题下为 274,单位为逻辑像素。

更多场景 ​

官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。

Select 选择器 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

实现与更多示例 ​

全部示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Select 选择器

gallery 演示 Select 的 Render 函数,源码 page_form.go#L77。

加载中…

Select 规范对齐(选择器)

gallery 演示 SelectAlign 的 Render 函数,源码 page_b1c.go#L34。

加载中…

Select 分组 / 远程搜索 / 创建条目 / 自定义选项 / 虚拟列表

gallery 演示 SelectAdvanced 的 Render 函数,源码 page_p2b_select.go#L58。

加载中…

完整 API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Select ​

go
func Select(c *Context, val *string, opts []Option) *SelectW

Select adds a single-value select bound to *val.

返回 *SelectW,可链式调用的方法见 SelectW 方法。

SelectW 方法 ​

ui.SelectW:SelectW is a dropdown select.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort).A11yLabel(s string) *SelectW
AllowClearAllowClear() *SelectW
AllowCreateAllowCreate offers the typed text as a new option when no label equals it (Element allow-create); read it with Created.AllowCreate() *SelectW
ChangedChanged reports a new selection this frame.Changed() bool
CreatedCreated returns the value of an entry created with AllowCreate this frame.Created() (string, bool)
DataVersionDataVersion tells the select that the option slice was modified in place (the virtual dropdown caches by slice identity and this number).DataVersion(v int) *SelectW
DebounceDebounce sets the delay before a Remote call.Debounce(d time.Duration) *SelectW
DisabledDisabled(on bool) *SelectW
FilterMethodFilterMethod replaces the case-insensitive label match of the search text with fn (same as VirtualSelect.FilterMethod); it is not used with Remote, whose results are already filtered.FilterMethod(fn func(query string, o Option) bool) *SelectW
GroupsGroups shows the options under titled groups (Ant OptGroup, Element el-option-group); the opts passed to Select are ignored.Groups(groups []OptionGroup) *SelectW
IsOpenIsOpen reports whether the dropdown is shown.IsOpen() bool
LabelRenderLabelRender sets the text shown for the selected value (Ant labelRender).LabelRender(fn func(o Option) string) *SelectW
LayoutLayout(o ...layout.Option) *SelectW
ListHeightListHeight sets the maximum dropdown height with or without virtual scrolling (Ant listHeight, 256).ListHeight(px float32) *SelectW
LoadingLoading shows a spinner and the loading text in the dropdown.Loading(on bool) *SelectW
NotFoundContentNotFoundContent sets the text of an empty dropdown (Ant notFoundContent, Element no-data-text / no-match-text); by default the Locale's NoData.NotFoundContent(s string) *SelectW
OpenOpen forces the dropdown open (screenshots, tests).Open() *SelectW
OpenChangedOpenChanged reports that the dropdown opened or closed this frame (Ant onOpenChange, Element visible-change); read IsOpen for the new state.OpenChanged() bool
OptionRenderOptionRender builds each option row's content with fn (row background, highlight and selection marks stay).OptionRender(fn func(o Option, s OptionState)) *SelectW
PlaceholderPlaceholder(s string) *SelectW
PlacementPlacement sets where the dropdown opens: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement bottomLeft / bottomRight / topLeft / topRight; Element placement).Placement(p Placement) *SelectW
PrefixPrefix shows ic before the value (Ant prefix).Prefix(ic *icon.Icon) *SelectW
PrefixTextPrefixText shows s before the value (Ant prefix, Element prefix slot), e.g. a field name inside the frame.PrefixText(s string) *SelectW
RemoteRemote turns on server-side search: fn receives the typed text after Debounce (300ms by default) and should replace the options.Remote(fn func(query string)) *SelectW
SearchableSearchable() *SelectW
SizeSize(s ControlSize) *SelectW
StatusStatus(s string) *SelectW
SuffixIconSuffixIcon replaces the dropdown arrow with ic (Ant suffixIcon, Element suffix-icon); it is not rotated and also stays while searching.SuffixIcon(ic *icon.Icon) *SelectW
VariantVariant sets the frame look (FieldOutlined by default).Variant(v FieldVariant) *SelectW
VirtualVirtual forces the virtual dropdown on or off; by default lists longer than 100 options are virtual (only the visible rows are built).Virtual(on bool) *SelectW
WidthWidth(px float32) *SelectW

Option 参数类型 ​

ui.Option:Option is an item of a Select / RadioGroup / CheckboxGroup. Group puts the option under a titled group in Select / MultiSelect / VirtualSelect dropdowns (Ant OptGroup, Element el-option-group); options of one group are listed together in order of the group's first appearance.

字段说明类型
Labelstring
Valuestring
Disabledbool
Groupstring

以 MIT 许可发布