5

I have a List which is being rendered using v-for and i am outputting the data into one column. Is there a way that after a certain width and height, the data flows into next column without making any new Array? Here is a demo.

new Vue({
  el: '#app',
  data() {
    return {
      lists: [{
          text: 'Item1'
        },
        {
          text: 'Item2'
        },
        {
          text: 'Item3'
        },
        {
          text: 'Item4'
        },
        {
          text: 'Item5'
        },
        {
          text: 'Item6'
        },
        {
          text: 'Item7'
        },
        {
          text: 'Item8'
        },
        {
          text: 'Item9'
        },
      ]
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet" />
<div id="app">
  <v-app id="inspire">
    <v-layout class="mt-3 ml-4">
      <v-flex md-4>
        <div v-for="list in lists" :key="list.text">
          {{list.text}}
        </div>
      </v-flex>
      <v-flex md4>
        //After a certain width and height, make data flow into this column?
      </v-flex>
    </v-layout>
  </v-app>
</div>

I prefer to use widths and heights instead of creating a new Array just so that i don't have to lot of manual adjustments throughout the code to deal with the new array.

6
  • If your list has a fixed height there are a number of solutions for this. Flex columns and wrapping for instance. Commented Nov 18, 2019 at 16:36
  • I actually don't have a fixed height set up on it so i would actually go with the easiest viable option possible i guess. Commented Nov 18, 2019 at 16:38
  • The easiest solution has nothing to do with Vue. Simply use CSS to modify the layout. You don't need to create a new array unless you wanted to split the columns at an exact spot (eg after the last item that starts with A). Commented Nov 18, 2019 at 16:44
  • @BryceHowitson That makes sense. I thought i had to do it with Vue lol. thanks for the tip. Commented Nov 18, 2019 at 16:47
  • Do you need help with the CSS? I'd remove the second v-flex and convert the first one into a wrapper with a class in an effort to make your markup easier to style. Commented Nov 18, 2019 at 16:49

1 Answer 1

7

You can use CSS to easily create columns. Just use a v-for to get the content on the screen then style it with CSS. In your case the loop will append multiple <div> elements. Here's an example of CSS columns.

This is the most basic solution where column-count: 3 splits the list into 3 equal columns.

.column_wrapper {
  column-count: 3;
}
<div class="column_wrapper">
  <!-- results of <div v-for="item in list"></div>  -->
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
  <div>Item 5</div>
  <div>Item 6</div>
  <div>Item 7</div>
  <div>Item 8</div>
  <div>Item 9</div>
  <div>Item 10</div>
  <div>Item 11</div>
  <div>Item 12</div>
  <div>Item 13</div>
  <div>Item 14</div>
  <div>Item 15</div>
</div>

The downfall of this solution is that the browser will ALWAYS try to break the list into 3 columns. If you only want the columns to split after a specific height, you can use flex-box

For example (if the list is longer than what fits into 200px tall)

.column_wrapper {
    max-height: 200px;
    display: flex;
    flex-flow: column wrap;
}
<div class="column_wrapper">
  <!-- results of <div v-for="item in list"></div>  -->
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
  <div>Item 5</div>
  <div>Item 6</div>
  <div>Item 7</div>
  <div>Item 8</div>
  <div>Item 9</div>
  <div>Item 10</div>
  <div>Item 11</div>
  <div>Item 12</div>
  <div>Item 13</div>
  <div>Item 14</div>
  <div>Item 15</div>
</div>

Note: in both examples the location at which it splits is arbitrary (as in the brower decides when to move to the next column). If you need to decide where it breaks, creating a new array for each column in vue is your best bet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.