Vue.js - multiple condition class binding

Viewed 2724

What is the proper way to bind class to a tag based on multiple conditions?

Given this tag, it seems that when trying to write multiple conditions one is being overwritten by another.

<q-tr :props="props" 
    :class=["(props.row.Name=='Row Name 1' || props.row.Name=='Row Name 2')?'text-bold':'bg-white text-black', (props.row.Name=='Row Name 3')?'text-green':'bg-white text-black']
>
</q-tr>

So in the above example text-bold class is overwritten by bg-white text-black since the second condition is overriding the first class binding.

Is there a way to structure conditions in if, else if, else style in vue class binding?

1 Answers

Bind that class attribute to a computed property called myClass :

<q-tr
    :class="myClass"
>
</q-tr>
computed:{
   myClass(){
     if(this.props.row.Name=='Row Name 1' ){
         return 'text-bold';
      }
      else if( this.props.row.Name=='Row Name 3'){
         return 'text-green';
      }
      else{
           return 'bg-white text-black'
      } 

   }

}
Related