Count numbers in a horizontal and vertical way

Hi,

I would like to create a table that can expand in a horizontal and in a vertical way.
The perfect example is the overview of the different cars and their costs, from the template ‘Wagenpark’.
Except, instead of linking account numbers, I want to insert a random amount.
But how do I add up the numbers in a horizontal way?
At the moment, the ‘controle’ stops at 260.0€ because the code of ‘wagenpark’ stops at $6.
How do you keep counting horizontal while be able to add infinite columns?

Thank you in advance!

@Henri-Louis

Welcome to community!

I have prepared two examples of code:
Example 1: number of columns is predefined (for loop inside fori loop)
Example 2: number of columns in infinite (fori loop inside fori loop)

Please take a look and let me know if you need additional information.

{% comment %}Number of columns is defined{% endcomment %}
Example 1


{% ic %}{% input custom.number_of.columns as:integer default:1 %}{% endic %}
{% assign number_of_columns = custom.number_of.columns | default:1 %}

{% stripnewlines %}
{% fori item in custom.rows1 %}

{% comment %}header{% endcomment %}
  {% if forloop.first %}
    |Description{% for number in (1..number_of_columns) %}|Column {{ number }}{% endfor %}{% newline %}
    |-----{% for number in (1..number_of_columns) %}|----{% endfor %}+#{% newline %}
  {% endif %}

|{% input item.description placeholder:'Description' %}
{% for number in (1..number_of_columns) %}
|{% input custom.[item.description.key].column_[number] as:currency placeholder:0 %}{% endfor %}{% newline %}

{% endfori %}
{% endstripnewlines %}


{% comment %}Example 2{% endcomment %}
{% comment %}Number of columns is unlimited{% endcomment %}
Example 2

{% stripnewlines %}
{% fori item in custom.rows2 %}

{% comment %}header{% endcomment %}
  {% if forloop.first %}
    |Description{% fori number in custom.[item.description.key] %}|Column {{ forloop.index0 }}{% endfori %}{% newline %}
    |-----{% fori number in custom.[item.description.key] %}|----{% endfori %}+#{% newline %}
  {% endif %}

|{% input item.description placeholder:'Description' %}
{% fori number in custom.[item.description.key] %}
|{% input number.column as:currency placeholder:0 %}{% endfori %}{% newline %}

{% endfori %}
{% endstripnewlines %}

Hi Jelena

Thank you for the response!

I opted for example 1.
Beneath you can find the result of your code.
To add an extra layer of complexity, to the right of ‘controle’ I would like to automatically calculate the entered amount above.
For example, beneath ‘boekjaar 1’ the total should be 300,00€, beneath ‘boekjaar 2’ the total should be 350,00.

But also, if i change the number of columns to 20, it should still automatically calculate the totals.

Thanks you in advance.

Hi @Henri-Louis

I have amended code, now it calculates subtotals of each column.
Please let us know if it works.

{% fori item in custom.rows1 %}

{% comment %}header{% endcomment %}
  {% if forloop.first %}
    |Description{% for number in (1..number_of_columns) %}|Column {{ number }}{% endfor %}{% newline %}
    |-----{% for number in (1..number_of_columns) %}|----{% endfor %}+#{% newline %}
  {% endif %}

|{% input item.description placeholder:'Description' %}
{% for number in (1..number_of_columns) %}
|{% input custom.[item.description.key].column_[number] as:currency placeholder:0 %}
{% capture sub_total %}sub_total_{{ number }}{% endcapture %}
{% assign [sub_total] = [sub_total]+custom.[item.description.key].column_[number] %}
{% endfor %}{% newline %}

{% comment %}Sub Total Value of each column{% endcomment %}
{% if forloop.last %}
|**Total**
{% for number in (1..number_of_columns) %}
{% capture sub_total %}sub_total_{{ number }}{% endcapture %}
|**{{ [sub_total] | currency }}**{% endfor %}{% newline %}

{% endif %}

{% endfori %}
{% endstripnewlines %}

Hi @Jelena_Sutova

Thank for your answer!

It works as requested!