Working with beego pagination


A beego recipe

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

    "github.com/astaxie/beego/utils/pagination"

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>
    {{else}}
        <li class="disabled"><a>First</a></li>
        <li class="disabled"><a>&laquo;</a></li>
    {{end}}
    {{range $index, $page := .paginator.Pages}}
        <li{{if $.paginator.IsActive .}} class="active"{{end}}>
            <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
        </li>
    {{end}}
    {{if .paginator.HasNext}}
        <li><a href="{{.paginator.PageLinkNext}}">&raquo;</a></li>
        <li><a href="{{.paginator.PageLinkLast}}">Last</a></li>
    {{else}}
        <li class="disabled"><a>&raquo;</a></li>
        <li class="disabled"><a>Last</a></li>
    {{end}}
</ul>
{{end}}

Post information
Posted by juanfgs 2015-01-09 23:58:41 +0000 UTC

Leave a Comment: