add paginator
parent
0e7247d483
commit
25c582735f
|
|
@ -0,0 +1,116 @@
|
||||||
|
package paginator
|
||||||
|
|
||||||
|
// Paginator
|
||||||
|
type Paginator struct {
|
||||||
|
page int
|
||||||
|
limit int
|
||||||
|
}
|
||||||
|
|
||||||
|
// New
|
||||||
|
func New(page, limit int) *Paginator {
|
||||||
|
return &Paginator{page: page, limit: limit}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Offset
|
||||||
|
func (p *Paginator) Offset() int {
|
||||||
|
if p.page < 1 {
|
||||||
|
p.page = 1
|
||||||
|
}
|
||||||
|
offset := p.limit * (p.page - 1)
|
||||||
|
return int(offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limit
|
||||||
|
func (p *Paginator) Limit() int {
|
||||||
|
return p.limit
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pagination
|
||||||
|
func (p *Paginator) Pagination(totalCount int) *Pagination {
|
||||||
|
pg := Pagination{}
|
||||||
|
|
||||||
|
total := (totalCount / p.limit)
|
||||||
|
|
||||||
|
// Calculator Total Page
|
||||||
|
remainder := totalCount % p.limit
|
||||||
|
if remainder == 0 {
|
||||||
|
pg.totalPage = total
|
||||||
|
} else {
|
||||||
|
pg.totalPage = total + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set current/record per page meta data
|
||||||
|
pg.currentPage = p.page
|
||||||
|
pg.recordPerPage = p.limit
|
||||||
|
|
||||||
|
// Calculator the Next/Previous Page
|
||||||
|
if p.page <= 0 {
|
||||||
|
pg.next = p.page + 1
|
||||||
|
} else if p.page < pg.totalPage {
|
||||||
|
pg.previous = p.page - 1
|
||||||
|
pg.next = p.page + 1
|
||||||
|
} else if p.page == pg.totalPage {
|
||||||
|
pg.previous = p.page - 1
|
||||||
|
pg.next = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return &pg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pagination Object
|
||||||
|
type Pagination struct {
|
||||||
|
next int
|
||||||
|
previous int
|
||||||
|
recordPerPage int
|
||||||
|
currentPage int
|
||||||
|
totalPage int
|
||||||
|
}
|
||||||
|
|
||||||
|
// PageRange 迭代页码
|
||||||
|
func (p *Pagination) PageRange() []int {
|
||||||
|
pages := []int{}
|
||||||
|
for i := 1; i <= p.totalPage; i++ {
|
||||||
|
pages = append(pages, i)
|
||||||
|
}
|
||||||
|
return pages
|
||||||
|
}
|
||||||
|
|
||||||
|
// NumPages 页码数量
|
||||||
|
func (p *Pagination) NumPages() int {
|
||||||
|
return p.totalPage
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasPrevious 是否有上一页
|
||||||
|
func (p *Pagination) HasPrevious() bool {
|
||||||
|
return p.previous > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// PreviousPageNumber 上一页的页码
|
||||||
|
func (p *Pagination) PreviousPageNumber() int {
|
||||||
|
return p.previous
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasNext 是否有下一页
|
||||||
|
func (p *Pagination) HasNext() bool {
|
||||||
|
return p.next > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextPageNumber 下一页的页码
|
||||||
|
func (p *Pagination) NextPageNumber() int {
|
||||||
|
return p.next
|
||||||
|
}
|
||||||
|
|
||||||
|
// Number 当前页码
|
||||||
|
func (p *Pagination) Number() int {
|
||||||
|
return p.currentPage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Number 当前页码
|
||||||
|
func (p *Pagination) IsFirstPage() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Number 当前页码
|
||||||
|
func (p *Pagination) IsLastPage() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue