Move pagination to helper function and implement filters+search in contacts
This commit is contained in:
parent
581d11c98d
commit
b3c309743b
5 changed files with 470 additions and 61 deletions
68
helpers/pagination.go
Normal file
68
helpers/pagination.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package helpers
|
||||
|
||||
type Pagination struct {
|
||||
PageSize int
|
||||
MaxPages int
|
||||
CurrentPage int
|
||||
Offset int
|
||||
Count int
|
||||
Pages []int
|
||||
}
|
||||
|
||||
func Paginate(size int, count int, page int) Pagination {
|
||||
if size < 1 {
|
||||
size = 1
|
||||
}
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
pagination := Pagination{
|
||||
PageSize: size,
|
||||
CurrentPage: page,
|
||||
Count: count,
|
||||
MaxPages: 1,
|
||||
}
|
||||
|
||||
if pagination.Count > pagination.PageSize {
|
||||
pagination.MaxPages = pagination.Count / pagination.PageSize
|
||||
}
|
||||
|
||||
if pagination.CurrentPage > pagination.MaxPages {
|
||||
pagination.CurrentPage = 1
|
||||
}
|
||||
|
||||
pagination.Offset = (pagination.CurrentPage - 1) / pagination.PageSize
|
||||
|
||||
for i := 1; i <= pagination.MaxPages; i++ {
|
||||
|
||||
if i == pagination.CurrentPage {
|
||||
pagination.Pages = append(pagination.Pages, i)
|
||||
continue
|
||||
}
|
||||
|
||||
if i >= (pagination.CurrentPage-2) && i < pagination.CurrentPage {
|
||||
pagination.Pages = append(pagination.Pages, i)
|
||||
continue
|
||||
}
|
||||
|
||||
if i <= (pagination.CurrentPage+2) && i > pagination.CurrentPage {
|
||||
pagination.Pages = append(pagination.Pages, i)
|
||||
continue
|
||||
}
|
||||
|
||||
if pagination.CurrentPage <= 2 && i <= 5 {
|
||||
pagination.Pages = append(pagination.Pages, i)
|
||||
continue
|
||||
}
|
||||
|
||||
if pagination.CurrentPage >= (pagination.MaxPages-2) && i >= (pagination.MaxPages-5) {
|
||||
pagination.Pages = append(pagination.Pages, i)
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return pagination
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue