I am using HTML templating in Go to render a pagination widget. I am trying to follow an example of how to do it from here: https://www.solodev.com/blog/web-design/adding-pagination-to-your-website.stml
This is my code so far:
// Item size like call 2 item from database
var pagesize = 2
var PaginationSize int = 6
Pagination := make([]int, PaginationSize)
for i := 0; i < PaginationSize; i++ {
if RequestPageNumber <= page {
Pagination[i] = i + 1
} else {
Pagination[i] = RequestPageNumber + i
}
// Make it active
if i == 0 {
Info.Pagination += template.HTML(fmt.Sprintf(`<li class="page-item active"><a class="page-link" href=?id=%v>%v</a></li>`,RequestPageNumber + i ,RequestPageNumber + i))
} else {
Info.Pagination += template.HTML(fmt.Sprintf(`<li class="page-item"><a class="page-link" href=?id=%v>%v</a></li>`,RequestPageNumber + i ,RequestPageNumber + i))
}
}
I would like to improve this by making the pagination list update only when the active page becomes a multiple of PaginationSize
. For example, when the displayed pagination shows
(1), 2, 3, 4, 5, 6
If the user clicks on 2, I would like to see
1, (2), 3, 4, 5, 6
But my current code displays
(2), 3, 4, 5, 6, 7
After the page has advanced past 6, the page list should look like
7, 8, 9, 10, 11, 12 ,13 ,14
The data wrapped in this pagination view may be in a database, so I'd also appreciate tips on how to make this work with a database, if possible.
Thanks.