-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]"` | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters