Skip to content

Commit f10dd0b

Browse files
committed
fix paging
1 parent 96cbabb commit f10dd0b

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

modules/paginator/paginator.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ Output:
3636
type 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,22 @@ 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+
p.curRows = rows
65+
if p.total == -1 && p.current == 1 && !p.HasNext() {
66+
// if there is only one page for the "unlimited paging", set total rows/pages count
67+
p.total = rows
68+
p.totalPages = 1
69+
}
5370
}
5471

5572
// IsFirst returns true if current page is the first page.
@@ -71,7 +88,10 @@ func (p *Paginator) Previous() int {
7188

7289
// HasNext returns true if there is a next page relative to current page.
7390
func (p *Paginator) HasNext() bool {
74-
return p.total == -1 || p.current*p.pagingNum < p.total
91+
if p.total == -1 {
92+
return p.curRows >= p.pagingNum
93+
}
94+
return p.current*p.pagingNum < p.total
7595
}
7696

7797
func (p *Paginator) Next() int {

routers/web/user/home.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ func Dashboard(ctx *context.Context) {
137137
return
138138
}
139139

140-
ctx.Data["Feeds"] = feeds
141-
142-
pager := context.NewPagination(count, setting.UI.FeedPagingNum, page, 5)
140+
pager := context.NewPagination(count, setting.UI.FeedPagingNum, page, 5).WithCurRows(len(feeds))
143141
pager.AddParamFromRequest(ctx.Req)
144142
ctx.Data["Page"] = pager
143+
ctx.Data["Feeds"] = feeds
145144

146145
ctx.HTML(http.StatusOK, tplDashboard)
147146
}

services/context/pagination.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func NewPagination(total, pagingNum, current, numPages int) *Pagination {
2828
return p
2929
}
3030

31+
func (p *Pagination) WithCurRows(n int) *Pagination {
32+
p.Paginater.SetCurRows(n)
33+
return p
34+
}
35+
3136
func (p *Pagination) AddParamFromRequest(req *http.Request) {
3237
for key, values := range req.URL.Query() {
3338
if key == "page" || len(values) == 0 || (len(values) == 1 && values[0] == "") {

templates/user/dashboard/dashboard.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="flex-container-main">
66
{{template "base/alert" .}}
77
{{template "user/heatmap" .}}
8-
{{if .Feeds}}
8+
{{if .Page.Paginater.TotalPages}}
99
{{template "user/dashboard/feeds" .}}
1010
{{else}}
1111
{{template "user/dashboard/guide" .}}

0 commit comments

Comments
 (0)