A
A
Anton89892018-07-29 13:18:16
Vue.js
Anton8989, 2018-07-29 13:18:16

How to make a simple checkbox calculator with Vue.js?

Good afternoon, there is not a big calculator with jquery checkboxes, you can help to do the same only on vue.js. I am attaching the code

<input type="checkbox" value="3.99" id="one" checked>
<input type="checkbox" value="5.99" id="two">
<input type="checkbox" value="7.99" id="three">
 <div id="total"></div>

var total = 0;
function CalculateTotal(){
    $("input:checkbox").each(function(){
        if($(this).is(":checked")){
            total += parseFloat($(this).val());
        }
    });
    $("#total").html(total);
}

$("input:checkbox").change(function(){
    total = 0;
    CalculateTotal();
}).trigger("change");

Link here ( jsfiddle.net/codeSpy/e3vb1tv8 ), thanks in advance!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-07-29
@Anton8989

data: () => ({
  items: [
    { value:  69, title:  'hello, world!!', checked: false },
    { value: 187, title:  'fuck the world', checked: false },
    { value: 666, title: 'fuck everything', checked: false },
  ],
}),
computed: {
  sum() {
    return this.items.reduce((acc, n) => acc + n.value * n.checked, 0);
  },
  checkAll: {
    get() {
      return this.items.every(n => n.checked);
    },
    set(val) {
      this.items.forEach(n => n.checked = val);
    },
  },
},

<div>
  <label>
    <input type="checkbox" v-model="checkAll">
    CHECK ALL
  </label>
</div>

<div v-for="n in items">
  <label>
    <input type="checkbox" v-model="n.checked">
    {{ n.title }}
  </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