Select 选择器
从一组选项中选择一个值。选项使用 []ui.Option,ui.Opts 可以快速从字符串创建选项。
在线体验
展开菜单,选择一项;也可以输入搜索词或点击清空。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
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 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。
实现与更多示例
全部示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Select 分组 / 远程搜索 / 创建条目 / 自定义选项 / 虚拟列表
gallery 演示 SelectAdvanced 的 Render 函数,源码 page_p2b_select.go#L58。
加载中…
完整 API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Select
func Select(c *Context, val *string, opts []Option) *SelectWSelect adds a single-value select bound to *val.
返回 *SelectW,可链式调用的方法见 SelectW 方法。
SelectW 方法
ui.SelectW:SelectW is a dropdown select.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort). | A11yLabel(s string) *SelectW |
AllowClear | AllowClear() *SelectW | |
AllowCreate | AllowCreate offers the typed text as a new option when no label equals it (Element allow-create); read it with Created. | AllowCreate() *SelectW |
Changed | Changed reports a new selection this frame. | Changed() bool |
Created | Created returns the value of an entry created with AllowCreate this frame. | Created() (string, bool) |
DataVersion | DataVersion 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 |
Debounce | Debounce sets the delay before a Remote call. | Debounce(d time.Duration) *SelectW |
Disabled | Disabled(on bool) *SelectW | |
FilterMethod | FilterMethod 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 |
Groups | Groups shows the options under titled groups (Ant OptGroup, Element el-option-group); the opts passed to Select are ignored. | Groups(groups []OptionGroup) *SelectW |
IsOpen | IsOpen reports whether the dropdown is shown. | IsOpen() bool |
LabelRender | LabelRender sets the text shown for the selected value (Ant labelRender). | LabelRender(fn func(o Option) string) *SelectW |
Layout | Layout(o ...layout.Option) *SelectW | |
ListHeight | ListHeight sets the maximum dropdown height with or without virtual scrolling (Ant listHeight, 256). | ListHeight(px float32) *SelectW |
Loading | Loading shows a spinner and the loading text in the dropdown. | Loading(on bool) *SelectW |
NotFoundContent | NotFoundContent 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 |
Open | Open forces the dropdown open (screenshots, tests). | Open() *SelectW |
OpenChanged | OpenChanged reports that the dropdown opened or closed this frame (Ant onOpenChange, Element visible-change); read IsOpen for the new state. | OpenChanged() bool |
OptionRender | OptionRender builds each option row's content with fn (row background, highlight and selection marks stay). | OptionRender(fn func(o Option, s OptionState)) *SelectW |
Placeholder | Placeholder(s string) *SelectW | |
Placement | Placement sets where the dropdown opens: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement bottomLeft / bottomRight / topLeft / topRight; Element placement). | Placement(p Placement) *SelectW |
Prefix | Prefix shows ic before the value (Ant prefix). | Prefix(ic *icon.Icon) *SelectW |
PrefixText | PrefixText shows s before the value (Ant prefix, Element prefix slot), e.g. a field name inside the frame. | PrefixText(s string) *SelectW |
Remote | Remote 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 |
Searchable | Searchable() *SelectW | |
Size | Size(s ControlSize) *SelectW | |
Status | Status(s string) *SelectW | |
SuffixIcon | SuffixIcon 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 |
Variant | Variant sets the frame look (FieldOutlined by default). | Variant(v FieldVariant) *SelectW |
Virtual | Virtual 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 |
Width | Width(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.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Disabled | bool | |
Group | string |