woodpecker/server/model/pagination.go
qwerty287 0f9188597e
Initiate Pagination Implementation for API and Infinite Scroll in UI (#1651)
- Add pagination support to the API endpoints that return lists of items
- Adjust UI to enable infinite scrolling via pagination
2023-04-30 03:40:13 +02:00

20 lines
371 B
Go

package model
type ListOptions struct {
All bool
Page int
PerPage int
}
func ApplyPagination[T any](d *ListOptions, slice []T) []T {
if d.All {
return slice
}
if d.PerPage*(d.Page-1) > len(slice) {
return []T{}
}
if d.PerPage*(d.Page) > len(slice) {
return slice[d.PerPage*(d.Page-1):]
}
return slice[d.PerPage*(d.Page-1) : d.PerPage*(d.Page)]
}