分页器调整
This commit is contained in:
parent
285a8077ea
commit
8d4f37cb69
|
@ -254,7 +254,7 @@ func Index(c *gin.Context) {
|
||||||
ginH["totalPage"] = h.getTotalPage(totalRaw)
|
ginH["totalPage"] = h.getTotalPage(totalRaw)
|
||||||
ginH["currentPage"] = h.page
|
ginH["currentPage"] = h.page
|
||||||
ginH["title"] = h.getTitle()
|
ginH["title"] = h.getTitle()
|
||||||
ginH["pagination"] = pagination.NewParsePagination(totalRaw, h.pageSize, h.page, q, c.Request.URL.Path, h.paginationStep)
|
ginH["pagination"] = pagination.NewParsePagination(totalRaw, h.pageSize, h.page, h.paginationStep, q, c.Request.URL.Path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTemplateName() string {
|
func getTemplateName() string {
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fthvgb1/wp-go/helper"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
type PageEle struct {
|
type PageEle struct {
|
||||||
PrevEle string
|
PrevEle string
|
||||||
|
@ -43,3 +48,22 @@ func (p PageEle) Dots() string {
|
||||||
func (p PageEle) Middle(page int, url string) string {
|
func (p PageEle) Middle(page int, url string) string {
|
||||||
return fmt.Sprintf(p.MiddleEle, url, page)
|
return fmt.Sprintf(p.MiddleEle, url, page)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var reg = regexp.MustCompile(`(/page)/(\d+)`)
|
||||||
|
|
||||||
|
func (p PageEle) Url(path, query string, page int) string {
|
||||||
|
if !strings.Contains(path, "/page/") {
|
||||||
|
path = fmt.Sprintf("%s%s", path, "/page/1")
|
||||||
|
}
|
||||||
|
if page == 1 {
|
||||||
|
path = reg.ReplaceAllString(path, "")
|
||||||
|
} else {
|
||||||
|
s := fmt.Sprintf("$1/%d", page)
|
||||||
|
path = reg.ReplaceAllString(path, s)
|
||||||
|
}
|
||||||
|
path = strings.Replace(path, "//", "/", -1)
|
||||||
|
if path == "" {
|
||||||
|
path = "/"
|
||||||
|
}
|
||||||
|
return helper.StrJoin(path, query)
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package pagination
|
package pagination
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/fthvgb1/wp-go/helper"
|
|
||||||
"math"
|
"math"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,6 +11,7 @@ type Elements interface {
|
||||||
Next(url string) string
|
Next(url string) string
|
||||||
Dots() string
|
Dots() string
|
||||||
Middle(page int, url string) string
|
Middle(page int, url string) string
|
||||||
|
Url(path, query string, page int) string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParsePagination struct {
|
type ParsePagination struct {
|
||||||
|
@ -27,7 +25,7 @@ type ParsePagination struct {
|
||||||
Step int
|
Step int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewParsePagination(totalRaw int, pageSize int, currentPage int, query string, path string, step int) ParsePagination {
|
func NewParsePagination(totalRaw int, pageSize int, currentPage, step int, query string, path string) ParsePagination {
|
||||||
allPage := int(math.Ceil(float64(totalRaw) / float64(pageSize)))
|
allPage := int(math.Ceil(float64(totalRaw) / float64(pageSize)))
|
||||||
return ParsePagination{TotalPage: allPage, TotalRaw: totalRaw, PageSize: pageSize, CurrentPage: currentPage, Query: query, Path: path, Step: step}
|
return ParsePagination{TotalPage: allPage, TotalRaw: totalRaw, PageSize: pageSize, CurrentPage: currentPage, Query: query, Path: path, Step: step}
|
||||||
}
|
}
|
||||||
|
@ -37,40 +35,28 @@ func Paginate(e Elements, p ParsePagination) string {
|
||||||
return p.ToHtml()
|
return p.ToHtml()
|
||||||
}
|
}
|
||||||
|
|
||||||
var complie = regexp.MustCompile(`(/page)/(\d+)`)
|
|
||||||
|
|
||||||
func (p ParsePagination) ToHtml() (html string) {
|
func (p ParsePagination) ToHtml() (html string) {
|
||||||
if p.TotalRaw < 2 {
|
if p.TotalRaw < 2 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pathx := p.Path
|
|
||||||
if !strings.Contains(p.Path, "/page/") {
|
|
||||||
pathx = fmt.Sprintf("%s%s", p.Path, "/page/1")
|
|
||||||
}
|
|
||||||
s := strings.Builder{}
|
s := strings.Builder{}
|
||||||
if p.CurrentPage > p.TotalPage {
|
if p.CurrentPage > p.TotalPage {
|
||||||
p.CurrentPage = p.TotalPage
|
p.CurrentPage = p.TotalPage
|
||||||
}
|
}
|
||||||
r := complie
|
|
||||||
start := p.CurrentPage - p.Step
|
start := p.CurrentPage - p.Step
|
||||||
end := p.CurrentPage + p.Step
|
end := p.CurrentPage + p.Step
|
||||||
if start < 1 {
|
if start < 1 {
|
||||||
start = 1
|
start = 1
|
||||||
}
|
}
|
||||||
if p.CurrentPage > 1 {
|
if p.CurrentPage > 1 {
|
||||||
pp := ""
|
s.WriteString(p.Prev(p.Url(p.Path, p.Query, p.CurrentPage-1)))
|
||||||
if p.CurrentPage >= 2 {
|
|
||||||
pp = replacePage(r, pathx, p.CurrentPage-1)
|
|
||||||
}
|
|
||||||
s.WriteString(p.Prev(helper.StrJoin(pp, p.Query)))
|
|
||||||
}
|
}
|
||||||
if p.CurrentPage >= p.Step+2 {
|
if p.CurrentPage >= p.Step+2 {
|
||||||
d := false
|
d := false
|
||||||
if p.CurrentPage > p.Step+2 {
|
if p.CurrentPage > p.Step+2 {
|
||||||
d = true
|
d = true
|
||||||
}
|
}
|
||||||
e := replacePage(r, p.Path, 1)
|
s.WriteString(p.Middle(1, p.Url(p.Path, p.Query, 1)))
|
||||||
s.WriteString(p.Middle(1, helper.StrJoin(e, p.Query)))
|
|
||||||
if d {
|
if d {
|
||||||
s.WriteString(p.Dots())
|
s.WriteString(p.Dots())
|
||||||
}
|
}
|
||||||
|
@ -84,8 +70,7 @@ func (p ParsePagination) ToHtml() (html string) {
|
||||||
if p.CurrentPage == page {
|
if p.CurrentPage == page {
|
||||||
h = p.Current(page)
|
h = p.Current(page)
|
||||||
} else {
|
} else {
|
||||||
d := replacePage(r, pathx, page)
|
h = p.Middle(page, p.Url(p.Path, p.Query, page))
|
||||||
h = p.Middle(page, helper.StrJoin(d, p.Query))
|
|
||||||
}
|
}
|
||||||
s.WriteString(h)
|
s.WriteString(h)
|
||||||
|
|
||||||
|
@ -95,30 +80,14 @@ func (p ParsePagination) ToHtml() (html string) {
|
||||||
if p.TotalPage > p.CurrentPage+p.Step+1 {
|
if p.TotalPage > p.CurrentPage+p.Step+1 {
|
||||||
d = true
|
d = true
|
||||||
}
|
}
|
||||||
dd := replacePage(r, pathx, p.TotalPage)
|
|
||||||
if d {
|
if d {
|
||||||
s.WriteString(p.Dots())
|
s.WriteString(p.Dots())
|
||||||
}
|
}
|
||||||
s.WriteString(p.Middle(p.TotalPage, helper.StrJoin(dd, p.Query)))
|
s.WriteString(p.Middle(p.TotalPage, p.Url(p.Path, p.Query, p.TotalPage)))
|
||||||
}
|
}
|
||||||
if p.CurrentPage < p.TotalPage {
|
if p.CurrentPage < p.TotalPage {
|
||||||
dd := replacePage(r, pathx, p.CurrentPage+1)
|
s.WriteString(p.Next(p.Url(p.Path, p.Query, p.CurrentPage+1)))
|
||||||
s.WriteString(p.Next(helper.StrJoin(dd, p.Query)))
|
|
||||||
}
|
}
|
||||||
html = s.String()
|
html = s.String()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func replacePage(r *regexp.Regexp, path string, page int) (src string) {
|
|
||||||
if page == 1 {
|
|
||||||
src = r.ReplaceAllString(path, "")
|
|
||||||
} else {
|
|
||||||
s := fmt.Sprintf("$1/%d", page)
|
|
||||||
src = r.ReplaceAllString(path, s)
|
|
||||||
}
|
|
||||||
src = strings.Replace(src, "//", "/", -1)
|
|
||||||
if src == "" {
|
|
||||||
src = "/"
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user