Skip to content
Advertisement

add empty cell in dynamic twig table

Hi I’ve got the following array

JavaScript

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:

JavaScript

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

JavaScript

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