pagination

package module
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 22

README

分页组件

支持两种分页模式:

  1. 页码分页模式
  2. 偏移值分页模式

分页方式

按照页码分页
import (
    "github.com/webx-top/echo"
    "github.com/webx-top/pagination"
)

func index(ctx echo.Context) error {
    p := pagination.New(ctx)
    totalRows := 1000
    page := ctx.Formx("page").Int()
    if page < 1 {
        page = 1
    }
    links := 10
    limit := ctx.Formx("size").Int()
    if limit < 0 {
        limit = 20
    }
    p.SetAll(`tmpl/pagination`, totalRows, page, links, limit)
    // 自动根据当前网址生成分页网址
    p.SetURL(nil, `_pjax`)
    /* 或者手动指定:
    q := ctx.Request().URL().Query()
    q.Del(`page`)
    q.Del(`rows`)
    q.Del(`_pjax`)
    p.SetURL(ctx.Request().URL().Path() + `?` + q.Encode() + `&page={page}&rows={rows}`)
    // p.SetPage(page).SetRows(totalRows))
    */
    ctx.Set(`pagination`, p)
    return ctx.Render(`list`, nil)
}

按照偏移值分页
import (
    "github.com/webx-top/echo"
    "github.com/webx-top/pagination"
)

func index(ctx echo.Context) error {
    p := pagination.New(ctx)
    offset := ctx.Form(`offset`)
    prevOffset := ctx.Form(`prev`)

    var nextOffset string
    // TODO:获取nextOffset
    // nextOffset = "你的下一页偏移值"

    // 自动根据当前网址生成分页网址
    p.SetURL(nil, `_pjax`)
    /* 或者手动指定:
    q := ctx.Request().URL().Query()
    q.Del(`offset`)
    q.Del(`prev`)
    q.Del(`_pjax`)
    p.SetURL(ctx.Request().URL().Path() + `?` + q.Encode() + `&offset={curr}&prev={prev}`)
    */
    p.SetPosition(prevOffset, nextOffset, offset)
    ctx.Set(`pagination`, p)
    return ctx.Render(`list`, nil)
}

模板中输出分页

list.html

{{Stored.pagination.Render}}
<!-- 或者指定分页链接的模板:{{Stored.pagination.Render "pagination_cursor"}} -->

Documentation

Index

Constants

View Source
const (
	// ModePageNumber 页码模式
	ModePageNumber = iota + 1
	// ModePosition 偏移值模式
	ModePosition
)
View Source
const (
	VarNamePage          = `page`
	VarNameSize          = `size`
	VarNameRows          = `rows`
	VarNameOffsetCurrent = `curr`
	VarNameOffsetNext    = `next`
	VarNameOffsetPrev    = `prev`
	VarNameOffset        = `offset`
)

Variables

View Source
var (
	// DefaultPageVarsMap tagName=>urlVar
	DefaultPageVarsMap = echo.KVList{
		{K: VarNamePage, V: VarNamePage},
		{K: VarNameSize, V: VarNameSize},
		{K: VarNameRows, V: VarNameRows},
	}
	// DefaultPositionVarsMap tagName=>urlVar
	DefaultPositionVarsMap = echo.KVList{
		{K: VarNameOffsetNext, V: VarNameOffsetNext},
		{K: VarNameOffsetCurrent, V: VarNameOffset},
		{K: VarNameOffsetPrev, V: VarNameOffsetPrev},
		{K: VarNameSize, V: VarNameSize},
	}
)

Functions

This section is empty.

Types

type Applier added in v0.0.3

type Applier interface {
	Apply(*Pagination)
}

type Pagination

type Pagination struct {
	// contains filtered or unexported fields
}

Pagination 分页

func New

func New(ctx echo.Context) *Pagination

New 创建分页实例

func (*Pagination) BuildQueryString added in v0.2.3

func (p *Pagination) BuildQueryString(query url.Values, delKeys ...string) string

func (*Pagination) CleanQuery added in v0.2.3

func (p *Pagination) CleanQuery(query url.Values, delKeys ...string) url.Values

func (*Pagination) Data

func (p *Pagination) Data() echo.H

Data 获取整个附加数据

func (*Pagination) Get

func (p *Pagination) Get(key string) interface{}

Get 获取某个附加数据

