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.
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.
Documents
📄️ Pagination - Dynamic Paging
This 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 Paging
Implement 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 Paging
Technical 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 Template
Use 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 Paging
Implement 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.