Git Product home page Git Product logo

pagination's Introduction

分页组件

支持两种分页模式:

  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"}} -->

pagination's People

Contributors

admpub avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.