Simple tool to orginize pagination in your web applications for Golang. It uses mechanism of pagination like google.
To install package:
go get github.com/gigovich/simpagin
pg, err := simpagin.New(
10, // Active page which items we displaying now
120, // Total count of items
8, // We show only 8 items in each page
10, // And our paginator rendered as 10 pages list
)
if err != nil {
log.Fatal(err)
}
// Show paginator
fmt.Printf("<a href=\"/page/%s/\"><</a>", pg.LeftPage.Number)
for _, page := range pg.PageList {
if page.IsActive {
fmt.Print(page.Number)
} else {
fmt.Printf("<a href=\"/page/%s/\"><</a>", pg.LeftPage.Number)
}
}
fmt.Printf("<a href=\"/page/%s/\">></a>", pg.RightPage.Number)
pg, err := simpagin.New(
10, // Active page which items we displaying now
120, // Total count of items
8, // We show only 8 items in each page
10, // And our paginator rendered as 10 pages list
)
if err != nil {
log.Fatal(err)
}
pg.SetRenderer(func (p Page) string {
switch p.Type {
case simpagin.LEFT:
if p.Number == 0 {
return "<li class=\"disabled\"><span>«</span></li>"
} else {
return fmt.Sprintf("<li><a href=\"?p=%d\">«</a></li>", p.Number)
}
case simpagin.MIDDLE:
if p.IsActive {
return fmt.Sprintf("<li class=\"active\"><span>%d</span></li>", p.Number)
} else {
return fmt.Sprintf("<li><a href=\"?p=%d\">%d</a></li>", p.Number, p.Number)
}
case simpagin.RIGHT:
if p.Number == 0 {
return "<li class=\"disabled\"><span>»</span></li>"
} else {
return fmt.Sprintf("<li><a href=\"?p=%d\">»</a></li>", p.Number, p.Number)
}
}
return ""
})
// Now each page element know how to render himself
fmt.Print(pg.LeftPage)
for _, page := range pg.PageList {
fmt.Print(page)
}
fmt.Print(pg.RightPage)