Hi,
I worked with the team on putting together a couple examples of this. Currently there isn’t anything documented on creating pagination for HubDB but hopefully these solutions will better help you create these for your events calendar.
I can think of two different ways to do this. Both of these are going to use batch filtering to create different sets of rows and this is how we can create groupings for the rows and then we can print these to the page. We can get all the rows in the table by creating a for loop like
{% for row in rows %}
but instead with batch filtering we can create something like
{% for row in row|batch(3, ‘ ’) %}
The first parameter is setting the number of rows you want to set in each “batch” which will create page 1. You can read more about the batch filter here.
The first solution would be to set a query string for the table and then use the query string in the URL to select which batch is being printed to the page and we can also set the number of items in each batch or how many events will print to the page by using the following
{% set batch_num = 2 %}.
{# create our table #}
{% set table = hubdb_table_rows(673679) %}
{# set the number of items per batch #}
{% set batch_num = 2 %}
{# set the active batch #}
{% if not Query.page_num %}
{% set page_num = 1 %}
{% elif Query.page_num %}
{% set page_num = Query.page_num %}
{% endif %}
After we have this setup we can then create the table that will start to print out our rows to the page. This will work but what will happen is when we call loop.last we have to use ‘export_to_template_context=true’ and export to the template due to how these modules are scoped in HubL and this wouldn’t live outside due to how the scope of the module. If that is too weird what we can do is use the |length filter and then use some simple math to calculate each batch number.
<table>
{% for row in table|batch(batch_num) %}
{% if loop.index == page_num %}
<tr class="batch-{{ loop.index }}">
{% for innerRow in row %}
<td>{{ innerRow.name }}</td>
{% endfor %}
</tr>
{% endif %}
{% if loop.last %}
{# if this is too weird, you could use the |length filter and then divide by the batch_num and round up #}
{% text "number_of_rows", value="{{ loop.index }}", export_to_template_context=true %}
{% endif %}
{% endfor %}
<div class="hubdb-pagination">
{% if page_num > 1 %}<div class="previous"><a href="{{ content.absolute_url }}?page_num={{ page_num|add(-1) }}">Previous</a></div>{% endif %}
{% if page_num < widget_data.number_of_rows.value %}<div class="next"><a href="{{ content.absolute_url }}?page_num={{ page_num|add(1) }}">Next</a></div>{% endif %}
</div>
</table>
The second way to do this still uses batches but this time we will print all the rows to the document and then we will set a display: none; on each of the rows and for the row that we want to show on the page we can remove that active class we added to hide the elements and this makes them appear on the page. First we can setup the table with
{# set the number of items per batch #}
{% set batch_num = 2 %}
{# set the active batch #}
{% set batch_num_active = 1 %}
Once we have this setup we can then create our table with something like the below. We would be adding in the additional class and then we could set a style in CSS to hide the table rows such as tr { display: none; } and then for the items that we want to display we can add a class of active which would change the display to block and this would display the “ next page” and then hide the “previous page”. I won’t go too much into the JS Solution as this would be straightforward JS and adding the active class and running that function on a click event to change the CSS attribute.
{% for row in table|batch(batch_num) %}
<tr class="batch-{{ loop.index }} {% if loop.index == batch_num_active %}active{%endif%}">
{% for innerRow in row %}
<td>{{ innerRow.name }}</td>
{% endfor %}
</tr>
{% if loop.last %}
{# if this is too weird, you could use the |length filter and then divide by the batch_num and round up #}
{% text "number_of_rows", value="{{ loop.index }}", export_to_template_context=true %}
{% endif %}
{% endfor %}