Skip to content
Advertisement

add empty cell in dynamic twig table

Hi I’ve got the following array

Array
(
    [6] => Array
        (
            [id] => 6
            [immobile] => 3
            [interno] => 1A
            [condomino] => Giuseppe Rossi (P)
            [Tabella generale] => Array
                (
                    [spesa] => 187.50
                    [millesimi] => 250.0000
                )

            [Tabella scale] => Array
                (
                    [spesa] => 125.00
                    [millesimi] => 250.0000
                )

        )

    [7] => Array
        (
            [id] => 7
            [immobile] => 3
            [interno] => 1A
            [condomino] => Francesco Rossi (Co-P)
            [Tabella generale] => Array
                (
                    [spesa] => 187.50
                    [millesimi] => 250.0000
                )

            [Tabella scale] => Array
                (
                    [spesa] => 125.00
                    [millesimi] => 250.0000
                )

        )

    [9] => Array
        (
            [id] => 9
            [immobile] => 3
            [interno] => 1A
            [condomino] => Alessandro Rossi (I)
            [Tabella generale] => Array
                (
                    [spesa] => 0.00
                    [millesimi] => 250.0000
                )

            [Tabella scale] => Array
                (
                    [spesa] => 125.00
                    [millesimi] => 250.0000
                )

        )

    [8] => Array
        (
            [id] => 8
            [immobile] => 5
            [interno] => 1B
            [condomino] => Alessandro Rossi (P)
            [Tabella generale] => Array
                (
                    [spesa] => 375.00
                    [millesimi] => 250.0000
                )

        )

)

And I need to convert it into a table using twig, this is what i’ve achieved so far

enter image description here

But i need to achieve this instead:

enter image description here

This is my code so far:

  <table class="table border-top-0 border-bottom small">

    <thead>
        <tr class="border-left-0">

          {% for titolo in ripartoPreventivo|first|keys|slice(1) %}
            <th class="font-weight-bold text-uppercase text-14">{{titolo}} </th>
          {% endfor %}

          <th class="font-weight-bold text-uppercase text-14"> Totale</th>

        </tr>
    </thead>

    <tbody>

      {% for sub_array in ripartoPreventivo %}
          <tr>
            {% for value in sub_array|slice(1,2) %}
              <td class="font-weight-bold small">{{ value|raw }}</td>
            {% endfor %}

            {% for value in sub_array|slice(3) %}

                <td class="font-weight-bold small">€ -{{ value.spesa|raw }} <span class="small text-muted"><strong>(mill. {{ value.millesimi|raw }})</strong></span></td>

            {% endfor %}

            {% set sum = 0 %}

            {% for value in sub_array|slice(3) %}
              {% set sum = sum + value.spesa %}
            {% endfor %}

            <td class="font-weight-bold small"> € -{{ sum|number_format(2, '.', ',') }}</td>
          </tr>
      {% endfor %}


    </tbody>



  </table>

As you can see from the pictures I need to add an empty cell if the dynamic key in the array doesn’t exist, instead with my code it put the total where the empty cell should be, any help? many thanks

Advertisement

Answer

Well same as you printed the keys to use them as headers, you can use those keys to output the data

Do note: This will only work if the structure of the data remains the same, meaning you only use an array to store the info about spesa and millesimi

In the snippet below I use the test iterable to determine whether the value is an array, based on that the code assumes, if it’s an array the keys spesa and millesimi are present.

If you have any other array based values you would need to perform some extra checks

<table>
    <tr>
        {% for key in ripartoPreventivo|first|keys|slice(1) %}
            <th>{{ key }}</th>
        {% endfor %}
        <th>Total</th>
    </tr>
    {% for row in ripartoPreventivo %}
    <tr>
        {% for key in ripartoPreventivo|first|keys|slice(1) %}
        <td>
            {% set value = row[key]|default('&nbsp;') %}
            {% if value is iterable %}
                € -{{ value.spesa|raw }} <span class="small text-muted"><strong>(mill. {{ value.millesimi|raw }})</strong></span>
            {% else %}
                {{ value|raw }}
            {% endif %}
        </td>
        {% endfor %}
        <td>
            500
        </td>
    </tr>
    {% endfor %}
</table>

demo


Just an extra FYI:

Keep in mind that it is possible not all keys will be present in the table as you are using ripartoPreventivo|first|keys|slice(1). This would mean if the first row in the set doesn’t have the key Tabella Scale present, it will never be shown in the table!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement