Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to break list into columns using VueJs?

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.

like image 694
Somethingwhatever Avatar asked Nov 18 '19 16:11

Somethingwhatever


1 Answers

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.

like image 198
Bryce Howitson Avatar answered Nov 07 '22 16:11

Bryce Howitson