Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Vue.js post checkbox empty data or full data

I am trying to make a checkbox with vue js, so that the method is not written. I want the checkbox to be false in the first place, the next step is that when the checkbox is posted, I want "opening_balance" to be sent as an empty array. or vice versa, if the checkbox is checked, I want it to appear in "opening_balance" when the data is posted.

For example: POST No Checkbox

opening_balance: {}

POST Checked

opening_balance: {date: "2021-08-30", net_total: "1000.00", currency: "USD"}

new Vue({
    el: '#app',
    data() {
        return {
            attributes: {
                opening_balance: {
                    net_total: 0,
                    date: new Date(),
                    currency: USD,
                },
            }
        }
    }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.js"></script>

<div id="app" style="padding: 1rem;">
<div class="d-flex">
    <input class="form-check-input" type="checkbox" value="false" @click="attributes.opening_balance" v-model="attributes.opening_balance">
    <label class="form-check-label"><b> opening balance</b></label>
</div>
<div v-if="attributes.opening_balance">
    <input type="text" id="Detaylari" class="form-control" v-model="attributes.opening_balance.date">
    <input type="text" class="form-control" v-model="attributes.opening_balance.net_total">
    <input type="text" class="form-control" v-model="attributes.opening_balance.currency">
</div>
</div>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If i understand you correctly please look at snippet bellow:

const app = new Vue({
  el: '#app',
  data() {
    return {
      attributes: {
        opening_balance: {
          
        },
      }
    }
  },
  methods: {
    setBalance(e) {
      if(!e.target.checked) {
        this.attributes.opening_balance= {}
      } 
    }
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app" style="padding: 1rem;">
<div class="d-flex">
    <input class="form-check-input" type="checkbox" @click="setBalance">
    <label class="form-check-label"><b> opening balance</b></label>
</div>
<div v-if="attributes.opening_balance">
    <input type="text" id="Detaylari" class="form-control" v-model="attributes.opening_balance.date">
    <input type="text" class="form-control" v-model="attributes.opening_balance.net_total">
    <input type="text" class="form-control" v-model="attributes.opening_balance.currency">
    
    <p>{{ attributes.opening_balance }}</p>
</div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can use a watcher to set a empty object when checked is false.

new Vue({
  el: '#app',
  watch: {
    isPosted: function(val) {
      // on posted unchecked set empty object
      if (!val) {
        this.attributes.opening_balance = {};
      } else {
        this.$set(this.attributes.opening_balance, "date", "2021-08-30");
        this.$set(this.attributes.opening_balance, "net_total", "1000.00");
        this.$set(this.attributes.opening_balance, "currency", "USD");
      }
    },
  },
  data() {
    return {
      isPosted: false,
      attributes: {
        opening_balance: {},
      }
    }
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.js"></script>

<div id="app" style="padding: 1rem;">
  <div class="d-flex">
    <input class="form-check-input" type="checkbox" v-model="isPosted">
    <label class="form-check-label"><b> opening balance</b></label>
  </div>
  <div v-if="isPosted">
    <input type="text" id="Detaylari" class="form-control" v-model="attributes.opening_balance.date">
    <input type="text" class="form-control" v-model="attributes.opening_balance.net_total">
    <input type="text" class="form-control" v-model="attributes.opening_balance.currency">
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!