Answer the question
In order to leave comments, you need to log in
How to rewrite this code in vue?
There is a code - link
Only I know vue. I read in the documentation about v-model with an array of checkboxes. I write like this:
let app = new Vue({
el: '#app',
data: {
inputs: []
}
})
<div class="checkbox-item">
<input type="checkbox" name="" id="c1" value="16000" class="checkbox" v-model="inputs">
<label for="c1">Пакет 1</label>
</div>
<div class="checkbox-item">
<input type="checkbox" name="" id="c2" value="17000" class="checkbox" v-model="inputs">
<label for="c2">Пакет 2</label>
</div>
<div>
{{inputs}}
</div>
{{inputs}}
simply through a line each value. And how to make the values summed up, i.e. like in the original js code?
Answer the question
In order to leave comments, you need to log in
First of all, you should get rid of the copy-paste in the markup. We see a group of similar elements - leave one, hang on it v-for
, to which an array with data will be transferred. The base unit will look something like this:
baseOptions: [
{ name: 'Свойство 1', price: 10000 },
{ name: 'Свойство 2', price: 11000 },
...
<ul>
<li v-for="n in baseOptions">{{ n.name }} ({{ n.price }})</li>
</ul>
v-model
):extraOptions: [
{ name: 'Пакет 1', price: 16000, checked: false },
{ name: 'Пакет 2', price: 17000, checked: false },
...
<div v-for="n in extraOptions">
<input type="checkbox" v-model="n.checked">
<label>{{ n.name }}</label>
</div>
methods: {
sum: arr => arr.reduce((acc, n) => acc + n.price, 0),
computed: {
baseSum() {
return this.sum(this.baseOptions);
},
extraSum() {
return this.sum(this.extraOptions.filter(n => n.checked));
},
filters: {
price: val => `${val.toLocaleString()} р.`,
<span>{{ baseSum | price }}</span>
<span>{{ extraSum | price }}</span>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question