D
D
Daksin2022-02-10 15:25:50
Vue.js
Daksin, 2022-02-10 15:25:50

How can I display the price from this form?

Good afternoon, I ran into a problem that I don’t know how I can display the final price from these checkboxes, that is, if the checkbox is selected, then a certain value is added to the price. How can I organize all this using vue?
62050443da88d151190594.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2022-02-10
@Daksin

data: () => ({
  items: [
    { text: '...', price: ..., checked: false },
    { text: '...', price: ..., checked: false },
    ...
  ],
}),
computed: {
  sum() {
    return this.items.reduce((acc, n) => acc + n.price * n.checked, 0);
  },
},

<div v-for="n in items">
  <label>
    <input v-model="n.checked" type="checkbox">
    {{ n.text }}
  </label>
</div>
<div>SUM: {{ sum }}</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question