Skip to content
Advertisement

Bind class to element depending on v-model in Vue.js

Trying to create dynamic table( link to Codepen) with Vue. Now i want to add class using :class to row when checkbox is checked. Every checkbox is bound with specific value in object of objects.

JavaScript

There is no error or warning, it just not working. I would be glad to hear any idea about solving this problem.

Advertisement

Answer

this is the problem:

this.checkboxes[data[index].id] = { id: data[index].id, checked: false };

the object property won’t reactive when defined in this way.

in https://v2.vuejs.org/v2/guide/reactivity.html,

Due to the limitations of modern JavaScript (and the abandonment of Object.observe), Vue cannot detect property addition or deletion.

Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it’s possible to add reactive properties to a nested object using the Vue.set(object, key, value) method

using .$set to set the object property would make it reactive.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement