paging

package
v1.90.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package paging contains utilities to handle pagination.

The Paging struct represents the paging information and it is automatically populated by the New function.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeOffsetAndLimit

func ComputeOffsetAndLimit(currentPage, pageSize uint) (uint, uint)

ComputeOffsetAndLimit computes the OFFSET (zero based) and LIMIT values to be used with SQL queries.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/paging"
)

func main() {
	var (
		currentPage uint = 3
		pageSize    uint = 5
	)

	offset, limit := paging.ComputeOffsetAndLimit(currentPage, pageSize)

	fmt.Println(offset)
	fmt.Println(limit)

}
Output:

10
5

Types

type Paging

type Paging struct {
	// CurrentPage is the current page number starting from 1.
	CurrentPage uint `json:"page"`

	// PageSize is the maximum number of items that can be contained in a page. It is also the LIMIT in SQL queries.
	PageSize uint `json:"page_size"`

	// TotalItems is the total number of items to be paginated.
	TotalItems uint `json:"total_items"`

	// TotalPages is the total number of pages required to contain all the items.
	TotalPages uint `json:"total_pages"`

	// PreviousPage is the previous page. It is equal to 1 if we are on the first page (CurrentPage == 1).
	PreviousPage uint `json:"previous_page"`

	// NextPage is the next page. It is equal to TotalPages if we are on the last page (CurrentPage == TotalPages).
	NextPage uint `json:"next_page"`

	// Offset is the zero-based number of items before the current page.
	Offset uint `json:"offset"`
}

Paging contains the paging information.

func New

func New(currentPage, pageSize, totalItems uint) Paging

New returns a new paging information instance.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/paging"
)

func main() {
	var (
		currentPage uint = 3
		pageSize    uint = 5
		totalItems  uint = 17
	)

	// calculate new paging parameters
	p := paging.New(currentPage, pageSize, totalItems)

	fmt.Println(p)

}
Output:

{3 5 17 4 2 4 10}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL