Git Product home page Git Product logo

simpagin's Introduction

Simpagin - simple pagination tool

Simple tool to orginize pagination in your web applications for Golang. It uses mechanism of pagination like google.

To install package:

go get github.com/gigovich/simpagin

Example - most simple

pg, err := simpagin.New(
	10,  // Active page which items we displaying now
	120, // Total count of items
	8,   // We show only 8 items in each page
	10,  // And our paginator rendered as 10 pages list
)
if err != nil {
	log.Fatal(err)
}
// Show paginator
fmt.Printf("<a href=\"/page/%s/\">&lt</a>", pg.LeftPage.Number)
for _, page := range pg.PageList {
	if page.IsActive {
		fmt.Print(page.Number)
	} else {
		fmt.Printf("<a href=\"/page/%s/\">&lt</a>", pg.LeftPage.Number)
	}
}
fmt.Printf("<a href=\"/page/%s/\">&gt</a>", pg.RightPage.Number)

Example 2 - customize page stringer method

pg, err := simpagin.New(
	10,  // Active page which items we displaying now
	120, // Total count of items
	8,   // We show only 8 items in each page
	10,  // And our paginator rendered as 10 pages list
)
if err != nil {
	log.Fatal(err)
}
pg.SetRenderer(func (p Page) string {
	switch p.Type {
	case simpagin.LEFT:
		if p.Number == 0 {
			return "<li class=\"disabled\"><span>&laquo;</span></li>"
		} else {
			return fmt.Sprintf("<li><a href=\"?p=%d\">&laquo;</a></li>", p.Number)
		}
	case simpagin.MIDDLE:
		if p.IsActive {
			return fmt.Sprintf("<li class=\"active\"><span>%d</span></li>", p.Number)
		} else {
			return fmt.Sprintf("<li><a href=\"?p=%d\">%d</a></li>", p.Number, p.Number)
		}
	case simpagin.RIGHT:
		if p.Number == 0 {
			return "<li class=\"disabled\"><span>&raquo;</span></li>"
		} else {
			return fmt.Sprintf("<li><a href=\"?p=%d\">&raquo;</a></li>", p.Number, p.Number)
		}
	}
	return ""
})
// Now each page element know how to render himself
fmt.Print(pg.LeftPage)
for _, page := range pg.PageList {
	fmt.Print(page)
}
fmt.Print(pg.RightPage)

simpagin's People

Contributors

2no avatar gigovich avatar

Watchers

 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.