Skip to content
Advertisement

Array watch with Vue.js 2

I’m having a set of selects (item.pricelists that are assigned to an item) rendered in a v-for loop and bound with v-model. Possible select options for that item are rendered with another v-for from item.valid_pricelists. Both are pulled from the server in the same array. Now I need to watch for when user changes selection , so i can update the item.pricelists array with new values from item.valid_pricelists.

Render part :

JavaScript

Watch part :

JavaScript

Now, watch – that works just fine for simple vars – refuses to budge.

Here’s the fiddle with relevant part of the code.

I was looking at the vue docs, but can’t wrap my head around it. Any help would be appreciated.

Advertisement

Answer

I don’t know if it’s possible to solve this with watch, however, I would suggest another approach. You could add v-on:change to your selects:

JavaScript

and add a method to the Vue instance:

JavaScript

This way you will know that selection has changed whenever the selectionChanged function is called.

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