Jayjay
Jayjay

Reputation: 103

How to add class in v-if Vue.js

I want to higlight the text in color red if the status is Inactive. Below is my code

<td v-for="option in selected_type" v-if="option.id == item.status"  v-bind:class="add class here">
    @{{ option.status }}
</td>

Thank you

Upvotes: 0

Views: 27117

Answers (2)

Vucko
Vucko

Reputation: 20834

A simple example, presuming that status is boolean:

new Vue({
  el: "#app",
  data: {
    items: [
      { label: 'foo', status: true },
      { label: 'bar', status: false }
    ]
  }
})
.text-red {
  color: red;
}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
  <p v-for="item in items" v-bind:class="{ 'text-red': item.status }">
    {{item.label}}
  </p>
</div>

Upvotes: 3

Renaud
Renaud

Reputation: 1300

<td v-for="option in selected_type" :key="option.id" :class="{ inactive: item.status }">

I'm assuming the status property is a boolean (Not sure why you're comparing the id with the status also)

.inactive { color: red; }

Upvotes: 4

Related Questions