Skip to content

Update basic.md for SetQueryFilterFn #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion zh/admin/table/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ info.SetFilterFormInputWidth(w int)
```go
info.SetQueryFilterFn(func(param parameter.Parameters, conn db.Connection) (ids []string, stopQuery bool) {
// 根据参数与连接对象返回表格筛选id,stopQuery代表是否停止框架逻辑的筛选,是的话直接则返回当前结果
// 以下为简单示例,在 my_table 表中模糊匹配筛选 name 和 description 字段数据
name := param.GetFieldValue("name")
description := param.GetFieldValue("description")
if name == "" && description == "" {
return nil, false // 无查询条件,走默认
}

// 使用原生 SQL 查询
sqlStr := "SELECT id FROM my_table WHERE name LIKE ? AND description LIKE ?"
rows, err := conn.Query(sqlStr, "%"+name+"%", "%"+description+"%")
if err != nil {
return nil, false
}

for _, row := range rows {
id := row["id"].(int64)
ids = append(ids, strconv.FormatInt(id, 10))
}

// 返回主键列表,并停止框架后续查询
return ids, true
})
```

Expand Down Expand Up @@ -539,4 +560,4 @@ func GetUserTable(ctx *context.Context) (userTable table.Table) {
>
> __columns: 隐藏的字段

接口需拿取对应的URL参数进行处理返回对应的JSON格式数据,GoAdmin会将数据展示出来。
接口需拿取对应的URL参数进行处理返回对应的JSON格式数据,GoAdmin会将数据展示出来。