I'm sorry for posting this, - because I can see that many questions similar to this one has been asked several time. Here's the ones that came closes to helping me - and why they didn't:
- This one is because the calculation shouldn't be done in the rendering, but rather in the methods/computed section. That doesn't help me.
- This one is using two different templates, writing the
v-if
on the template-tag. This would seem foolish in my case, since the two templates would be 98% identical. - This Medium-article addresses a problem very very close to mine. However, - it's a filtering of users in his case (which is solved by computed properties), and not an if-clause that inserts a snippet of code at a certain iteration (which is what I think I'm looking for).
The problem
I have a list of items, being pulled from an API, - so the amount will change. I want them displayed in two columns as such:
-----------------
| Item1 Item5 |
| Item2 Item6 |
| Item3 Item7 |
| Item4 |
-----------------
I'm looping through them using a v-for
loop.
My attempts
- Using pure CSS with
display: flex
But that can only do this:
-----------------
| Item1 Item2 |
| Item3 Item4 |
| Item5 Item6 |
| Item7 |
-----------------
- Using CSS with
column-count: 2;
But that breaks of the column mid-element, regards of display: block; overflow: hidden;
and many other attemps. It should be said, that the height of these elements can vary.
- So I gave up on fixing it using CSS.
If it had been php
, then I'd simply do something like this:
<?php
if( $index == count( $items)/2 ):
echo '</div>';
echo '</div>';
echo '<div class="col-md-6">';
echo '<div class="item-container">';
endif;
?>
... But it's not. And I'm looking for the vue-alternative. I tried this:
{{#if key === Number( items.length / 2 ) }}
</div>
</div>
<div class="col-md-6">
<div class="item-container">
{{/if}
But it doesn't work. And as far as I can tell, then it's not 'the vue way' of doing it. But I can't figure out what is. :-/
Does any such thing exist?
A simplification of my current code
<div class="col-md-12">
<div class="items-container">
<div class="item-container" v-for="item, key in items['data']">
<!-- A BUNCH OF ITEM-INFO -->
</div>
</div>
</div>