LightFilter 轻量筛选
把查询条件压缩成“标签:值”的筛选胶囊:点击胶囊在浮层中编辑,确认后才写回值,并可直接清除单个条件。
何时使用
- 列表工具栏空间有限,但需要让用户快速查看和修改已生效的筛选条件时。
- 条件数量较少,适合把每个字段显示成一个独立筛选胶囊时。
- 需要把多余字段收进“更多筛选”浮层,同时保留已选条件概览时。
- 需要始终展开的多字段查询网格时,用 QueryFilter 查询表单;只有一个普通输入框时用 Search 搜索框。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见打开自绘窗口。
package main
import "github.com/tangoui/tango/ui"
var (
lightValues = ui.ProValues{"status": "running"}
lightFields = []ui.ProField{
{Key: "status", Label: "状态", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "running", Label: "运行中"}, {Value: "done", Label: "已完成"}}},
{Key: "owner", Label: "负责人"},
{Key: "created", Label: "创建日期", ValueType: ui.ValueDate},
}
)
func page(c *ui.Context) {
lf := ui.LightFilter(c, lightFields).Values(&lightValues).Show()
if lf.Changed() {
reloadList(lf.Params())
}
}
func reloadList(params map[string]string) {}
func main() {}LightFilter 使用和 ProForm、QueryFilter 相同的 ProField 定义。点击胶囊后,在浮层中编辑并确认,Changed() 在确认或清除的当前帧返回 true;Params() 返回非空值的文本参数。
常见用法
绑定筛选值并显示请求参数
把 ProValues 放在页面状态中,页面外的列表请求可以直接读取它。没有绑定 Values 时,组件会在自己的状态中保存筛选值。
package main
import "github.com/tangoui/tango/ui"
var (
values = ui.ProValues{"owner": "alice", "amount": 1000.0}
fields = []ui.ProField{
{Key: "owner", Label: "负责人"},
{Key: "amount", Label: "金额", ValueType: ui.ValueMoney},
}
)
func page(c *ui.Context) {
lf := ui.LightFilter(c, fields).Values(&values).Bordered().Show()
if lf.Changed() {
fetch(lf.Params())
}
}
func fetch(params map[string]string) {}
func main() {}折叠到“更多筛选”
Collapse(n) 保留前 n 个字段为胶囊,其余字段进入一个统一浮层。浮层中的值确认后才生效,“更多筛选”按钮会显示当前已设置的数量。
package main
import "github.com/tangoui/tango/ui"
var moreValues = ui.ProValues{
"status": "running",
"amount": 300.0,
}
var moreFields = []ui.ProField{
{Key: "status", Label: "状态", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "running", Label: "运行中"}, {Value: "paused", Label: "已暂停"}}},
{Key: "owner", Label: "负责人"},
{Key: "amount", Label: "金额", ValueType: ui.ValueMoney},
{Key: "created", Label: "创建日期", ValueType: ui.ValueDate},
}
func page(c *ui.Context) {
ui.LightFilter(c, moreFields).Values(&moreValues).Collapse(2).Show()
}
func main() {}控制尺寸和边框
Size 改变胶囊内控件的高度,Bordered 为每个胶囊绘制边框。尺寸可以继承外层 ConfigProvider.Size,只有需要覆盖局部工具栏时才在实例上设置。
package main
import "github.com/tangoui/tango/ui"
var filterValues ui.ProValues
var filterFields = []ui.ProField{
{Key: "type", Label: "类型", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "bug", Label: "缺陷"}, {Value: "task", Label: "任务"}}},
{Key: "keyword", Label: "关键词"},
}
func page(c *ui.Context) {
ui.LightFilter(c, filterFields).Values(&filterValues).
Size(ui.Small).Bordered().Show()
}
func main() {}根据状态隐藏条件
和 QueryFilter 一样,Hide、Disable 和 Options 都接收当前的 ProValues。被隐藏的字段不会生成胶囊,也不会进入“更多筛选”浮层。
package main
import "github.com/tangoui/tango/ui"
var values ui.ProValues
var fields = []ui.ProField{
{Key: "scope", Label: "范围", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "all", Label: "全部"}, {Value: "mine", Label: "我的"}}},
{Key: "team", Label: "团队", Hide: func(v ui.ProValues) bool { return v.String("scope") != "mine" }},
{Key: "archived", Label: "包含归档", ValueType: ui.ValueSwitch},
}
func page(c *ui.Context) {
ui.LightFilter(c, fields).Values(&values).Show()
}
func main() {}主题与样式
LightFilter 继承 ProField 控件、颜色和尺寸令牌;Bordered() 只改变胶囊边框,不改变浮层内控件。可通过 ConfigProvider.Defaults("LightFilter", ui.Props{...}) 设置 size、bordered 和 collapse 默认值。shadcn 主题会使用更接近数据表格 faceted filter 的虚线按钮与值徽标,Ant Design 和 Element Plus 保留各自的胶囊边框风格。
注意事项
Values必须绑定到稳定的页面状态;不要每帧新建ProValues,否则浮层草稿和确认后的筛选会被覆盖。- LightFilter 的值在确认前保存在浮层草稿中,取消或关闭浮层不会触发
Changed();清除单个胶囊会触发一次变更。 Collapse(n)的n <= 0表示所有字段都显示为胶囊;字段较多时应设置正数,避免工具栏换行过长。- 组件根节点是工具栏语义;键盘聚焦胶囊后可打开浮层并在控件、清除和确认之间操作。自定义
Render控件要提供可访问名称。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.LightFilter
func LightFilter(c *Context, fields []ProField) *LightFilterWLightFilter starts a filter bar over fields; chain options, then Show.
ConfigProvider.Defaults("LightFilter", ...) keys: "size", "bordered", "collapse" (number of pills before "more filters").
返回 *LightFilterW,可链式调用的方法见 LightFilterW 方法。
LightFilterW 方法
ui.LightFilterW:LightFilterW is a compact filter bar (Ant Design Pro LightFilter): each field is a "label: value" pill; clicking it opens a popup with the field's editor and clear / confirm buttons. It takes the same field definitions as QueryFilter and ProForm.
lf := ui.LightFilter(c, fields).Values(&filter).Show()
if lf.Changed() { reload(lf.Params()) }
| 方法 | 说明 | 签名 |
|---|---|---|
Bordered | Bordered draws an outline around every pill. | Bordered() *LightFilterW |
Changed | Changed reports a value confirmed or cleared in the last Show. | Changed() bool |
Collapse | Collapse shows at most n fields as pills; the others are edited together in a "more filters" popup (n <= 0: every field is a pill). | Collapse(n int) *LightFilterW |
ID | ID returns the widget ID (valid after Show). | ID() ID |
OpenKey | OpenKey returns the key of the field whose popup is open ("" if none). | OpenKey() string |
Params | Params returns the non-empty values as text (dates as 2006-01-02). | Params() map[string]string |
Show | Show builds the bar. | Show() *LightFilterW |
Size | Size sets the pill size (default: the global size). | Size(s ControlSize) *LightFilterW |
Values | Values binds the values (otherwise the filter keeps its own). | Values(v *ProValues) *LightFilterW |
ProField 参数类型
ui.ProField:ProField describes a form field; ProForm, QueryFilter, StepsForm, ModalForm and DrawerForm render fields from it.
| 字段 | 说明 | 类型 |
|---|---|---|
Key | string | |
Label | string | |
ValueType | see the Value* constants (default text) | string |
ValueEnum | options of select / radio / checkbox / status / tag | []ProEnum |
Placeholder | string | |
Help | hint under the control | string |
Span | grid columns out of 24 (default: 24 / form columns) | int |
Group | Group puts the field in a titled ProFormGroup with the consecutive fields of the same Group (ProForm, ModalForm, StepsForm). | string |
Width | fixed control width (default: fill the column) | float32 |
Initial | value when the form starts or resets | any |
Required | Validation: Required adds a required rule with the locale message; Rules check the value's text form; Validate checks the typed value against all values (cross-field); AsyncValidate runs after them. | bool |
Rules | []Rule | |
Validate | func(v any, all ProValues) string | |
AsyncValidate | func(v any, done func(msg string)) | |
Dependencies | Linkage: Dependencies lists keys whose change re-validates this field, refreshes Options and drops a value Options no longer offers. | []string |
Hide | func(all ProValues) bool | |
Disable | func(all ProValues) bool | |
Options | func(all ProValues) []ProEnum | |
Min | Number fields. | float64 |
Max | Number fields. | float64 |
Precision | int | |
Pick | Pick handles the upload field's trigger (open a file dialog); call done from any goroutine with the files to add. | func(done func(files []UploadFile)) |
Dragger | Dragger shows the upload field as a drag area (Upload.Dragger): it highlights while files are dragged over it and takes dropped files (DropZone inside Upload). | bool |
Render | Render replaces the built-in control (bind it to vals[Key]). | func(c *Context, vals ProValues) |
Readonly | Readonly shows the value as read-only text (ProFormW.Readonly does it for every field); a read-only field is not validated. | bool |
OnCaptcha | OnCaptcha sends the code of a captcha field (call done from any goroutine; nil starts the countdown); the button then counts down CaptchaSeconds (default 60). | func(vals ProValues, done func(error)) |
CaptchaSeconds | int | |
Captcha | Captcha configures the phone key, button text and timing callback. | *ProFieldCaptcha |
Segmented | Segmented configures the ValueSegmented editor (custom item content, direction, size, icons, etc.) before it is built. | func(w *SegmentedW) |
List | List gives the row fields of a formList field. | *ProFieldList |