The difference between Ajax pagination and other pagination methods is that the pagination links are implemented using Javascript methods. This Javascript method is the pagination method, with fixed parameters for the pagination URL address corresponding to the pagination. This Javascript method retrieves the pagination content corresponding to the URL link via Ajax and renders it onto the page.

    A complete example is as follows:

    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. )
    7. func main() {
    8. s := g.Server()
    9. s.BindHandler("/page/ajax", func(r *ghttp.Request) {
    10. page := r.GetPage(100, 10)
    11. page.AjaxActionName = "DoAjax"
    12. buffer, _ := gview.ParseContent(`
    13. <html>
    14. <head>
    15. <style>
    16. a,span {padding:8px; font-size:16px;}
    17. div{margin:5px 5px 20px 5px}
    18. </style>
    19. <script src="https://cdn.bootcss.com/jquery/2.0.3/jquery.min.js"></script>
    20. <script>
    21. function DoAjax(url) {
    22. $.get(url, function(data,status) {
    23. $("body").html(data);
    24. });
    25. }
    26. </script>
    27. </head>
    28. <body>
    29. <div>{{.page1}}</div>
    30. <div>{{.page2}}</div>
    31. <div>{{.page3}}</div>
    32. <div>{{.page4}}</div>
    33. </body>
    34. </html>
    35. `, g.Map{
    36. "page1": page.GetContent(1),
    37. "page2": page.GetContent(2),
    38. "page3": page.GetContent(3),
    39. "page4": page.GetContent(4),
    40. })
    41. r.Response.Write(buffer)
    42. })
    43. s.SetPort(8199)
    44. s.Run()
    45. }

    In this example, we define a DoAjax(url) method to perform pagination operations. For demonstration purposes, its logic is quite simple, loading the content of the specified pagination page and replacing the current page’s pagination content.

    1. function DoAjax(url) {
    2. $.get(url, function(data,status) {
    3. $("body").html(data);
    4. });
    5. }