pagination.go 935 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package controller
  5. const (
  6. nbItemsPerPage = 100
  7. )
  8. type pagination struct {
  9. Route string
  10. Total int
  11. Offset int
  12. ItemsPerPage int
  13. ShowNext bool
  14. ShowPrev bool
  15. NextOffset int
  16. PrevOffset int
  17. }
  18. func (c *Controller) getPagination(route string, total, offset int) pagination {
  19. nextOffset := 0
  20. prevOffset := 0
  21. showNext := (total - offset) > nbItemsPerPage
  22. showPrev := offset > 0
  23. if showNext {
  24. nextOffset = offset + nbItemsPerPage
  25. }
  26. if showPrev {
  27. prevOffset = offset - nbItemsPerPage
  28. }
  29. return pagination{
  30. Route: route,
  31. Total: total,
  32. Offset: offset,
  33. ItemsPerPage: nbItemsPerPage,
  34. ShowNext: showNext,
  35. NextOffset: nextOffset,
  36. ShowPrev: showPrev,
  37. PrevOffset: prevOffset,
  38. }
  39. }