I have a string pipe of values, which I want to modify. the string pipe has a range from 0-5 values, so I made the following:
new Vue({
el: '#app',
data: {
valuesString: ""
},
computed: {
values: {
get() {
var values = this.valuesString ? this.valuesString.split("-") : [];
if(values.length < 5)
values.push(null);
return values;
},
set(values) {
this.valuesString = values.filter(value => value).join("-")
}
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
<div v-for="(value, i) in values" :key="i">
<select v-model="values[i]" style="width: 200px">
<option></option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<br>
<span>valuesString: {{ valuesString }}</span>
<br>
<span>values: {{ values }}</span>
</div>
The problem now is, that the setter of my computed values
property getn't called.
my usecase is a filter like a categories filter. I get the valuesString
as parameter from my router. The user should be able to select 1-5 categories to filter. every time the filter changes the router paramter should change and a new empty select should appear until there are 5 categories set.
maby my question wasn't clear. Bert's comment is right
I solved my problem with a workaround, but I'm not so satisfied. maby anyone can help me to optimize it. For this I tried to use computed setters.