Home > Blockchain >  Vuetify/VueJS - Multiple conditions on md/lg/xl
Vuetify/VueJS - Multiple conditions on md/lg/xl

Time:05-24

I'm struggling here applying conditions on the breakpoints in Vue/Vuetify

Basically, I want to say... if the length of the array is divisible by 2, then MD=6, if it is divisible by 3 then MD=12

<v-col cols="12"
    :md="module.plannings.length % 2 === 0 ? 6  ? module.plannings.length % 2 === 0 ? 12 : 12"
    v-for="planning in module.plannings"
    :key="planning.id"
>

Clearly I'm going about it the wrong way here.. what would be the correct syntax?

CodePudding user response:

You can make computed property:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      module: {id: 0, plannings: [0,1,2,3,4,5]}
    }
  },
  computed: {
    msize() {
      return this.module.plannings.length % 2 === 0 ? 
        6 : this.module.plannings.length % 3 === 0 ? 
        12 : 12
    }
  }
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<div id="app">
  <v-app>
    <v-main>
      <v-container>
        <v-col cols="12"
            :md="msize"
            v-for="planning in module.plannings"
            :key="planning.id"
        >
        {{planning}}
        </v-col>
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

CodePudding user response:

Correct syntax will be :md="plannings.length % 2 === 0 ? 6 : 12"

Demo :

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    plannings: [{
      id: 1
    }, {
      id: 2
    }]
   }),
});
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css"/>
<div id="app">
  <v-row>
    <v-col
           :md="plannings.length % 2 === 0 ? 6 : 12"
           v-for="planning in plannings"
           :key="planning.id"
           style="background-color: yellow"
           >
           {{ planning.id }}
    </v-col>
  </v-row>
</div>

  • Related