@@ -36,9 +36,11 @@ Output:
3636type Paginator struct {
3737 total int // total rows count, -1 means unknown
3838 totalPages int // total pages count, -1 means unknown
39- pagingNum int // how many rows in one page
4039 current int // current page number
41- numPages int // how many pages to show on the UI
40+ curRows int // current page rows count
41+
42+ pagingNum int // how many rows in one page
43+ numPages int // how many pages to show on the UI
4244}
4345
4446// New initialize a new pagination calculation and returns a Paginator as result.
@@ -49,7 +51,26 @@ func New(total, pagingNum, current, numPages int) *Paginator {
4951 current = min (current , totalPages )
5052 }
5153 current = max (current , 1 )
52- return & Paginator {total , totalPages , pagingNum , current , numPages }
54+ return & Paginator {
55+ total : total ,
56+ totalPages : totalPages ,
57+ current : current ,
58+ pagingNum : pagingNum ,
59+ numPages : numPages ,
60+ }
61+ }
62+
63+ func (p * Paginator ) SetCurRows (rows int ) {
64+ // For "unlimited paging", we need to know the rows of current page to determine if there is a next page.
65+ // There is still an edge case: when curRows==pagingNum, then the "next page" will be an empty page.
66+ // Ideally we should query one more row to determine if there is really a next page, but it's impossible in current framework.
67+ p .curRows = rows
68+ if p .total == - 1 && p .current == 1 && ! p .HasNext () {
69+ // if there is only one page for the "unlimited paging", set total rows/pages count
70+ // then the tmpl could decide to hide the nav bar.
71+ p .total = rows
72+ p .totalPages = util .Iif (p .total == 0 , 0 , 1 )
73+ }
5374}
5475
5576// IsFirst returns true if current page is the first page.
@@ -71,7 +92,10 @@ func (p *Paginator) Previous() int {
7192
7293// HasNext returns true if there is a next page relative to current page.
7394func (p * Paginator ) HasNext () bool {
74- return p .total == - 1 || p .current * p .pagingNum < p .total
95+ if p .total == - 1 {
96+ return p .curRows >= p .pagingNum
97+ }
98+ return p .current * p .pagingNum < p .total
7599}
76100
77101func (p * Paginator ) Next () int {
0 commit comments