0

toggleEnable(event) {
  let shouldEnable = event ? event.target.checked : false
  if (typeof event === 'boolean') {
    this.isEnabled = event;
  }
  if (shouldEnable !== this.autoCampaign.enabled) {
    this.updateSettings({
      enabled: shouldEnable
    })
  }
},
<div id="switch-wrapper">
  <span>enabled</span>
  <span class="switch">
  <input name="auto_campaign[enabled]" type="hidden" value="0">
  <input id="enabled" class="switch-input" type="checkbox" value="1"
  name="auto_campaign[enabled]" v-model="isEnabled"
  @change="toggleEnable" />
<label class="switch-paddle enable-button" for="enabled"></label>
</span>
</div>

Issue with the code is The status is displayed always as "enabled" even if the toggle is off. But i need to show a separate message as The status should be "disabled" when the toggle is on OFF position.

I saw that this can be achieved through v-if and v-else. but not sure how to proceed

1 Answer 1

1

The v-if and v-else implementation will be as below.

Please note v-else should be added to the next of v-if or else v-else wont work.

You just need to depend on your v-model value isEnabled. This will automatically be updated when you change the checkbox. You dont need to update it seperately through toggleEnable function

new Vue({
    el: "#app",
    data: {
        isEnabled: false
    },
    methods: {
        toggleEnable(event) {
            // let shouldEnable = event ? event.target.checked : false
            // if (typeof event === 'boolean') {
            //     this.isEnabled = event;
            // }
            // if (shouldEnable !== this.autoCampaign.enabled) {
            //     this.updateSettings({
            //         enabled: shouldEnable
            //     })
            // }
        },
    }
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="app">
    <div id="switch-wrapper">
        <span v-if="isEnabled">enabled</span>
        <span v-else>disabled</span>
        <span class="switch">
            <input name="auto_campaign[enabled]" type="hidden" value="0">
            <input id="enabled" class="switch-input" type="checkbox" value="1" name="auto_campaign[enabled]"
                v-model="isEnabled" @change="toggleEnable" />
            <label class="switch-paddle enable-button" for="enabled"></label>
        </span>
    </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.