Let us built a case in which we want to create a dropdown lit of all shareholders we hve. When selected one, we want to display its relevant info from that selected shareholder:
Beneath is how we can approach such case:
{% comment %}all info regarding shareholders are in the drop period.shareholders{% endcomment %}
{% assign shareholders = period.shareholders %}
{% comment %}create variable of all the names of each shareholder, splitted by a "|". This will be used later on as the options for our dropdown. Linked to that, we use the unique "ID" each person has for the option values too{% endcomment %}
{% assign names = "" %}
{% assign values = "" %}
{% for person in shareholders %}
{% comment %}it is the db var "represented_by_name" that decides if a person is legal or not - needed for default dropdown LEGAL OR NOT{% endcomment %}
{% assign default_type = "" %}
{% if person.custom.represented_by_name != blank %}
{% assign default_type = "legal" %}
{% else %}
{% assign default_type = "nature" %}
{% endif %}
{% comment %}look at the dropdown to check whether someone is LEGAL or not{% endcomment %}
{% assign type = person.custom.type | default:default_type %}
{% comment %}create the needed arrays for select and option_values later on{% endcomment %}
{% if type == "legal" %}
{% assign names = names | append:person.name %} {% comment %}name of legal person is stored in db var .name{% endcomment %}
{% assign values = values | append:person.id %} {% comment %}unique for every person{% endcomment %}
{% else %}
{% assign names = names | append:person.custom.first_name | append:" " | append:person.custom.last_name %} {% comment %}name of non-legal person is stored in db var custom.first_name & custom.last_name{% endcomment %}
{% assign values = values | append:person.id %} {% comment %}unique for every person{% endcomment %}
{% endif %}
{% unless forloop.last %} {% comment %}each value in the array needs to be split on "|" except for the last element (= loop) in the array{% endcomment %}
{% assign names = names | append:"|" %}
{% assign values = values | append:"|" %}
{% endunless %}
{% endfor %}
{% comment %}create info text to chose the shareholder{% endcomment %}
{% ic %}{::infotext}
Choose shareholder: {% input custom.choosing.shareholders as:select options:names option_values:values %}
{:/infotext}{% endic %}
{% comment %}
the select creates a dropdown, the options visualises the elements of the dropdown and the option_values is the actual value of each chosen element ==> the ID of a person!
This means that the value of custom.choosing.shareholders is the ID and not the name!
{% endcomment %}
* * *
{% comment %}let us loop over the complete period. shareholders drop and display the person that has the same ID as the chosen person from the dropdown, and display the info like name, VAT nbr, ...{% endcomment %}
{% stripnewlines %}
<table class="usr-width-100{% nic %} usr-bordered{% endnic %}">
<thead>
<tr>
<th class="usr-width-25 usr-line-bottom"><b>Name shareholder</b></th>
<th class="usr-width-25 usr-line-bottom"><b>VAT nbr</b></th>
<th class="usr-line-bottom"><b>Amount of shares</b></th>
</tr>
</thead>
<tbody>
{% for person in period.shareholders %}
{% if person.id == custom.choosing.shareholders %}
{% comment %}it is the db var "represented_by_name" that decides if a person is legal or not - needed for default dropdown LEGAL OR NOT{% endcomment %}
{% assign default_type = "" %}
{% if person.custom.represented_by_name != blank %}
{% assign default_type = "legal" %}
{% else %}
{% assign default_type = "nature" %}
{% endif %}
{% comment %}look at the dropdown to check whether someone is LEGAL or not{% endcomment %}
{% assign type = person.custom.type | default:default_type %}
<tr>
<td>
{% if type == "legal" %}
{{ person.name }}
{% else %}
{{ person.custom.first_name }} {{ person.custom.last_name }}
{% endif %}
</td>
<td>{{ person.custom.vat_identifier }}</td>
<td><b>{{ person.amount_of_shares | integer }}</b></td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
{% endstripnewlines %}
This case has been updated on 03/10/2022 to include HTML tables.