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.
tip
The gpage
module is primarily used for generating HTML code for pagination, commonly used in MVC
development scenarios.
Usage:
import "github.com/gogf/gf/v2/util/gpage"
API Documentation:
https://pkg.go.dev/github.com/gogf/gf/v2/util/gpage
Pagination Object:
// Page is the pagination implementer.
// All the attributes are public, you can change them when necessary.
type Page struct {
TotalSize int // Total size.
TotalPage int // Total page, which is automatically calculated.
CurrentPage int // Current page number >= 1.
UrlTemplate string // Custom url template for page url producing.
LinkStyle string // CSS style name for HTML link tag <a>.
SpanStyle string // CSS style name for HTML span tag <span>, which is used for first, current and last page tag.
SelectStyle string // CSS style name for HTML select tag <select>.
NextPageTag string // Tag name for next p.
PrevPageTag string // Tag name for prev p.
FirstPageTag string // Tag name for first p.
LastPageTag string // Tag name for last p.
PrevBarTag string // Tag string for prev bar.
NextBarTag string // Tag string for next bar.
PageBarNum int // Page bar number for displaying.
AjaxActionName string // Ajax function name. Ajax is enabled if this attribute is not empty.
}
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:
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:
function DoAjax(url) {
// Read the content of the URL here and display it according to business logic
}
Please refer to subsequent chapters for specific usage examples.