Advanced Table Issue

Hi all -

I am generating a table with variable columns (based off a dropdown question box allowing us to select the number of columns and using a forloop to generate the table based off this). In this table, we have a calculated metric (Variable 1) in row #1. Then in Row #2 we want unique input boxes (input custom.some.thing) for each column (i.e. each custom.some.thing needs to be unique per each column). we have solved this by assigning “thing” = “thing | plus:1”.

However, the issue is we would like a 3rd row that subtracts out custom input row 2 from our calculated row 1 based on each column. In other words, how does one generate code that allows for a variable custom input in a table (i.e. input custom.some.thing) to subtract from the appropriate row in the appropriate column?

Any ideas?
Thanks!

Hi @Alexander_Krause_US,

Our apologies for the late reply.

If I understand your question correctly, I believe what you are trying to do is definitely possible.

However, to fully understand your case and to guide you towards a solution, it would be easier if you pasted a part or a simplified example of your code here so we can have a look.

Thank you!

Kind regards,
Robin

Hi Robin

Thanks for looking at this issue! Here’s a simplified code containing the input table (above the “divider”) and the output table where the math and inputs are to be coded. My issue is the very last line getting the math to work.

Happy to clarify anything that might be confusing. I appreciate your help!

Alex

CODE:

{% comment %}input variable number of columns (and headings) {% endcomment %}
{% fori columns in custom.variable_columns %}
{% input columns.name %}
{% input columns.number %}

{% endfori %}

----------------------DIVIDER-------------------------------

{% comment %}custom headings {% endcomment %}
{% stripnewlines %}
{% for columns in custom.variable_columns %}
| {{ columns.name }}
{% endfor %}

{% comment %}table formatting{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
|--------
{% endfor %}

{% comment %}calculated values{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
| {{ columns.number }}
{% endfor %}

{% comment %}cusotm input values{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
{% assign number = number | plus:1 %}
|{% input custom.variable.[number] %}
{% endfor %}

{% comment %}math (Pain point){% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
|^MATH EQUATION HERE (subtraction)^
{% endfor %}

{% endstripnewlines %}

@Alexander_Krause_US

I have amended your code & added comments.
Please let me know if it works for your task.

Thanks.

{% fori columns in custom.variable_columns %}
{% input columns.name %}
{% input columns.number %}

{% endfori %}

----------------------DIVIDER-------------------------------

{% comment %}custom headings {% endcomment %}
{% stripnewlines %}
{% for columns in custom.variable_columns %}
| {{ columns.name }}
{% endfor %}

{% comment %}table formatting{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
|--------:
{% endfor %}

{% comment %}calculated values{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
| {{ columns.number }}
{% endfor %}

{% comment %}cusotm input values{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
{% comment %}Instead of number - it is better to use key value of each element from fori input.
The reason is - if you delete element 2 in fori loop, related variables should be deleted as well.

If you would like to attach variable name to number, you can do it by using 
{% input custom.variable1.[forloop.index] %}

forloop.index : numbering of each element starts at 1
forloop.index0 : numbering of each element starts at 0

{% endcomment %}

|{% input custom.variable1.[columns.key] %}
{% endfor %}

{% comment %}cusotm input values{% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
|{% input custom.variable2.[columns.key] %}
{% endfor %}

{% comment %}math (Pain point){% endcomment %}
{% newline %}
{% for columns in custom.variable_columns %}
{% comment %}MATH EQUATION HERE (subtraction){% endcomment %}
|{{ custom.variable1.[columns.key]-custom.variable2.[columns.key] | currency }}
{% endfor %}

{% endstripnewlines %}

Hi Jelena!

Thank you for the code and explanation. I was able to apply it effectively to our more complex table!

I used the forloop.index as my key value.

One follow-up question I have is, what exactly is the purpose of [columns.key]? Is this an alternative to using forloop.index?

Alex Krause

@Alexander_Krause_US

Thank you for your question.
The purpose of .key is that each input element of fori loop has a unique key, which can be connected to variables related to that input element.

For example,

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

{% input item.car placeholder:"Car model" %} 
  {% if item.car.persisted %}
    {% input custom.variable_price.[item.key] placeholder:"Car price" as:currency %}
    {% input custom.variable_colour.[forloop.index] placeholder:"Car colour" %} {% newline %}
  {% endif %}

{% endfori %}
{% endstripnewlines %}

Example of main input in fori loop and related variables:

1st item.                            Car A
custom.variable_price.test_1         1,000
custom.variable_colour. 1.             red

2nd item                            Car B
custom.variable_price.test_2        2,000
custom.variable_colour.2            green

3rd item.                           Car C
custom.variable_price.test_3        3,000
custom.variable_colour.3             blue

Let’s say, user deletes 2nd item in the loop - Car B.

Variables & results will be:

1st item                            Car A
custom.variable_price.test_1         1,000
custom.variable_colour. 1.           red

2nd item                            Car C
custom.variable_price.test_3        3,000
custom.variable_colour.2            green

As a result 3rd item Car C, become now 2nd item in the loop.
Car C had related price 3,000 & colour blue.

Related price 3,000 was connected by using .key - it is displayed correctly.
Related colour blue was connected by using forloop.index number, after deletion displayed colour is green - not correct, should be blue . The reason is item number in the loop has changed.

To sum up, .key and forloop.index can be used to connect variables to for loop input item.
Due to above reason, if the sequence number of element can change in a loop/array - we recommend to avoid using forloop.index in connected variables names.

Please let me know if you have any further questions regarding .key and forloop.index usage.

Thanks.
Jelena