在动态树枝中添加空单元格 table

add empty cell in dynamic twig table

您好,我有以下数组

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
                )

        )

)

我需要使用 twig 将其转换为 table,这就是我目前所取得的成就

但我需要实现这一点:

到目前为止,这是我的代码:

  <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>

正如您从图片中看到的那样,如果数组中的动态键不存在,我需要添加一个空单元格,而不是使用我的代码将总数放在空单元格应该在的位置,有什么帮助吗?非常感谢

我想你可以在渲染你的树枝之前这样做。它正在添加空数据作为索引 Tabella scale。如果索引存在,则该行不受代码影响

$data 是你的初始数组

$initial = [
    'spesa' => '',
    'millesimi' => '',
];
foreach($data as $item) {
    if(!isset($item['Tabella scale'])) {
        $item['Tabella scale'] = $initial;
    }
}

编辑:

根据评论,我建议你这样做(不是最干净的)

$modele = [
    "id" => '',
    "immobile" => '',
    "interno" => '',
    "condomino" => '',
    'Tabella generale' => [
        'spesa' => '',
        'millesimi' => '',
    ],
    'Tabella scale' => [
        'spesa' => '',
        'millesimi' => '',
    ]
];
$data = [
    [
        "id" => 10,
        "condomino" => '20',
        'Tabella generale' => [
            'spesa' => '100',
            'millesimi' => '25',
        ],
    ],
    [
        "id" => 22,
        "interno" => '15',
        'Tabella scale' => [
            'spesa' => '100',
            'millesimi' => '25',
        ],
    ],
];
//add all fields from model to all data
foreach($data as $key => $item) {
    //to add fields, use a simple merge
    //be careful of array_merge order
    //if array_merge($item, $modele), your values will be erased
    $new_item = array_merge($modele, $item);
    $data[$key] = $new_item;
}

//reorder based on model keys
$reordered = [];
foreach($data as $row) {
    $item = [];
    #loop over your model keys to have model's order
    foreach($modele as $key_model => $modele_value) {
        #add to your new ordered item at the index of the model, your current value
        $item[$key_model] = $row[$key_model];
    }
    $reordered[] = $item;
}

好吧,就像你打印键来使用它们一样headers,你可以使用这些键来输出数据

请注意: 这仅在数据结构保持不变时才有效,这意味着您仅使用数组来存储有关 spesamillesimi

在下面的代码片段中,我使用测试 iterable 来确定该值是否是一个数组,基于代码假设,如果它是一个数组,则键 spesamillesimi 存在

如果您有任何其他基于数组的值,则需要执行一些额外的检查

<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


仅供参考:

请记住,当您使用 ripartoPreventivo|first|keys|slice(1) 时,可能并非所有键都出现在 table 中。这意味着如果集合中的第一行没有键 Tabella Scale,它将永远不会显示在 table!