Golang : Pagination with go-paginator configuration example
Here is an example that you can use as reference on how to configure go-paginator properly. The snippets below are from my own project and it took me a while to get it right.
In the main.go
:
import "github.com/go-chi/chi"
...
mx := chi.NewRouter()
...
mx.HandleFunc("/listproperty", controllers.ListProperty) // to handle first page
mx.HandleFunc("/listproperty/{pageNumber}", controllers.ListProperty) // and subsequent pages(pagination)
In the ListProperty.go
controller:
// ListProperty to populate home page
func ListProperty(w http.ResponseWriter, r *http.Request) {
...
currentPageNumber := chi.URLParam(r, "pageNumber")
currentPageNumberInt, _ := strconv.Atoi(currentPageNumber) // convert to int
allRows := conn.Model(properties) <--- replace with your own function that returns all the related rows from database
propertiesPerPage := 3 <---- change this number to your own
paginated := paginator.New(adapter.NewGORMAdapter(allRows), propertiesPerPage)
paginated.SetPage(currentPageNumberInt)
if err = paginate.Results(&properties); err != nil {
panic(err)
}
templateDataMap["propertiesList"] = *properties
paginateView := view.New(&paginate)
templateDataMap["paginatePages"] = paginateView.Pages()
templateDataMap["paginateNext"] = paginateView.Next()
templateDataMap["paginatePrev"] = paginateView.Prev()
templateDataMap["paginateLast"] = paginateView.Last()
templateDataMap["paginateCurrent"] = paginateView.Current()
templateDataMap["paginateHasNext"] = paginate.HasNext()
templateDataMap["paginateHasPrev"] = paginate.HasPrev()
templateDataMap["paginateHasPages"] = paginate.HasPages()
...
// populate the view(template) with data
var listPropertyPageBody = template.Must(template.New("listPropertyPageBody").Parse(body.ListPropertyHTML))
if err := listPropertyPageBody.ExecuteTemplate(w, "listPropertyPageBody", templateDataMap); err != nil {
log.Println(err)
}
and finally in the HTML template:
{{if .paginateHasPages}}
<ul class="pagination">
{{if .paginateHasPrev}}
<li class="page-item"><a class="page-link" href="/listproperty">First</a></li>
{{end}}
{{if .paginateHasPrev}}
<li class="page-item"><a class="page-link" href="/listproperty/{{.paginatePrev}}">Previous</a></li>
{{end}}
{{range .paginatePages}}
{{if (ge $.paginateLast .)}}
<li class="page-item {{if (eq $.paginateCurrent .)}}
{{print "active"}}
{{else}}
{{print ""}}
{{end}}">
<a class="page-link" href="/listproperty/{{.}}">{{.}}</a>
</li>
{{end}}
{{end}}
{{if .paginateHasNext}}
<li class="page-item"><a class="page-link" href="/listproperty/{{.paginateNext}}">Next</a></li>
{{end}}
{{if (gt $.paginateLast $.paginateCurrent)}}
<li class="page-item"><a class="page-link" href="/listproperty/{{.paginateLast}}">Last</a></li>
{{end}}
</ul>
{{end}}
Hope this helps!
References:
https://socketloop.com/tutorials/golang-how-to-use-if-eq-and-print-properly-in-html-template
See also : Golang : How to use if, eq and print properly in html template
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.1k Golang : Of hash table and hash map
+10k Golang : Random Rune generator
+6.6k Golang : Find the longest line of text example
+21.8k Fix "Failed to start php5-fpm.service: Unit php5-fpm.service is masked."
+11.8k Golang : Detect user location with HTML5 geo-location
+10.9k Golang : Fix - does not implement sort.Interface (missing Len method)
+13k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+26.1k Golang : Calculate future date with time.Add() function
+47.7k Golang : How to convert JSON string to map and slice
+9.9k Golang : Edge detection with Sobel method
+25.6k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+5.8k Fontello : How to load and use fonts?