@@ -36,9 +36,11 @@ Output:
36
36
type Paginator struct {
37
37
total int // total rows count, -1 means unknown
38
38
totalPages int // total pages count, -1 means unknown
39
- pagingNum int // how many rows in one page
40
39
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
42
44
}
43
45
44
46
// New initialize a new pagination calculation and returns a Paginator as result.
@@ -49,7 +51,26 @@ func New(total, pagingNum, current, numPages int) *Paginator {
49
51
current = min (current , totalPages )
50
52
}
51
53
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
+ }
53
74
}
54
75
55
76
// IsFirst returns true if current page is the first page.
@@ -71,7 +92,10 @@ func (p *Paginator) Previous() int {
71
92
72
93
// HasNext returns true if there is a next page relative to current page.
73
94
func (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
75
99
}
76
100
77
101
func (p * Paginator ) Next () int {
0 commit comments