Skip to content

Commit

Permalink
refactor: move column selector from form to table
Browse files Browse the repository at this point in the history
This commit changes the way the `column_selector` is set up. Up until
now, it was a simple select input, that could only be used in
conjunction with an existing form. That meant, that the colums selector
had to be places at a position away from the actual table.
Now the column selector is its own form, therefore it can be places
whereever we want it. Also it is not a templatetag anymore, but a simple
partial template that has to be included with {% include ... %}.
The downside is, that we might now have multiple forms sending get
parameters - this is where the new "form_get_hidden.html" template comes
in. It adds existing get parameters to hidden fields in the form, that
way multiple forms don't get in each others way.

Closes: #173
  • Loading branch information
b1rger committed Oct 19, 2023
1 parent 025186f commit 317d7ed
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.

This file was deleted.

24 changes: 15 additions & 9 deletions apis_core/apis_metainfo/templates/generic_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,8 @@ <h3 style="margin-bottom:15px;">

<!--Search mask-->
{% load django_tables2 crispy_forms_tags %}
<form action="." class="uniForm" method="get">

{% if togglable_colums %}
<legend>Select additional columns</legend>
{% column_selector %}
{% endif %}

<br />
<form class="uniForm">
{% include "partials/form_get_hidden.html" with request=request exclude_fields=filter.form.fields %}
{% crispy filter.form filter.form.helper %}
</form>

Expand Down Expand Up @@ -118,14 +112,26 @@ <h1>{{ data.title }}</h1>
{% endif %}

{% endblock %}

</div>
</div>
</div>
<div class="col-md-8" id="results">
<div class="card">
<div class="card-body">
{% with table.paginator.count as total %}
<legend>Result: {{ total }} items</legend>
<div class="row">
<div class="col">
<h3>Result: {{ total }} items</h3>
</div>
<div class="col">

{% if togglable_colums %}
{% include "partials/column_selector.html" with togglable_columns=togglable_colums request=request %}
{% endif %}

</div>
</div>

{% if conf_items %}
<div class="dropdown">
Expand Down
9 changes: 0 additions & 9 deletions apis_core/apis_metainfo/templatetags/apis_metainfo_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,3 @@ def class_definition(context):
print(e)
pass
return values


@register.inclusion_tag("apis_metainfo/tags/column_selector.html", takes_context=True)
def column_selector(context):
try:
return {"columns": context["togglable_colums"]}
except Exception as e:
print(e)
return {"columns": None}
20 changes: 20 additions & 0 deletions apis_core/core/templates/partials/column_selector.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% if togglable_columns %}
<form class="form-inline float-right">
<select class="selectpicker"
multiple
data-selected-text-format="count > 2"
name="columns"
id="column_selector">
{% for col in togglable_columns %}<option value="{{ col }}">{{ col|title }}</option>{% endfor %}
</select>
{% include "partials/form_get_hidden.html" with request=request exclude_fields="columns" %}
<button type="submit" class="form-control">Show</button>
<script>
$('#column_selector').on('loaded.bs.select', function (e) {
var url = new URL(window.location.href);
var columns = url.searchParams.getAll("columns");
$('#column_selector').selectpicker('val', columns);
});
</script>
</form>
{% endif %}
11 changes: 11 additions & 0 deletions apis_core/core/templates/partials/form_get_hidden.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{# this template is meant to be used in forms #}
{# it add exisiting GET parameters to the form as hidden input fields #}
{# it requires the request as argument `request` as well as an `exclude_fields` list #}
{# this should be the list of fields of the form itself #}
{% for field, values in request.GET.lists %}

{% if field not in exclude_fields %}
{% for value in values %}<input type="hidden" name="{{ field }}" value="{{ value }}" />{% endfor %}
{% endif %}

{% endfor %}

0 comments on commit 317d7ed

Please sign in to comment.