As the predefined styles of the paging object are relatively limited, sometimes we want to customize the style or tags of the paging. Since all properties and methods of the paging object are public, this provides developers with a high degree of flexibility in customizing paging styles. Developers can customize paging content in the following ways:

  1. Use regex matching to replace the output content for customization.
  2. Organize paging content based on the properties and methods exposed by the paging object for customization.

Custom Tag Replacement

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "github.com/gogf/gf/v2/os/gview"
  6. "github.com/gogf/gf/v2/text/gstr"
  7. "github.com/gogf/gf/v2/util/gpage"
  8. )
  9. // wrapContent wraps each of the page tag with html li and ul.
  10. func wrapContent(page *gpage.Page) string {
  11. content := page.GetContent(4)
  12. content = gstr.ReplaceByMap(content, map[string]string{
  13. "<span": "<li><span",
  14. "/span>": "/span></li>",
  15. "<a": "<li><a",
  16. "/a>": "/a></li>",
  17. })
  18. return "<ul>" + content + "</ul>"
  19. }
  20. func main() {
  21. s := g.Server()
  22. s.BindHandler("/page/custom1/*page", func(r *ghttp.Request) {
  23. page := r.GetPage(100, 10)
  24. content := wrapContent(page)
  25. buffer, _ := gview.ParseContent(`
  26. <html>
  27. <head>
  28. <style>
  29. a,span {padding:8px; font-size:16px;}
  30. div{margin:5px 5px 20px 5px}
  31. </style>
  32. </head>
  33. <body>
  34. <div>{{.page}}</div>
  35. </body>
  36. </html>
  37. `, g.Map{
  38. "page": content,
  39. })
  40. r.Response.Write(buffer)
  41. })
  42. s.SetPort(10000)
  43. s.Run()
  44. }

After execution, the page output is:

Pagination - Custom Paging - 图1

Define Paging Tag Names

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "github.com/gogf/gf/v2/os/gview"
  6. "github.com/gogf/gf/v2/util/gpage"
  7. )
  8. // pageContent customizes the page tag name.
  9. func pageContent(page *gpage.Page) string {
  10. page.NextPageTag = "NextPage"
  11. page.PrevPageTag = "PrevPage"
  12. page.FirstPageTag = "HomePage"
  13. page.LastPageTag = "LastPage"
  14. pageStr := page.FirstPage()
  15. pageStr += page.PrevPage()
  16. pageStr += page.PageBar()
  17. pageStr += page.NextPage()
  18. pageStr += page.LastPage()
  19. return pageStr
  20. }
  21. func main() {
  22. s := g.Server()
  23. s.BindHandler("/page/custom2/*page", func(r *ghttp.Request) {
  24. page := r.GetPage(100, 10)
  25. buffer, _ := gview.ParseContent(`
  26. <html>
  27. <head>
  28. <style>
  29. a,span {padding:8px; font-size:16px;}
  30. div{margin:5px 5px 20px 5px}
  31. </style>
  32. </head>
  33. <body>
  34. <div>{{.page}}</div>
  35. </body>
  36. </html>
  37. `, g.Map{
  38. "page": pageContent(page),
  39. })
  40. r.Response.Write(buffer)
  41. })
  42. s.SetPort(10000)
  43. s.Run()
  44. }

After execution, the page output is:

Pagination - Custom Paging - 图2