You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paginate.go 815B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package util
  5. import "reflect"
  6. // PaginateSlice cut a slice as per pagination options
  7. // if page = 0 it do not paginate
  8. func PaginateSlice(list interface{}, page, pageSize int) interface{} {
  9. if page <= 0 || pageSize <= 0 {
  10. return list
  11. }
  12. if reflect.TypeOf(list).Kind() != reflect.Slice {
  13. return list
  14. }
  15. listValue := reflect.ValueOf(list)
  16. page--
  17. if page*pageSize >= listValue.Len() {
  18. return listValue.Slice(listValue.Len(), listValue.Len()).Interface()
  19. }
  20. listValue = listValue.Slice(page*pageSize, listValue.Len())
  21. if listValue.Len() > pageSize {
  22. return listValue.Slice(0, pageSize).Interface()
  23. }
  24. return listValue.Interface()
  25. }