Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pagination.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package context
  4. import (
  5. "fmt"
  6. "html/template"
  7. "net/url"
  8. "strings"
  9. "code.gitea.io/gitea/modules/paginator"
  10. )
  11. // Pagination provides a pagination via paginator.Paginator and additional configurations for the link params used in rendering
  12. type Pagination struct {
  13. Paginater *paginator.Paginator
  14. urlParams []string
  15. }
  16. // NewPagination creates a new instance of the Pagination struct.
  17. // "pagingNum" is "page size" or "limit", "current" is "page"
  18. func NewPagination(total, pagingNum, current, numPages int) *Pagination {
  19. p := &Pagination{}
  20. p.Paginater = paginator.New(total, pagingNum, current, numPages)
  21. return p
  22. }
  23. // AddParamString adds a string parameter directly
  24. func (p *Pagination) AddParamString(key, value string) {
  25. urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(value))
  26. p.urlParams = append(p.urlParams, urlParam)
  27. }
  28. // GetParams returns the configured URL params
  29. func (p *Pagination) GetParams() template.URL {
  30. return template.URL(strings.Join(p.urlParams, "&"))
  31. }
  32. // SetDefaultParams sets common pagination params that are often used
  33. func (p *Pagination) SetDefaultParams(ctx *Context) {
  34. if v, ok := ctx.Data["SortType"].(string); ok {
  35. p.AddParamString("sort", v)
  36. }
  37. if v, ok := ctx.Data["Keyword"].(string); ok {
  38. p.AddParamString("q", v)
  39. }
  40. if v, ok := ctx.Data["IsFuzzy"].(bool); ok {
  41. p.AddParamString("fuzzy", fmt.Sprint(v))
  42. }
  43. // do not add any more uncommon params here!
  44. }