Implementing Pagination and Filtering of HubDB Table

Hi!

I am trying to implement a HubDB row filter using and a pagination of the the same HubDB table.

I have tried using the select option to filter the rows based on specific properties of the row, but it doesn’t seem to work with my pagination code. I am hoping someone can point me in the right direction on how to combine the filteration and pagination together.

Below is my pagination code:

{% set batch_num = 2 %}

{% if not request.query_dict.page_num %}
{% set page_num = 1 %}
{% set offset_num = 0 %}
{% elif request.query_dict.page_num %}
{% set page_num = request.query_dict.page_num %}
{% set offset_num = page_num|add(-1) * batch_num %}
{% endif %}

{% set query = “limit=” ~ batch_num ~ “&offset=” ~ offset_num %}

{% set rows = hubdb_table_rows(‘XXXXXXX’, query) %}

<ul class=“item-list”>
{% for row in rows %}
<li class=“list-item”>{{ row[“type”].name }}</li>
{% endfor %}
</ul>

{% set nav = hubdb_table_rows(‘XXXXXXX’) %}

{% set total_pages = nav|length|divide(batch_num) %}

{% set more_pages = total_pages - page_num %}

<nav>
{% if page_num > 1 %}
<a class=“previous-posts-link” href=“{{ content.absolute_url }}?page_num={{ page_num|add(-1) }}” title=“Next”>Next</a>
{% endif %}

{% if more_pages == 0 %}
{% if page_num|add(-4) >= 1 -%} <a href=“{{ content.absolute_url }}?page_num={{ page_num|add(-4) }}”>{{ page_num|add(-4) }}</a>{%- endif %}
{% endif %}

{% if more_pages <= 1 %}
{% if page_num|add(-3) >= 1 %}<a href=“{{ content.absolute_url }}?page_num={{ page_num|add(-3) }}”>{{ page_num|add(-3) }}</a>{% endif %}
{% endif %}

{% if page_num|add(-2) >= 1 %} <a href=“{{ content.absolute_url }}?page_num={{ page_num|add(-2) }}”>{{ page_num|add(-2) }}</a>{% endif %}

{% if page_num|add(-1) >= 1 %}<a href=“{{ content.absolute_url }}?page_num={{ page_num|add(-1) }}”>{{ page_num|add(-1) }}</a>{% endif %}

<a class=“active” href=“{{ content.absolute_url }}?page_num={{ page_num }}”>{{ page_num }}</a>

{% if page_num|add(1) <= total_pages %}<a href=“{{ content.absolute_url }}?page_num={{ page_num|add(1) }}”>{{ page_num|add(1) }}</a>{% endif %}

{% if page_num|add(2) <= total_pages %}<a href=“{{ content.absolute_url }}?page_num={{ page_num|add(2) }}”>{{ page_num|add(2) }}</a>{% endif %}

{% if page_num <= 2 %}
{% if page_num|add(3) <= total_pages %}<a href=“{{ content.absolute_url }}?page_num={{ page_num|add(3) }}”>{{ page_num|add(3) }}</a>{% endif %}
{% endif %}

{% if page_num == 1 %}
{% if page_num|add(4) <= total_pages %}<a href=“{{ content.absolute_url }}?page_num={{ page_num|add(4) }}”>{{ page_num|add(4) }}</a>{% endif %}
{% endif %}

{% if total_pages > page_num %}
<a class=“next-posts-link” href=“{{ content.absolute_url }}?page_num={{ page_num|add(1) }}” title=“Older Posts”>Previous</a>
{% endif %}
</nav>

It doesn’t look like you’ve added the filtering to your query. For example if you’re filtering by column “category”, you could add your filter like this (this is a hardcoded filter):

{% set query = "limit=" ~ batch_num ~ "&offset=" ~ offset_num ~ "&category__in=Boots,Dresses,Shirts" %}
{% set rows = hubdb_table_rows('XXXXXXX', query) %}

A more flexible option and if you’re setting the option from the Select menu to be filtered by using URL parameters of path, e.g. mywebsite.com, you can do the below. Switching to using category ids instead of names and “category__contains” as it’s more useful if it’s multiple select i.e. you have one row with multiple categories

{% set queryparam = "" %}
{% if request.query_dict.category in ["1", "2", "3"] %}
 {% set queryparam = queryparam ~ "&category__contains="~request.query_dict.category|urlencode %}
{% endif %}
{% set query = "limit=" ~ batch_num ~ "&offset=" ~ offset_num ~ queryparam %}
{% set rows = hubdb_table_rows('XXXXXXX', query) %}

Thank you for the response! Yes I have attempted the filtering using URL, but it appears I get something wrong within my code. The pagination works, just not the filtering. Hopefully you can point me to my mistake. Below is the filtering query and pagination code combined.

