36 lines
848 B
Go
36 lines
848 B
Go
|
|
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
|
||
|
|
}
|