Skip to content

LightFilter 轻量筛选 ​

把查询条件压缩成“标签:值”的筛选胶囊:点击胶囊在浮层中编辑,确认后才写回值,并可直接清除单个条件。

何时使用 ​

  • 列表工具栏空间有限,但需要让用户快速查看和修改已生效的筛选条件时。
  • 条件数量较少,适合把每个字段显示成一个独立筛选胶囊时。
  • 需要把多余字段收进“更多筛选”浮层,同时保留已选条件概览时。
  • 需要始终展开的多字段查询网格时,用 QueryFilter 查询表单;只有一个普通输入框时用 Search 搜索框。

在线体验 ​

LightFilter 轻量筛选 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

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

go
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 时,组件会在自己的状态中保存筛选值。

go
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 个字段为胶囊,其余字段进入一个统一浮层。浮层中的值确认后才生效,“更多筛选”按钮会显示当前已设置的数量。

go
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,只有需要覆盖局部工具栏时才在实例上设置。

go
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。被隐藏的字段不会生成胶囊,也不会进入“更多筛选”浮层。

go
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 入门。

LightFilter 轻量筛选

gallery 演示 LightFilter 的注册调用(含 Render 函数),源码 page_pro3_filter.go#L21。

加载中…

LightFilter 折叠更多筛选

gallery 演示 LightFilterCollapse 的注册调用(含 Render 函数),源码 page_p2e_more.go#L107。

加载中…

API ​

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

入口函数 ​

ui.LightFilter ​

go
func LightFilter(c *Context, fields []ProField) *LightFilterW

LightFilter 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()) }
方法说明签名
BorderedBordered draws an outline around every pill.Bordered() *LightFilterW
ChangedChanged reports a value confirmed or cleared in the last Show.Changed() bool
CollapseCollapse 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
IDID returns the widget ID (valid after Show).ID() ID
OpenKeyOpenKey returns the key of the field whose popup is open ("" if none).OpenKey() string
ParamsParams returns the non-empty values as text (dates as 2006-01-02).Params() map[string]string
ShowShow builds the bar.Show() *LightFilterW
SizeSize sets the pill size (default: the global size).Size(s ControlSize) *LightFilterW
ValuesValues 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.

字段说明类型
Keystring
Labelstring
ValueTypesee the Value* constants (default text)string
ValueEnumoptions of select / radio / checkbox / status / tag[]ProEnum
Placeholderstring
Helphint under the controlstring
Spangrid columns out of 24 (default: 24 / form columns)int
GroupGroup puts the field in a titled ProFormGroup with the consecutive fields of the same Group (ProForm, ModalForm, StepsForm).string
Widthfixed control width (default: fill the column)float32
Initialvalue when the form starts or resetsany
RequiredValidation: 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
Validatefunc(v any, all ProValues) string
AsyncValidatefunc(v any, done func(msg string))
DependenciesLinkage: Dependencies lists keys whose change re-validates this field, refreshes Options and drops a value Options no longer offers.[]string
Hidefunc(all ProValues) bool
Disablefunc(all ProValues) bool
Optionsfunc(all ProValues) []ProEnum
MinNumber fields.float64
MaxNumber fields.float64
Precisionint
PickPick 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))
DraggerDragger 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
RenderRender replaces the built-in control (bind it to vals[Key]).func(c *Context, vals ProValues)
ReadonlyReadonly shows the value as read-only text (ProFormW.Readonly does it for every field); a read-only field is not validated.bool
OnCaptchaOnCaptcha 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))
CaptchaSecondsint
CaptchaCaptcha configures the phone key, button text and timing callback.*ProFieldCaptcha
SegmentedSegmented configures the ValueSegmented editor (custom item content, direction, size, icons, etc.) before it is built.func(w *SegmentedW)
ListList gives the row fields of a formList field.*ProFieldList

相关链接 ​

以 MIT 许可发布