3

I want to ask you, if its possible doing this with dynamic class like this. <div :class="'outofstock.item_' + item.id ? 'bg-green-500' : 'bg-red-500' "> If exist solution, I cant find a rigth syntax for this. Thank you for a answers.

thats a example what I try to do. I need a result outofstock.item_1, outofstock.item_2 and outofstock.item_3 in :claas

<template>
<ul>
    <li v-for="item in items">
        <div :class="'outofstock.item_' + item.id ? 'bg-green-500' : 'bg-red-500' ">
            {{ item.name }}
        </div>
    </li>
</ul>
</template>

<script>   
    export default {
        data () {
            return {
                items: [{id: 1, name: 'monitor'}, {id: 2, name: 'printer'},{id: 3, name: 'scanner'],
                outofstock: {item_1: false, item_2: true, item_3: false}
            }
        }
    }
</script>
1
  • so do you want outofstock.item_1 or outofstock.item_1 bg-green-500 or bg-green-500? Commented Jan 18, 2023 at 19:43

3 Answers 3

3

This works without calling a method: Vue Playground

<template>
  <ul>
    <li v-for="item in items">
      <div :class="outofstock['item_' + item.id] ? 'bg-green-500' : 'bg-red-500' ">
        {{ item.name }}
      </div>
    </li>
  </ul>
</template>
<script>
  export default {
    data() {
      return {
        items: [{ id: 1, name: 'monitor'}, { id: 2, name: 'printer'}, { id: 3, name: 'scanner'}],
        outofstock: {
          item_1: false,
          item_2: true,
          item_3: false
        }
      }
    }
  }
</script>
<style>
  .bg-green-500 {
    background-color: green;
  }
  .bg-red-500 {
    background-color: red
  }
</style>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answers, and may I ask you to show me what could be solved better?
I know, I check all solutions and thanks for them. I realy dont know yet how stackowerflow working. I thanking you in coments and at this time I dont see that comment. And I see in all 3 answer, I set a useful a answer. Thank you again for helping me.
@StEvE, you're welcome, the question in comment above is for tauzN
2

Define a computed property that returns a function which takes the item id as parameter, then use string template `` to render the right class :

<template>
<ul>
    <li v-for="item in items">
        <div :class="`${getItemValue(item.id) ? 'bg-green-500' : 'bg-red-500' }`">
            {{ item.name }}
        </div>
    </li>
</ul>
</template>

<script>   
    export default {
        data () {
            return {
                items: [{id: 1, name: 'monitor'}, {id: 2, name: 'printer'},{id: 3, name: 'scanner'],
                outofstock: {item_1: false, item_2: true, item_3: false}
            }
        },
   computed:{
       getItemValue(){
         return (id)=>this.outofstock[`item_${id}`];
       }
     }
    }
</script>

Comments

1

You can create method and return right value:

const app = Vue.createApp({
  data() {
    return {
      items: [{id: 1, name: 'monitor'}, {id: 2, name: 'printer'},{id: 3, name: 'scanner'}],
      outofstock: {item_1: false, item_2: true, item_3: false}
    };
  },
  methods: {
    getOutofstock(id) {
      return this.outofstock[id]
    }
  }
})
app.mount('#demo')
.bg-green-500 {
  color: green;
}
.bg-red-500 {
  color: red;
}
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
<ul>
    <li v-for="item in items">
        <div :class="getOutofstock('item_'+item.id) ? 'bg-green-500' : 'bg-red-500' ">
            {{ item.name }}
        </div>
    </li>
</ul>
</div>

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.