paginator.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pagination
  15. import (
  16. "math"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. )
  21. // Paginator within the state of a http request.
  22. type Paginator struct {
  23. Request *http.Request
  24. PerPageNums int
  25. MaxPages int
  26. nums int64
  27. pageRange []int
  28. pageNums int
  29. page int
  30. }
  31. // PageNums Returns the total number of pages.
  32. func (p *Paginator) PageNums() int {
  33. if p.pageNums != 0 {
  34. return p.pageNums
  35. }
  36. pageNums := math.Ceil(float64(p.nums) / float64(p.PerPageNums))
  37. if p.MaxPages > 0 {
  38. pageNums = math.Min(pageNums, float64(p.MaxPages))
  39. }
  40. p.pageNums = int(pageNums)
  41. return p.pageNums
  42. }
  43. // Nums Returns the total number of items (e.g. from doing SQL count).
  44. func (p *Paginator) Nums() int64 {
  45. return p.nums
  46. }
  47. // SetNums Sets the total number of items.
  48. func (p *Paginator) SetNums(nums interface{}) {
  49. p.nums, _ = toInt64(nums)
  50. }
  51. // Page Returns the current page.
  52. func (p *Paginator) Page() int {
  53. if p.page != 0 {
  54. return p.page
  55. }
  56. if p.Request.Form == nil {
  57. p.Request.ParseForm()
  58. }
  59. p.page, _ = strconv.Atoi(p.Request.Form.Get("p"))
  60. if p.page > p.PageNums() {
  61. p.page = p.PageNums()
  62. }
  63. if p.page <= 0 {
  64. p.page = 1
  65. }
  66. return p.page
  67. }
  68. // Pages Returns a list of all pages.
  69. //
  70. // Usage (in a view template):
  71. //
  72. // {{range $index, $page := .paginator.Pages}}
  73. // <li{{if $.paginator.IsActive .}} class="active"{{end}}>
  74. // <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
  75. // </li>
  76. // {{end}}
  77. func (p *Paginator) Pages() []int {
  78. if p.pageRange == nil && p.nums > 0 {
  79. var pages []int
  80. pageNums := p.PageNums()
  81. page := p.Page()
  82. switch {
  83. case page >= pageNums-4 && pageNums > 9:
  84. start := pageNums - 9 + 1
  85. pages = make([]int, 9)
  86. for i := range pages {
  87. pages[i] = start + i
  88. }
  89. case page >= 5 && pageNums > 9:
  90. start := page - 5 + 1
  91. pages = make([]int, int(math.Min(9, float64(page+4+1))))
  92. for i := range pages {
  93. pages[i] = start + i
  94. }
  95. default:
  96. pages = make([]int, int(math.Min(9, float64(pageNums))))
  97. for i := range pages {
  98. pages[i] = i + 1
  99. }
  100. }
  101. p.pageRange = pages
  102. }
  103. return p.pageRange
  104. }
  105. // PageLink Returns URL for a given page index.
  106. func (p *Paginator) PageLink(page int) string {
  107. link, _ := url.ParseRequestURI(p.Request.URL.String())
  108. values := link.Query()
  109. if page == 1 {
  110. values.Del("p")
  111. } else {
  112. values.Set("p", strconv.Itoa(page))
  113. }
  114. link.RawQuery = values.Encode()
  115. return link.String()
  116. }
  117. // PageLinkPrev Returns URL to the previous page.
  118. func (p *Paginator) PageLinkPrev() (link string) {
  119. if p.HasPrev() {
  120. link = p.PageLink(p.Page() - 1)
  121. }
  122. return
  123. }
  124. // PageLinkNext Returns URL to the next page.
  125. func (p *Paginator) PageLinkNext() (link string) {
  126. if p.HasNext() {
  127. link = p.PageLink(p.Page() + 1)
  128. }
  129. return
  130. }
  131. // PageLinkFirst Returns URL to the first page.
  132. func (p *Paginator) PageLinkFirst() (link string) {
  133. return p.PageLink(1)
  134. }
  135. // PageLinkLast Returns URL to the last page.
  136. func (p *Paginator) PageLinkLast() (link string) {
  137. return p.PageLink(p.PageNums())
  138. }
  139. // HasPrev Returns true if the current page has a predecessor.
  140. func (p *Paginator) HasPrev() bool {
  141. return p.Page() > 1
  142. }
  143. // HasNext Returns true if the current page has a successor.
  144. func (p *Paginator) HasNext() bool {
  145. return p.Page() < p.PageNums()
  146. }
  147. // IsActive Returns true if the given page index points to the current page.
  148. func (p *Paginator) IsActive(page int) bool {
  149. return p.Page() == page
  150. }
  151. // Offset Returns the current offset.
  152. func (p *Paginator) Offset() int {
  153. return (p.Page() - 1) * p.PerPageNums
  154. }
  155. // HasPages Returns true if there is more than one page.
  156. func (p *Paginator) HasPages() bool {
  157. return p.PageNums() > 1
  158. }
  159. // NewPaginator Instantiates a paginator struct for the current http request.
  160. func NewPaginator(req *http.Request, per int, nums interface{}) *Paginator {
  161. p := Paginator{}
  162. p.Request = req
  163. if per <= 0 {
  164. per = 10
  165. }
  166. p.PerPageNums = per
  167. p.SetNums(nums)
  168. return &p
  169. }