可以新增分页功能吗?在实际的项目中分页功能十分常见。
type PageModel struct {
List *interface{} `json:"list"`
Total *int64 `json:"total"`
Page int `json:"page"`
Size int `json:"size"`
}
type PageQueryModel struct {
Page int `json:"page"`
Size int `json:"size"`
}
func (db *CustomDB) Page(tx *gorm.DB, pageQuery model.PageQueryModel, dbModeType interface{}) *model.PageModel {
//1.默认参数
if pageQuery.Size < 1 {
pageQuery.Size = 10
}
if pageQuery.Page < 1 {
pageQuery.Page = 1
}
typeOfT := reflect.TypeOf(dbModeType)
dbModel := reflect.New(typeOfT).Interface()
sliceOfT := reflect.SliceOf(typeOfT)
list := reflect.MakeSlice(sliceOfT, 0, 0).Interface()
var total int64
tx.Model(&dbModel).Count(&total)
pageModel := &model.PageModel{
List: &list,
Total: &total,
Page: pageQuery.Page,
Size: pageQuery.Size,
}
offset := pageQuery.Size * (pageQuery.Page - 1)
tx.Limit(pageModel.Size).Offset(offset).Find(&list)
pageModel.List = &list
return pageModel
}
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: jinzhu
https://gorm.io/docs/scopes.html#Pagination