Percent of total

Hello,

how is it possible to do the following:

col1: input col2: %

{% input %} % of total
{% input %} % of total
{% input %} % of total
{% input %} % of total
{% input %} % of total

total

Thank you,
David

Hi @David_Clesse,

There are probably different ways to calculate this. Here is one example to get the desired result:

I’m assuming you will use a fori-loop in your table, as such:

{% stripnewlines %}
|—|---+{% fori item in custom.items %}{% newline %}
|{% input item.suffix %} |{{ % of total }}
{% endfori %}{% newline %}
{% endstripnewlines %}

As the variable total is a local variable, it is important to calculate this total amount above your table (as local variables cannot be used before they are created - Liquid is always read top down). So in order to calculate the total, you should loop over the same collection above the table in your code:

{% assign total = 0 %}
{% for item in custom.items %}
{% assign total = total+item.suffix %}
{% endfor %}

The output % of total in your table can than be replaced by {{ item.suffix/total | percentage }}, i.e.:

|—|---+{% fori item in custom.items %}{% newline %}
|{% input item.suffix %} |{{ item.suffix/total | percentage }}
{% endfori %}{% newline %}
{% endstripnewlines %}

Hope this was what you were looking for?

Good luck finishing your table! If you have any more question, do not hesitate to reach out.

Kind regards,
Robin

Hello Robin,
first, thank you for you kind answer.
Actually I’m using a ‘hardcoded’ list of 5 custom input rows, without a loop.
Is it still possible?
Thank you,
David.

Hi @David_Clesse

Somthing like this should do the trick:

{% assign total = custom.item1.value+custom.item2.value+custom.item3.value+custom.item4.value+custom.item5.value %}

{% stripnewlines %}
| Value | Percentage of total {% newline %}
|-----:|----: {% newline %}
| {% input custom.item1.value as:currency %} | {{ (custom.item1.value/total) | percentage }} {% newline %}
| {% input custom.item2.value as:currency %} | {{ (custom.item2.value/total) | percentage }} {% newline %}
| {% input custom.item3.value as:currency %} | {{ (custom.item3.value/total) | percentage }} {% newline %}
| {% input custom.item4.value as:currency %} | {{ (custom.item4.value/total) | percentage }} {% newline %}
| {% input custom.item5.value as:currency %} | {{ (custom.item5.value/total) | percentage }} {% newline %}
|^**{{ total | currency }}**^|^**{{ 1 | percentage }}**^
{% endstripnewlines %}

In the tabel you can see 5 different inputs. You should give them an appropriate name. As @robindeclercq already said, the total has to be calculated at the top. So that’s what I am doing as well. In the right colomn, the individual values are divided by the total.

Kind regards
Sam

Thank you Sam, that’s exactly what I need :slight_smile: