add pagination
This commit is contained in:
35
src/internal/model/pagination.go
Normal file
35
src/internal/model/pagination.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type PageButton struct {
|
||||
Content string
|
||||
Active bool
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
type PaginationViewModel []PageButton
|
||||
|
||||
func NewPaginationViewModel(currentPage uint, totalPages uint) *PaginationViewModel {
|
||||
pagVM := make(PaginationViewModel, 0)
|
||||
|
||||
if totalPages > 1 {
|
||||
pagVM = append(pagVM, PageButton{"1", currentPage == 1, false})
|
||||
}
|
||||
if currentPage > 3 {
|
||||
pagVM = append(pagVM, PageButton{"...", false, true})
|
||||
}
|
||||
for i := max(2, currentPage-1); i <= min(totalPages-1, currentPage+1); i++ {
|
||||
pagVM = append(pagVM, PageButton{strconv.Itoa(int(i)), i == currentPage, false})
|
||||
}
|
||||
if currentPage < totalPages-2 {
|
||||
pagVM = append(pagVM, PageButton{"...", false, true})
|
||||
}
|
||||
if totalPages > 1 {
|
||||
pagVM = append(pagVM, PageButton{strconv.Itoa(int(totalPages)), totalPages == currentPage, false})
|
||||
}
|
||||
|
||||
return &pagVM
|
||||
}
|
||||
Reference in New Issue
Block a user