Skip to content

Latest commit

 

History

History
145 lines (111 loc) · 6.22 KB

pagination.md

File metadata and controls

145 lines (111 loc) · 6.22 KB

Pagination

Introduction

In other frameworks, pagination can be very painful. The Hunt framework's paginator is integrated with the provides convenient, easy-to-use pagination of database results out of the box. The HTML generated by the paginator is compatible with the Bootstrap CSS framework.

Basic Usage

Paginating Query Builder Results

In this example, the only argument passed to the paginate method is the number of items you would like displayed "per page". In this case, let's specify that we would like to display 15 items per page:

module app.component.system.repository.UserRepository;

import app.component.system.model.User;
import hunt.entity;
import hunt.entity.repository;
import hunt.framework.Simplify;

class UserRepository : EntityRepository!(User, int) {

    this() {
        super(defaultEntityManager());
    }

    Page!User findByUser(int page = 1, int perPage = 10) {
        page = page < 1 ? 1 : page;
        perPage = perPage < 1 ? 10 : perPage;
        return _manager.createQuery!(User)("SELECT u FROM User u", new Pageable(page - 1, perPage))
            .getPageResult();
    }
}

Simple Pagination

If you only need to display simple "Next" and "Previous" links in your pagination view, you may this like belown. This is very useful for large datasets when you do not need to display a link for each page number when rendering your view:

    @Action 
    Response list(int perPage, int page = 1) {
        perPage = perPage < 1 ? 10 : perPage;
        page = page < 1 ? 1 : page;
        Page!User alldata = (new UserRepository()).findByUser(page, perPage);
        view.assign("users", alldata.getContent());

        view.assign("pageModel",  alldata.getModel());
        view.assign("pageQuery", buildQueryString(request.input()));

        return new Response(request)
        .setContent(view.render("system/user/list"));
    }

Paginating Entity Results

In this example, we will paginate the User model with 15 items per page. As you can see, the syntax is nearly identical to paginating query builder results:

    Page!User users = (new UserRepository()).findByUser(1, 15);

Manually Creating A Paginator

Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so by creating either an hunt.entity.domain.Page instance, depending on your needs.

Displaying Pagination Results

When calling the paginate method, you will receive an instance of hunt.entity.domain.Page. When calling the simplePaginate method, you will receive an instance of hunt.entity.domain.Page. These objects provide several methods that describe the result set. In addition to these helpers methods, the paginator instances are iterators and may be looped as an array. So, once you have retrieved the results, you may display the results and render the page:

{% if pageModel.totalPages > 1 %}
<nav aria-label="Page navigation example">
    <ul class="pagination justify-content-end">
        <li class="page-item popover-body">current {{ pageModel.current }}  (total {{ pageModel.totalPages }} page)</li>
        <li class="page-item{% if pageModel.current < 2 %} disabled{% endif %}">
            <a class="page-link" href="?page={{pageModel.previous}}&{{pageQuery}}" tabindex="-1">{{ trans("PAGE_SYSTEM_PREVIOUS") }}</a>
        </li>
        {% for p in pageModel.pages %}
        {% if p == 0 %}
        <li class="page-item disabled">
            <a class="page-link" tabindex="...">...</a>
        </li>
        {% else %}
        <li class="page-item{% if pageModel.current == p %} active{% endif %}">
            <a class="page-link" href="?page={{p}}&{{pageQuery}}">{{ p }}</a>
        </li>
        {% endif %}
        {% endfor %}
        <li class="page-item{% if pageModel.totalPages == pageModel.current %} disabled{% endif %}">
            <a class="page-link" href="?page={{pageModel.next}}&{{pageQuery}}">{{ trans("PAGE_SYSTEM_NEXT") }}</a>
        </li>
    </ul>
</nav>
{% endif %}

The pageModel.pages will render the links to the rest of the pages in the result set. Each of these links will already contain the proper page query string variable. Remember, the HTML generated by the pageModel.pages method is compatible with the Bootstrap CSS framework.

Appending To Pagination Links

You may append to the query string of pagination links using the appends method. For example, to append sort=votes to each pagination link, you should make the following call to appends:

    <li class="page-item{% if pageModel.current == p %} active{% endif %}">
            <a class="page-link" href="?page={{p}}&{{pageQuery}}">{{ p }}</a>
    </li>

Each paginator instance provides additional pagination information via the following methods:

Method Description
result.getNumber() Get the number of items for the current page.
result.getSize() Get the size of items for the current page.
result.getTotalPages() Get total pages number.
result.getTotalElements() Determine the total number of matching items in the data store..
result.hasPreviousPage() Get the result is previous page exist.
result.isFirstPage() Get the result is the first page of current page.
result.hasNextPage() Get the result is nest page exist.
result.isLastPage() Determine if the paginator is on the first page.
result.getContent() get page content.
result.hasContent() has content.
result.getModel() get page result model.