I find function of limit, but just receive one parameter, I do not know how to query by paging。 please,tell me! thanks
There's a function called Offset, so to get 20 results starting at row 100 you'd do something like db.Offset(100).Limit(20).Find(&interface, "id = ?", id)
分页能直接返回分页信息吗?包含:
当前页,总页数,总条目数量,数据
@sdvdxl use Count to calculate those information.
do you have some example for this, thanks you :D
I know this is a very old thread but in case anybody is still looking for an example, here's how I am returning the total # of records along with paginated results:
// Usually the client controls how many items it wishes
// to retrieve but for demo, hard-coding it
itemsPerPage := 20
// Again - this will come from the client but for demo
// purposes hard-coding. Generally it's going to be
// (page - 1) * itemsPerPage
offset := 20
// Create *DB object with our filters
filteredUsers := db.Model(&AppUser{}).Where("something = something")
// Declare variable for total rows
var totalRows int
// Populate total row count
filteredUsers.Count(&totalRows)
// Retrieve paginated rows
filteredUsers.Limit(itemsPerPage).Offset(offset).Find(&users)
Anyway, @dduncan1987 was right on the money with their answer but I think maybe some people were still confused as to how to retrieve the paginated results and a total row count. Hopefully this helps some future person with this question.
I don't understand what filteredDB is? I don't see it in the gorm API and you don't assign it in your code block....
I don't understand what filteredDB is? I don't see it in the gorm API and you don't assign it in your code block....
@sean-abbott that was a copy/paste error. I fixed it for you but the upshot is: you chain Count and then Limit/Offset on your already-filtered *DB. Hope that makes sense!
Most helpful comment
分页能直接返回分页信息吗?包含:
当前页,总页数,总条目数量,数据