CMS Development

VDave
Member

Filter hubDB Data with select list by month and year with pagination

I done the pagination part but facing issues with select list with filter. I tries with choice but now How can I filter the data using the url 

 

Thanks in advance 🙂

My code is below : 


<h1>DB Filter Demo</h1>

 

{# Set your HubDB ID. There is a field (HubDB table) to make the user choose. If it's empty, it will display the default #}
{% set db = module.db || '5533038' %}

{% set queryparam = "" %}

{# Check if the query_dict of pubtype contains the existing pubtypes of the HubDB column options and append it to the queryparam #}
{% if request.query_dict.month in types|map('id') %}
{# Set new queryparam by adding the pubtype #}
{% set queryparam = queryparam ~ "&month="~request.query_dict.month|urlencode %}
{% endif %}

 

<div>
<form id="form_id" method="get">
{# Choice filter #}
<div>
<h4>Publication Type:</h4>
<select name="pubtype" form="form_id" onChange="this.form.submit()">
<option value="show-all">Show All</option>
{% set types = hubdb_table_column(db, "month").options %}
{% for choice in types %}
{% set type_list = type_list~choice.id|list%}
<option {% if choice.id == request.query_dict.pubtype %}selected="selected"{% endif %} value="{{ choice.id }}">{{ choice.name }}</option>
{% endfor %}
</select>
</div>
</form>
</div>

 

 

 

 

 

 


{% set batch_num = 5 %}

{% 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('5533038', query) %}
<ul class="item-list">
{% for row in rows %}
<li class="list-item">
<h2>{{ row.name }}</h2>
<p> {{ row.description }}</p>
<p>{{ row.date|datetimeformat('%B') }}</p>
<p>{{ row.date|datetimeformat('%Y') }}</p>
</li>

{% endfor %}
</ul>

 

 

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

{% 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">Previous</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">Next</a>
{% endif %}
</nav>

 

DB Screenshot:

Screenshot_1.png

0 Upvotes
1 Reply 1
ChehakWadhdwa
Member | Diamond Partner
Member | Diamond Partner

Filter hubDB Data with select list by month and year with pagination

Hey  @VDave 

 

Have a look at the links given below :



Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards.

0 Upvotes