Skip to content

Commit

Permalink
chore(docs): custom filter
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Dec 18, 2024
1 parent d723c0a commit 47a3ead
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export default defineConfig(({mode}) => {
{text: '接口参考', link: '/validator/validator'},
]
},
{
text: '筛选器',
items: [
{text: '接口参考', link: '/filter'},
]
},
{
text: '数据库迁移',
items: [
Expand Down
44 changes: 44 additions & 0 deletions docs/filter/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 筛选器

`v1.13.0` 版本开始,我们引入自定义列表筛选器的能力。

## 实现筛选器
```go
package crypto

import (
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/model"
"gorm.io/gorm"
)

type MyFilter struct{}

func (MyFilter) Filter(c *gin.Context, db *gorm.DB, key string, field *model.ResolvedModelField, model *model.ResolvedModel) *gorm.DB {
queryValue := c.Query(key)
if queryValue == "" {
return db
}
// ${FieldName}_like: name_like, phone_like
myColumn := key + "_like"
return myGormSearch(myColumn, queryValue)(db)
}
```

## 注册筛选器
```go
func init() {
filter.RegisterFilter("fussy[my]", MyFilter{})
}
```

## 使用筛选器
定义模型时使用

```go
package model

type MyModel struct {
Name string `json:"name" cosy:"list:fussy[my]"`
}
```
1 change: 1 addition & 0 deletions docs/project-level/define-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type User struct {
| or_equal | SetOrEqual() |
| or_fussy | SetOrFussy() |
| preload | SetPreload() |
| 其他 | 自定义筛选器 |

### item

Expand Down

0 comments on commit 47a3ead

Please sign in to comment.