1

I have a list of items - each item has an isDefault property. The initial value of item.isDefault is false.

I have a button with a method which toggles 'isDefault' - which should change a css class on the item (A font awesome class. Font awesome css is loaded, and the class works when used on the page as static html).

<div v-for="(item, index) in items">
  <button class="button" @click="toggleDefault(index)">
    <span class="icon is-small">
        <i class="fas" :class="item.isDefault ? 'fa-check' : 'fa-minus' "></i>
    </span>
    <span>is item default? {{item.isDefault}}</span>
  </button>
</div>

My toggleDefault method works - I pass the item index in, and do:

toggleDefault(i) {
    this.items[i].isDefault = !this.items[i].isDefault;
}

Indeed, I can see the value change from true to false in dev tools, and the html, and I can log the change to the console...

But the css class doesn't change. Looking through the inspector, it remains 'fa-minus'. I use the same pattern elsewhere and it works? Am I doing something dumb?

** Edit ** Added my JS - isDefault is defined on each item... toggleDefault toggles the value - but the class still doesn't change...

const app = new Vue({
    el: '#app',
    data: {
        items: [
            {
                name: 'foo',
                isDefault: false,
            },
            {
                name: 'bar',
                isDefault: false,
            }
        ],
    },
    methods: {
        toggleDefault(i) {
            this.items[i].isDefault = !this.items[i].isDefault;
        }
    }
})
3
  • 1
    check to see if there are any unrelated error preventing you from updating the class because I copied your code and it worked, could see the change in classes in the inspector html Commented Jul 22, 2019 at 21:47
  • 1
    Where and how is items defined? The only way this can happen is if your item objects don't actually have an isDefault property in their initial data Commented Jul 23, 2019 at 0:31
  • Adding a class to the button - it changes! But the class on the icon does not.... Commented Jul 23, 2019 at 12:49

1 Answer 1

1

Found the problem.... I'm using font-awesome SVG.... Font awesome swaps the tags out! the tag isn't there for vue to run the method on it!

Solution - wrap the font awesome tag in a v-if condition... this feels a bit gross, but it works.

<button class="button" :class="item.isDefault ? 'is-primary' : 'is-white' " @click="toggleDefault(item)">
   <template  v-if="item.isDefault">
       <span class="icon is-small">
            <i class="fas fa-check"></i>
       </span>
   </template>
   <template  v-if="!item.isDefault">
       <span class="icon is-small">
             <i class="fas fa-minus"></i>
       </span>
   </template>
   <span>Set as Default</span>
</button>
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer. I'm sure this will come in handy for others in future. You should mark it as the accepted answer
Thanks - have just checked it

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.