<div>
<form id="form_id" method="get">

<div>
<h4>Filter Products</h4>
<select name="type" form="form_id" onChange="this.form.submit()">
<option value="show-all">Show All</option>
{% set queryparam = "" %}
{% if request.query_dict.type in ["1", "2", "3", "4"] %}
{% set queryparam = queryparam ~ "&type="~request.query_dict.type|urlencode %}
{% endif %}
{% if request.query_dict.type == "show-all" %}
{% set queryparam = queryparam %}
{% endif %} 

{% set types = hubdb_table_column(XXXXXXX, "type").options %}
{% for choice in types %}
{% set type_list = type_list~choice.id|list%}
{% if choice.id == request.query_dict.type%}
<option selected="selected" value="{{ choice.id }}">{{ choice.name }}</option>
{% else %}
<option value="{{ choice.id }}">{{ choice.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>

</form>
</div> 

{% set batch_num = 4 %}

{% if not request.query_dict.page_num %}
{% set page_num = 1 %}
{% set offset_num = 0 %}
{% elif request.query_dict.page_num %}
{% set page_num = request.query_dict.page_num %}
{% set offset_num = page_num|add(-1) * batch_num %}
{% endif %}

{% set query = "limit=" ~ batch_num ~ "&offset=" ~ offset_num %}

{% set rows = hubdb_table_rows('XXXXXXX', query) %}

<ul class="item-list">
{% for row in rows %}
<li class="list-item">{{ row.name }}</li>
{% endfor %}
</ul>

{% set nav = hubdb_table_rows('XXXXXXX') %}

{% set total_pages = nav|length|divide(batch_num) %}

{% set more_pages = total_pages - page_num %}

<nav>
{% if page_num > 1 %}
<a class="previous-posts-link" href="{{ content.absolute_url }}?page_num={{ page_num|add(-1) }}" title="Next">Next</a>
{% endif %}

{% if more_pages == 0 %}
{% if page_num|add(-4) >= 1 -%} <a href="{{ content.absolute_url }}?page_num={{ page_num|add(-4) }}">{{ page_num|add(-4) }}</a>{%- endif %}
{% endif %}

{% if more_pages <= 1 %}
{% if page_num|add(-3) >= 1 %}<a href="{{ content.absolute_url }}?page_num={{ page_num|add(-3) }}">{{ page_num|add(-3) }}</a>{% endif %}
{% endif %}

{% if page_num|add(-2) >= 1 %} <a href="{{ content.absolute_url }}?page_num={{ page_num|add(-2) }}">{{ page_num|add(-2) }}</a>{% endif %}

{% if page_num|add(-1) >= 1 %}<a href="{{ content.absolute_url }}?page_num={{ page_num|add(-1) }}">{{ page_num|add(-1) }}</a>{% endif %}

<a class="active" href="{{ content.absolute_url }}?page_num={{ page_num }}">{{ page_num }}</a>

{% if page_num|add(1) <= total_pages %}<a href="{{ content.absolute_url }}?page_num={{ page_num|add(1) }}">{{ page_num|add(1) }}</a>{% endif %}

{% if page_num|add(2) <= total_pages %}<a href="{{ content.absolute_url }}?page_num={{ page_num|add(2) }}">{{ page_num|add(2) }}</a>{% endif %}

{% if page_num <= 2 %}
{% if page_num|add(3) <= total_pages %}<a href="{{ content.absolute_url }}?page_num={{ page_num|add(3) }}">{{ page_num|add(3) }}</a>{% endif %}
{% endif %}

{% if page_num == 1 %}
{% if page_num|add(4) <= total_pages %}<a href="{{ content.absolute_url }}?page_num={{ page_num|add(4) }}">{{ page_num|add(4) }}</a>{% endif %}
{% endif %}

{% if total_pages > page_num %}
<a class="next-posts-link" href="{{ content.absolute_url }}?page_num={{ page_num|add(1) }}" title="Older Posts">Previous</a>
{% endif %}
</nav>

You’vet set the queryparam but you haven’t used it, so it’s not adding the filter to your hubdb_table_rows function.

Change

{% set query = "limit=" ~ batch_num ~ "&offset=" ~ offset_num %}

to

{% set query = "limit=" ~ batch_num ~ "&offset=" ~ offset_num ~ queryparam %}

Thank you!

Hi @piersg this method doesn’t seem to be paginating right when a filter is applied - I have tried setting:

{% set nav = hubdb_table_rows('XXXXXXX', queryparam) %}

but as soon as you go to a second page of the filtered data, it resets and adds all the page numbers as if its the full table contents that is being displayed. Can you help?

It’s happening the same thing to me, @piersg - any thoughts?