Transfer 穿梭框
在两个列表之间移动条目,常用于授权和成员分配。
何时使用
- 从较大的候选集中选择多个对象,并明确展示已选结果。
- 选择项较少时使用 Checkbox 多选框。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
var selectedUsers = []string{"u2"}
func page(c *ui.Context) {
items := []ui.TransferItem{{Key: "u1", Title: "张三"}, {Key: "u2", Title: "李四"}}
ui.Transfer(c, items, &selectedUsers)
}右侧列表绑定 target 中的 key,左侧为其余条目。
常见用法
搜索和标题
func search(c *ui.Context, items []ui.TransferItem, target *[]string) {
ui.Transfer(c, items, target).Titles("候选人", "已选").Searchable().ListSize(240, 280)
}默认勾选和操作文案
func defaults(c *ui.Context, items []ui.TransferItem, target *[]string) {
ui.Transfer(c, items, target).DefaultChecked("u1").Operations("加入", "移除")
}状态和禁用
func disabled(c *ui.Context, items []ui.TransferItem, target *[]string) {
ui.Transfer(c, items, target).Status("error").Disabled(false)
}长列表
两侧的普通列表按搜索、分页后的条目数各自判断。当前页超过 100 项时自动启用虚拟滚动,只构建视口附近的行。Virtual(true) 强制启用,Virtual(false) 关闭。ListSize(width, height) 调整列表大小。
主题与样式
Transfer 令牌控制列表尺寸、头部、选中项、操作按钮和错误状态。条目的 Disabled 可禁止移动。
注意事项
目标 key 列表是业务状态,应持久保存;Changed 表示本帧发生移动。大量条目建议开启搜索并控制列表高度,保持键盘焦点可在列表间移动。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Transfer 自定义渲染 / 分页 / 单向 / 树穿梭 / 虚拟列表
gallery 演示 TransferAdvanced 的 Render 函数,源码 page_p2b_transfer.go#L46。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Transfer
func Transfer(c *Context, items []TransferItem, target *[]string) *TransferWTransfer adds a transfer bound to the keys of the right-hand list.
返回 *TransferW,可链式调用的方法见 TransferW 方法。
TransferW 方法
ui.TransferW:TransferW is a two-list shuttle: items move between the source (left) and the target (right) list.
| 方法 | 说明 | 签名 |
|---|---|---|
Actions | Actions labels the move buttons (Ant 6 actions, Element button-texts; default: icons only). | Actions(toRight, toLeft string) *TransferW |
Changed | Changed reports items moved this frame. | Changed() bool |
Checked | Checked returns the checked keys of a side (0 left, 1 right). | Checked(side int) []string |
DefaultChecked | DefaultChecked checks keys when the widget is first shown. | DefaultChecked(keys ...string) *TransferW |
Disabled | Disabled disables the whole transfer. | Disabled(on bool) *TransferW |
ListSize | ListSize sets the size of each list box. | ListSize(wd, ht float32) *TransferW |
OneWay | OneWay makes the transfer one-directional (Ant oneWay): only the move to the right remains, and right-hand items have a remove button. | OneWay() *TransferW |
Operations | Operations labels the move buttons. | Operations(toRight, toLeft string) *TransferW |
Pagination | Pagination shows pageSize items per page with a pager under each list (Ant pagination). | Pagination(pageSize int) *TransferW |
Render | Render builds each row's content with fn instead of the title (the row background and checkbox stay). | Render(fn func(o Option)) *TransferW |
Searchable | Searchable adds a filter input to each list. | Searchable() *TransferW |
ShowSelectAll | ShowSelectAll shows or hides the select-all checkbox in the list headers (Ant showSelectAll, default true). | ShowSelectAll(on bool) *TransferW |
Status | Status sets the validation status ("error", "warning"). | Status(s string) *TransferW |
TargetOrder | TargetOrder sets where moved keys go in the bound target slice and the order of the right list (Element target-order): "unshift" (default, as Ant: new keys first, the list follows the slice), "push" (new keys last, the list follows the slice) or "original" (the slice and the list follow the item order). | TargetOrder(s string) *TransferW |
Titles | Titles sets the list titles. | Titles(left, right string) *TransferW |
TreeSource | TreeSource shows nodes as a checkable tree on the left (Ant tree transfer). | TreeSource(nodes []TreeNode) *TransferW |
Virtual | Virtual forces virtual lists on or off (default: more than 100 items on a page). | Virtual(on bool) *TransferW |
TransferItem 参数类型
ui.TransferItem:TransferItem is an entry of a Transfer.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Title | string | |
Disabled | bool |