Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Pass query results to paginate tag #4120

Closed
jan-dh opened this issue Apr 10, 2019 · 6 comments
Closed

FR: Pass query results to paginate tag #4120

jan-dh opened this issue Apr 10, 2019 · 6 comments

Comments

@jan-dh
Copy link

jan-dh commented Apr 10, 2019

Description

I sometimes have pages where I need a list of items, both paginated and not paginated. For example: a page with a list of shops. I would like to have

  1. paginated shops
  2. a google maps with all the shops on it

At the moment I have to do this in 2 seperate queries:

  1. paginated tag with the query
  2. another query for all the musea that I then output to a json
@brandonkelly
Copy link
Member

brandonkelly commented Apr 10, 2019

This mostly defeats the point of pagination, but we just released Craft 3.1.22 which adds a setPageResults() method to Craft’s Paginator class, so now you should be able to do this:

{% set query = craft.entries()
    .section('shops')
    .orderBy('shopName ASC') %}

{% set allShops = query.all() %}
{% set shopsPerPage = 10 %}
{% set offset = (craft.app.request.pageNum - 1) * shopsPerPage %}
{% set pageShops = allShops|slice(offset, shopsPerPage) %}

{% set paginator = create('craft\\db\\Paginator', [query, {
    pageSize: shopsPerPage,
    currentPage: craft.app.request.pageNum,
    pageResults: pageShops
}]) %}

<!-- create the map -->
{% for shop in allShops %}
    ...
{% endfor %}

<!-- page results -->
{% for shop in pageShops %}
    ...
{% endfor %}

<!-- pagination navigation -->
{% set pageInfo = create('craft\\web\\twig\\variables\\Paginate').create(paginator) %}

Page: {{ pageInfo.currentPage }}
First Page: {{ pageInfo.firstUrl }}
Last Page: {{ pageInfo.lastUrl }}
Prev Page: {{ pageInfo.prevUrl }}
Next Page: {{ pageInfo.nextUrl }}

If it were me, I’d just opt for a second query on the page, maybe within some {% cache %} tags along with all the map HTML, so it doesn’t even need to be run most of the time. But I’ll let you be the judge :)

@jan-dh
Copy link
Author

jan-dh commented Apr 10, 2019

Nice! I'll make sure to test out your solution. Thanks for the quick reply. Awesome work as always 🤗

@simeon-smith
Copy link

I'm trying to do something similar but am getting and error when I try to set up the pageInfo.

{% set pageInfo = create('craft\\web\\twig\\variables').create(paginator) %}

@brandonkelly
Copy link
Member

@simeon-smith craft\web\twig\variables is not a class name.

@simeon-smith
Copy link

@brandonkelly, I was following the code in your example. Is this no longer available?

@brandonkelly
Copy link
Member

Whoops, that should have been craft\\web\\twig\\variables\\Paginate. Just updated the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants