QueryFilter 查询表单
用于列表页顶部的可折叠查询表单:按字段定义生成控件,收集查询条件,并在点击查询或重置后通知页面重新加载数据。
何时使用
- 列表页有多个查询条件,需要按可用宽度自动排成 1–4 列时。
- 查询条件超过一行,需要默认收起并由用户展开更多字段时。
- 需要与
ProTable共用ProField定义、值类型和枚举时。 - 只有一两个输入项且不需要折叠时,用 Form 表单 的
Inline()更直接;需要胶囊式筛选时用 LightFilter 轻量筛选。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见打开自绘窗口。
package main
import "github.com/tangoui/tango/ui"
var (
queryFields = []ui.ProField{
{Key: "name", Label: "规则名称"},
{Key: "status", Label: "状态", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "running", Label: "运行中"}, {Value: "done", Label: "已完成"}}},
{Key: "created", Label: "创建日期", ValueType: ui.ValueDate},
}
queryValues ui.ProValues
)
func page(c *ui.Context) {
q := ui.QueryFilter(c, queryFields).Values(&queryValues).Show()
if q.Searched() {
reloadList(q.Params())
}
}
func reloadList(params map[string]string) {}
func main() {}Values(&queryValues) 把值放在页面自己的 ProValues 中;点击查询或重置后,Searched() 在当前帧返回 true,Params() 返回非空值的文本表示(日期格式为 2006-01-02)。不绑定 Values 时,QueryFilter 会自己保存值。
常见用法
查询多个值并发起请求
查询条件通常在 Searched() 分支中转换成请求参数。日期、数字和选择值已经由 QueryFilter 按字段类型管理,不需要自己读取控件。
package main
import "github.com/tangoui/tango/ui"
var (
filterValues = ui.ProValues{"status": "running"}
filterFields = []ui.ProField{
{Key: "keyword", Label: "关键词"},
{Key: "status", Label: "状态", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "running", Label: "运行中"}, {Value: "paused", Label: "已暂停"}}},
{Key: "amount", Label: "金额", ValueType: ui.ValueMoney},
}
)
func page(c *ui.Context) {
q := ui.QueryFilter(c, filterFields).Values(&filterValues).Show()
if q.Searched() {
requestRules(q.Params())
}
}
func requestRules(params map[string]string) {}
func main() {}默认展开、标签宽度与按钮文案
DefaultExpanded() 适合字段较少但仍希望保留折叠能力的页面。LabelWidth 只影响水平查询项的标签区域;Loading(true) 可在请求未完成时让查询按钮显示加载状态并避免重复点击。
package main
import "github.com/tangoui/tango/ui"
var (
searching bool
expandedFields = []ui.ProField{
{Key: "owner", Label: "负责人"},
{Key: "department", Label: "部门"},
{Key: "updated", Label: "更新时间", ValueType: ui.ValueDateRange},
}
)
func page(c *ui.Context) {
q := ui.QueryFilter(c, expandedFields).
LabelWidth(96).
DefaultExpanded().
SearchText("执行查询").
ResetText("清空条件").
Loading(searching).
Show()
if q.Searched() {
searching = true
startSearch(q.Params())
}
}
func startSearch(params map[string]string) { searching = false }
func main() {}按条件隐藏或禁用字段
Hide 和 Disable 接收当前全部值。隐藏字段不会参与布局,也不会参与查询表单的显示;禁用字段仍可保留已有值,但不能在当前条件下编辑。
package main
import "github.com/tangoui/tango/ui"
var query ui.ProValues
var dependentFields = []ui.ProField{
{Key: "kind", Label: "对象类型", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "project", Label: "项目"}, {Value: "team", Label: "团队"}}},
{Key: "project", Label: "项目", ValueType: ui.ValueSelect,
ValueEnum: []ui.ProEnum{{Value: "tango", Label: "TanGo"}},
Hide: func(v ui.ProValues) bool { return v.String("kind") != "project" }},
{Key: "archived", Label: "包含归档", ValueType: ui.ValueSwitch,
Disable: func(v ui.ProValues) bool { return v.String("kind") == "team" }},
}
func page(c *ui.Context) {
ui.QueryFilter(c, dependentFields).Values(&query).Show()
}
func main() {}在 ProTable 中复用字段定义
ProTable 会用列定义自动创建查询表单;需要独立控制折叠、按钮或外部查询状态时,也可以先用 QueryFilter,再把 Params() 传给自己的列表请求。不要同时让两个查询表单绑定同一份值并各自提交。
package main
import "github.com/tangoui/tango/ui"
type rule struct{ ID, Name string }
var rules = []rule{{ID: "r1", Name: "TanGo"}}
var tableQuery ui.ProValues
func page(c *ui.Context) {
fields := []ui.ProField{{Key: "name", Label: "规则名称"}}
q := ui.QueryFilter(c, fields).Values(&tableQuery).Show()
if q.Searched() {
rows, total := findRules(q.Params())
_ = rows
_ = total
}
}
func findRules(params map[string]string) ([]rule, int) { return rules, len(rules) }
func main() {}主题与样式
QueryFilter 复用 ProField 控件的主题和尺寸。可在局部 ConfigProvider 中设置 labelWidth、defaultCollapsed、searchText、resetText;显式的链式方法优先。Ant Design 主题使用标签冒号和链接式展开,Element Plus 使用对应的表单边框,shadcn 主题使用更紧凑的查询工具栏间距。
注意事项
- 即时模式下,
queryValues应放在页面函数外或页面状态结构中,不能每帧重新声明,否则输入和重置状态会丢失;组件的StateKey 由页面结构和调用位置保持。 - QueryFilter 的
Params()只返回非空值的字符串,适合拼接请求参数;需要保留类型时直接读取绑定的ProValues。 - 字段很多时优先让它默认折叠;大数据量的结果集应交给
ProTable的分页或服务端查询,不要在查询表单中预加载数据。 - 控件和展开按钮支持键盘操作;自定义字段渲染时仍应提供
A11yLabel。ConfigProvider 的Direction会影响布局方向。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.QueryFilter
func QueryFilter(c *Context, fields []ProField) *QueryFilterWQueryFilter starts a query form over fields; chain options, then Show.
ConfigProvider.Defaults("QueryFilter", ...) keys: "labelWidth" (number), "defaultCollapsed" (bool, false = start expanded), "searchText", "resetText" (string).
返回 *QueryFilterW,可链式调用的方法见 QueryFilterW 方法。
QueryFilterW 方法
ui.QueryFilterW:QueryFilterW is a collapsible query form (Ant Design Pro QueryFilter): fields flow in 1–4 columns by width with the buttons in the last cell; beyond the first row they fold behind an expand link. ProTable uses it for its search form.
q := ui.QueryFilter(c, fields).Values(&query).Show()
if q.Searched() { reload(q.Params()) }
| 方法 | 说明 | 签名 |
|---|---|---|
DefaultExpanded | DefaultExpanded starts with every field shown. | DefaultExpanded() *QueryFilterW |
ID | ID returns the widget ID (valid after Show). | ID() ID |
LabelWidth | LabelWidth sets the label width (default 80). | LabelWidth(px float32) *QueryFilterW |
Loading | Loading spins the search button. | Loading(on bool) *QueryFilterW |
Params | Params returns the non-empty values as text (dates as 2006-01-02). | Params() map[string]string |
ResetClicked | ResetClicked reports a reset in the last Show. | ResetClicked() bool |
ResetText | ResetText sets the reset button's text. | ResetText(s string) *QueryFilterW |
SearchText | SearchText sets the search button's text. | SearchText(s string) *QueryFilterW |
Searched | Searched reports a search (or a reset, which searches again) in the last Show. | Searched() bool |
Show | Show builds the query form. | Show() *QueryFilterW |
Values | Values binds the values (otherwise the filter keeps its own). | Values(v *ProValues) *QueryFilterW |
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 |