Introduction

Pagination management is implemented by the gpage module. gpage offers powerful dynamic and static pagination functionalities and provides developers with high flexibility in customizing pagination styles.

Pagination - 图1tip

The gpage module is primarily used for generating HTML code for pagination, commonly used in MVC development scenarios.

Usage:

  1. import "github.com/gogf/gf/v2/util/gpage"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/util/gpage

Pagination Object:

  1. // Page is the pagination implementer.
  2. // All the attributes are public, you can change them when necessary.
  3. type Page struct {
  4. TotalSize int // Total size.
  5. TotalPage int // Total page, which is automatically calculated.
  6. CurrentPage int // Current page number >= 1.
  7. UrlTemplate string // Custom url template for page url producing.
  8. LinkStyle string // CSS style name for HTML link tag <a>.
  9. SpanStyle string // CSS style name for HTML span tag <span>, which is used for first, current and last page tag.
  10. SelectStyle string // CSS style name for HTML select tag <select>.
  11. NextPageTag string // Tag name for next p.
  12. PrevPageTag string // Tag name for prev p.
  13. FirstPageTag string // Tag name for first p.
  14. LastPageTag string // Tag name for last p.
  15. PrevBarTag string // Tag string for prev bar.
  16. NextBarTag string // Tag string for next bar.
  17. PageBarNum int // Page bar number for displaying.
  18. AjaxActionName string // Ajax function name. Ajax is enabled if this attribute is not empty.
  19. }

Creating Pagination Objects

Since pagination objects are often used in Web services, starting from version v1.12 of the framework, we provide a more convenient way to create pagination objects. Pagination objects are integrated into the ghttp.Request object, allowing you to easily obtain pagination objects through the Request.GetPage method. The method is defined as follows:

  1. func (r *Request) GetPage(totalSize, pageSize int) *gpage.Page

As you can see, to obtain a pagination object, you only need to pass the total number and page quantity. Of course, pagination objects can also be used independently. Due to space limitations, we only introduce the most commonly used and simplest method here.

Please refer to subsequent chapters for specific usage examples.

Predefined Pagination Styles

The GetContent method provides predefined common pagination styles for developers to use quickly. When predefined styles do not meet the developers’ needs, they can use open methods to customize pagination styles (or override methods for customization) or use regular expressions to replace certain parts of the predefined pagination style for customization.

Please refer to subsequent chapters for specific usage examples.

Using Ajax Pagination Feature

The AjaxActionName attribute of the pagination object is used to specify an Ajax method name for implementing Ajax pagination. However, it should be noted that this Ajax method name needs to be consistently agreed upon between the front and backend, and the Ajax method should have only one URL parameter. Below is an example of a client-side definition of an Ajax method:

  1. function DoAjax(url) {
  2. // Read the content of the URL here and display it according to business logic
  3. }

Please refer to subsequent chapters for specific usage examples.

Documentation

📄️ Pagination - Dynamic PagingThis document introduces how to use dynamic paging in the GoFrame framework by passing paging configuration through GET parameters, with the default parameter name being ‘page’. Through the provided sample code, users can learn how to integrate four predefined paging styles on a webpage and implement the paging management process.

📄️ Pagination - Static PagingImplement static paging management in the GoFrame framework. Static paging is achieved by using route parameters, which has a high level of coupling. This example illustrates how to use fuzzy matching routes, named matching routes, and field matching routes in the GoFrame framework to achieve the paging function, allowing the paging object to accept paging parameters from the route, thereby achieving split page display.

📄️ Pagination - Ajax PagingTechnical details on implementing pagination management using the Ajax method. Unlike traditional pagination, Ajax pagination dynamically retrieves and renders pagination content using Javascript methods for a smoother user experience. The sample code demonstrates how to integrate Ajax pagination functionality in the GoFrame framework, providing a practical backend pagination solution.

📄️ Pagination - URL TemplateUse the gpage of the GoFrame framework for pagination management and realize dynamic page rendering by replacing page number content with built-in variables through the custom URL template function. The article provides detailed code examples demonstrating how to achieve personalized pagination URL configuration by setting the UrlTemplate property, offering developers a flexible and efficient solution.

📄️ Pagination - Custom PagingImplement custom paging styles and tags in the GoFrame framework. Developers can achieve higher flexibility and personalization by replacing or organizing paging content through regex matching of the properties and methods exposed by the paging object.