func (*Pagination) HasNext

func (p *Pagination) HasNext() bool

HasNext 是否有下一页

func (*Pagination) HasPrev

func (p *Pagination) HasPrev() bool

HasPrev 是否有上一页

func (*Pagination) IsPosition added in v0.2.4

func (p *Pagination) IsPosition() bool

func (*Pagination) Limit

func (p *Pagination) Limit() int

func (*Pagination) List

func (p *Pagination) List(num ...int) []int

func (*Pagination) MarshalJSON

func (p *Pagination) MarshalJSON() ([]byte, error)

MarshalJSON allows type Pagination to be used with json.Marshal

func (*Pagination) MarshalXML

func (p *Pagination) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML allows type Pagination to be used with xml.Marshal

func (*Pagination) NextPage

func (p *Pagination) NextPage() int

func (*Pagination) NextPosition

func (p *Pagination) NextPosition() string

NextPosition 下一页偏移值

func (*Pagination) Num

func (p *Pagination) Num() int

func (*Pagination) Offset added in v0.0.2

func (p *Pagination) Offset() int

func (*Pagination) Options

func (p *Pagination) Options() echo.H

func (*Pagination) Page

func (p *Pagination) Page() int

func (*Pagination) Pages

func (p *Pagination) Pages() int

func (*Pagination) Position

func (p *Pagination) Position() string

Position 当前偏移值

func (*Pagination) PrevPage

func (p *Pagination) PrevPage() int

func (*Pagination) PrevPosition

func (p *Pagination) PrevPosition() string

PrevPosition 上一页偏移值

func (*Pagination) RebuildQueryString

func (p *Pagination) RebuildQueryString(delKeys ...string) string

func (*Pagination) RebuildURL

func (p *Pagination) RebuildURL(pageVars map[string]string, delKeys ...string) string

func (*Pagination) Render

func (p *Pagination) Render(settings ...string) interface{}

func (*Pagination) Rows

func (p *Pagination) Rows() int

func (*Pagination) Set

func (p *Pagination) Set(key string, data interface{}) *Pagination

Set 设置附加数据

func (*Pagination) SetAll

func (p *Pagination) SetAll(tmpl string, rows int, pageAndLinksAndSize ...int) *Pagination

SetAll 设置按页码分页模式所需的所有参数 tmpl 模板 rows 总行数 pageAndLinksAndSize[0] 当前页码 pageAndLinksAndSize[1] 分页链接数量 pageAndLinksAndSize[2] 每页数量

func (*Pagination) SetLimit

func (p *Pagination) SetLimit(limit int) *Pagination

func (*Pagination) SetNum

func (p *Pagination) SetNum(num int) *Pagination

func (*Pagination) SetOptions

func (p *Pagination) SetOptions(m echo.H) *Pagination

func (*Pagination) SetPage

func (p *Pagination) SetPage(page int) *Pagination

func (*Pagination) SetPosition

func (p *Pagination) SetPosition(prev string, next string, curr string) *Pagination

SetPosition 设置按偏移值分页模式所需的所有参数 prev 上一页偏移值 next 下一页偏移值 curr 当前页偏移值

func (*Pagination) SetRows

func (p *Pagination) SetRows(rows int) *Pagination

func (*Pagination) SetSize

func (p *Pagination) SetSize(size int) *Pagination

SetSize SetLimit方法的别名

func (*Pagination) SetTmpl

func (p *Pagination) SetTmpl(tmpl string) *Pagination

func (*Pagination) SetURL

func (p *Pagination) SetURL(s interface{}, delKeys ...string) *Pagination

func (*Pagination) Sets

func (p *Pagination) Sets(args ...interface{}) *Pagination

Sets 设置多个附加数据(参数按照key,value,key1,value1的格式)

func (*Pagination) Size

func (p *Pagination) Size() int

Size Limit方法的别名

func (*Pagination) Tmpl

func (p *Pagination) Tmpl() string

func (*Pagination) URL

func (p *Pagination) URL(curr interface{}) (s string)

func (*Pagination) UnmarshalJSON added in v0.0.3

func (p *Pagination) UnmarshalJSON(b []byte) error

func (*Pagination) UnmarshalXML added in v0.0.3

func (p *Pagination) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

Jump to

Keyboard shortcuts

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