Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 39e134b

Browse files
authored
Merge pull request studygolang#52 from javasgl/feature.support.paginator
首页 all tab 支持分页
2 parents a4454f5 + 1a8b6a5 commit 39e134b

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

src/http/controller/index.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/labstack/echo"
2222
"github.com/polaris1119/config"
23+
"github.com/polaris1119/goutils"
2324
"github.com/polaris1119/logger"
2425
)
2526

@@ -46,11 +47,23 @@ func (IndexController) Index(ctx echo.Context) error {
4647
if tab == "" {
4748
tab = logic.WebsiteSetting.IndexNavs[0].Tab
4849
}
50+
paginator := logic.NewPaginator(goutils.MustInt(ctx.QueryParam("p"), 1))
51+
52+
data := logic.DefaultIndex.FindData(ctx, tab, paginator)
4953

50-
data := logic.DefaultIndex.FindData(ctx, tab)
5154
SetCookie(ctx, "INDEX_TAB", data["tab"].(string))
55+
5256
data["all_nodes"] = logic.GenNodes()
5357

58+
if tab == "all" {
59+
pageHtml := paginator.SetTotal(logic.DefaultFeed.GetTotalCount(ctx)).GetPageHtml(ctx.Request().URL().Path())
60+
61+
data["page"] = template.HTML(pageHtml)
62+
63+
data["total"] = paginator.GetTotal()
64+
65+
}
66+
5467
return render(ctx, "index.html", data)
5568
}
5669

src/logic/feed.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ type FeedLogic struct{}
2323

2424
var DefaultFeed = FeedLogic{}
2525

26+
func (self FeedLogic) GetTotalCount(ctx context.Context) int64 {
27+
objLog := GetLogger(ctx)
28+
count, err := MasterDB.Where("state=0").Count(new(model.Feed))
29+
if err != nil {
30+
objLog.Errorln("FeedLogic Count error:", err)
31+
return 0
32+
}
33+
return count
34+
}
35+
36+
func (self FeedLogic) FindRecentWithPaginator(ctx context.Context, paginator *Paginator) []*model.Feed {
37+
objLog := GetLogger(ctx)
38+
39+
feeds := make([]*model.Feed, 0)
40+
err := MasterDB.Desc("updated_at").Limit(paginator.PerPage(), paginator.Offset()).Find(&feeds)
41+
if err != nil {
42+
objLog.Errorln("FeedLogic FindRecent error:", err)
43+
return nil
44+
}
45+
46+
return self.fillOtherInfo(ctx, feeds, true)
47+
}
48+
2649
func (self FeedLogic) FindRecent(ctx context.Context, num int) []*model.Feed {
2750
objLog := GetLogger(ctx)
2851

src/logic/index.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type IndexLogic struct{}
1919

2020
var DefaultIndex = IndexLogic{}
2121

22-
func (IndexLogic) FindData(ctx context.Context, tab string) map[string]interface{} {
22+
func (IndexLogic) FindData(ctx context.Context, tab string, paginator *Paginator) map[string]interface{} {
23+
2324
indexNav := GetCurIndexNav(tab)
2425
if indexNav == nil {
2526
indexNav = WebsiteSetting.IndexNavs[0]
@@ -41,7 +42,7 @@ func (IndexLogic) FindData(ctx context.Context, tab string) map[string]interface
4142
switch {
4243
case indexNav.DataSource == "feed":
4344
topFeeds := DefaultFeed.FindTop(ctx)
44-
feeds := DefaultFeed.FindRecent(ctx, 50)
45+
feeds := DefaultFeed.FindRecentWithPaginator(ctx, paginator)
4546
data["feeds"] = append(topFeeds, feeds...)
4647
case isNid:
4748
paginator := NewPaginator(1)

src/logic/page.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ func (this *Paginator) SetTotal(total int64) *Paginator {
143143
return this
144144
}
145145

146+
func (this *Paginator) GetTotal() int {
147+
return this.total
148+
}
149+
146150
func (this *Paginator) Offset() (offset int) {
147151
if this.curPage > 1 {
148152
offset = (this.curPage - 1) * this.perPage

template/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</div>
2020
{{end}}
2121

22+
2223
{{with or .feeds .topics .articles .docs}}
2324

2425
{{range $i, $feed := $.feeds}}
@@ -82,6 +83,7 @@
8283
{{end}}
8384
</tr>
8485
</tbody></table>
86+
8587
</div>
8688
{{if eq $i 4}}
8789
{{if $.pos_ad.banner}}
@@ -278,6 +280,14 @@
278280
{{end}}
279281
{{end}}
280282
{{end}}
283+
284+
{{if $.page}}
285+
<div class="box_white">
286+
<div class="inner_content">
287+
<nav class="text-center"><ul class="pagination pagination-sm" style="margin: 1px 0;">{{$.page}}</ul></nav>
288+
</div>
289+
</div>
290+
{{end}}
281291

282292
{{else}}
283293

0 commit comments

Comments
 (0)