7

I have been using vue for about a month now but one thing that I haven't really been able to do properly yet is this.

so in twig we can do something like this

<th class=" 
  {% if condition %}
      class1
  {% else %}      
      class2
  {% end if %}  
">

<th class="sorting_asc" v-for="col in columns" v-if="col.label == sortKey">
<th class="sorting" v-for="col in columns" v-else>

So my first issue is I can't seem to be able to achieve something similar in Vue js. And secondly where I've written 'v-if="col.label == sortKey"', Can I even access the car col.label outside of the html block (outside the loop). Thank you in advanced

1 Answer 1

16

You can do following with vuejs with dynamic styling:

<th v-bind:class="{'class1': condition, 'class2': !condition}">

From documentation:

<div v-bind:class="{ active: isActive }"></div>

The above syntax means the presence of the active class will be determined by the truthiness of the data property isActive. you can as well put simple conditions returning boolean in place of isActive

Sign up to request clarification or add additional context in comments.

3 Comments

But there is still one question not clear. Can I use the ${ col.label } if the if there? as that value comes from the for loop
@TheMan68 I believe you are asking to do this: <th class="{'sorting_asc': col.label == sortKey, 'sorting': col.label != sortKey}" v-for="col in columns"
For completeness, it is also possible to use a ternary expression in your example: <th v-bind:class="[condition ? class1 : class2]">.

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.