Ir al contenido principal

Working with beego pagination

 I wanted to take a while to explain how to use this library easily. Basically we begin adding the following import:


Paginator class is pretty straightforward, so in our code we just have to make use of the current controller Context, define how many posts per page we want and the total count of posts in the query:

    paginator := pagination.SetPaginator(this.Ctx, postsPerPage, countPosts)


In the view we can format our paginator in the following way:


{{if .paginator.HasPages}}
<ul class="pagination pagination">
    {{if .paginator.HasPrev}}
        <li><a href="{{.paginator.PageLinkFirst}}">First</a></li>
        <li><a href="{{.paginator.PageLinkPrev}}">&laquo;</a></li>
        <li class="disabled"><a>First</a></li>
        <li class="disabled"><a>&laquo;</a></li>
    {{range $index, $page := .paginator.Pages}}
        <li{{if $.paginator.IsActive .}} class="active"{{end}}>
            <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
    {{if .paginator.HasNext}}
        <li><a href="{{.paginator.PageLinkNext}}">&raquo;</a></li>
        <li><a href="{{.paginator.PageLinkLast}}">Last</a></li>
        <li class="disabled"><a>&raquo;</a></li>
        <li class="disabled"><a>Last</a></li>


Comments powered by Disqus