windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

V-if inside v-for – display list of items in two columns

Asked By: Anonymous

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

  1. Using pure CSS with display: flex

But that can only do this:

| Item1   Item2 |
| Item3   Item4 |
| Item5   Item6 |
| Item7         |
  1. 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.

  1. So I gave up on fixing it using CSS.

If it had been php, then I’d simply do something like this:

if( $index == count( $items)/2 ):
  echo '</div>';
  echo '</div>';
  echo '<div class="col-md-6">';
  echo '<div class="item-container">';

… But it’s not. And I’m looking for the vue-alternative. I tried this:

{{#if key === Number( items.length / 2 ) }}
  <div class="col-md-6">
  <div class="item-container">

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><!-- /.item-container -->
    </div><!-- /.items-container -->
</div><!-- /.col-md-12 -->


Answered By: Anonymous

What I would do is create a computed property dividing (or chunking) the items array into the appropriate number of columns.

Here’s an example that uses a flexbox layout and one extra column element.



new Vue({_x000D_
  el: 'main',_x000D_
  data: {_x000D_
    items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7'],_x000D_
    cols: 2_x000D_
  computed: {_x000D_
    columns () {_x000D_
      let columns = []_x000D_
      let mid = Math.ceil(this.items.length / this.cols)_x000D_
      for (let col = 0; col < this.cols; col++) {_x000D_
        columns.push(this.items.slice(col * mid, col * mid + mid))_x000D_
      return columns_x000D_


.container {_x000D_
  display: flex;_x000D_
  border: 1px solid;_x000D_
.col {_x000D_
  margin: 10px;_x000D_
  border: 1px solid;_x000D_
  flex-grow: 1;_x000D_
  display: flex;_x000D_
  flex-direction: column;_x000D_
.item-container {_x000D_
  border: 1px solid;_x000D_
  padding: 5px;_x000D_
  margin: 5px;_x000D_


<a href="[email protected]/dist/vue.min.js">[email protected]/dist/vue.min.js</a>_x000D_
<p><label>Columns:<label> <input type="number" v-model="cols"></p>_x000D_
<div class="container">_x000D_
  <div class="col" v-for="column in columns">_x000D_
    <div class="item-container" v-for="item in column">{{item}}</div>_x000D_




If you’d like a less verbose way of chunking the items array, see Split array into chunks

techinplanet staff

instagram volgers kopen volgers kopen buy windows 10 pro buy windows 11 pro

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
Alanya escort Manavgat escort Fethiye escort Kemer escort Didim escort Çanakkale escort Aydın escort Muğla escort Tekirdağ escort Manisa escort Balıkesir escort Trabzon escort Elazığ escort Ordu escort Kütahya escort Isparta escort Rize escort Kahramanmaraş escort Yalova escort Giresun escort Yozgat escort Tokat escort Şanlıurfa escort Sivas escort Batman escort Erzurum escort Sinop escort Kırşehir escort Karaman escort Kırıkkale escort Bolu escort Amasya escort Niğde escort Uşak escort Edirne escort Çorum escort Osmaniye escort Zonguldak escort Van escort Erzincan escort Söke escort Bodrum escort Çerkezköy escort Akhisar escort Bandırma escort Ayvacık escort Akçaabat escort Karakoçan escort Altınordu escort Tavşanlı escort Eğirdir escort Ardeşen escort Afşin escort Altınova escort Bulancak escort Sorgun escort Erbaa escort Viranşehir escort Zara escort Kozluk escort Aziziye escort Ayancık escort Kaman escort Ermenek escort Keskin escort Gerede escort Göynücek escort Bor escort Banaz escort Havsa escort Osmancık escort Bahçe escort Alaplı escort Başkale escort Kemah escort Nazilli escort Fethiye escort Çorlu escort Alaşehir escort Altıeylül escort Biga escort Araklı escort Kovancılar escort Fatsa escort Simav escort Yalvaç escort Çayeli escort Dulkadiroğlu escort Çiftlikköy escort Espiye escort Sarıkaya escort Niksar escort Suruç escort Yıldızeli escort Sason escort Horasan escort Boyabat escort Mucur escort Sarıveliler escort Yahşihan escort Göynük escort Gümüşhacıköy escort Çamardı escort Eşme escort İpsala escort Sungurlu escort Hasanbeyli escort Çaycuma escort İpekyolu escort Refahiye escort Kuşadası escort Marmaris escort Süleymanpaşa escort Turgutlu escort Susurluk escort Gelibolu escort Of escort Ünye escort Domaniç escort Fındıklı escort Elbistan escort Çınarcık escort Tirebolu escort Akdağmadeni escort Turhal escort Eyyübiye escort Suşehri escort Yakutiye escort Gerze escort Mengen escort Merzifon escort Ulukışla escort Sivaslı escort Keşan escort Kadirli escort Ereğli escort Özalp escort Tercan escort Efeler escort Didim escort Çine escort Dalaman escort Menteşe escort Milas escort Ortaca escort Seydikemer escort Ergene escort Kapaklı escort Malkara escort Salihli escort Şehzadeler escort Soma escort Yunusemre escort Ayvalık escort Bigadiç escort Burhaniye escort Gönen escort Karesi escort Çan escort Yenice escort Ortahisar escort Yomra escort Perşembe escort Pazar escort Onikişubat escort Pazarcık escort Türkoğlu escort Eynesil escort Görele escort Piraziz escort Yağlıdere escort Çayıralan escort Boğazlıyan escort Zile escort Siverek escort Karaköprü escort Haliliye escort Akçakale escort Şarkışla escort Gemerek escort Oltu escort Palandöken escort Mudurnu escort Suluova escort Taşova escort Toprakkale escort Kilimli escort Tuşba escort Üzümlü escort