2

I have dynamic list of options which comes from api:

       <tr v-for="(option, index) in options">
                <div class="custom-control custom-switch">
                        <input type="checkbox" class="custom-control-input" id="toggle" v-model="option.value" @click="toggleOption(option.id, index)">
                        <label class="custom-control-label" for="toggle">{{ option.value }}</label>
                </div>

Method:

 toggleOption(id, index) {
            let app = this;
            let option = this.options[index];
            app.loading = true;
            option.value = !option.value;
            axios.patch('/apiendoint' + id, option)
                .then(function (resp) {
                    app.loading = false;
                })
                .catch(function (resp) {

                });
        }

When checkbox is clicked all checkboxes changes, if only one item comes from api everything is working. How to make to work it with multiple checkboxes?

4
  • I think you have a typo v-model="options.value" should be v-model="option.value" Commented Jan 18, 2019 at 7:18
  • oops, my bad, but this not solves problem. Commented Jan 18, 2019 at 7:40
  • You are missing <td> inside your <tr>... Commented Jan 18, 2019 at 7:51
  • Can you create a reproducible codepen or like that? It's not quite clear what do you need and what is the problem. Commented Jan 18, 2019 at 7:52

1 Answer 1

4

I created basic working example

new Vue({
  el: '#app',
  data: {
    loading: false,
    options: [
      {id: 1, value: true},
      {id: 2, value: true},
      {id: 3, value: true},
    ]      
  },
  methods: {
    /*
      instead of passing `id` and `index`, just pass `option`
    */
    toggleOption(option) {
      let app = this;
      
      app.loading = true;
      option.value = !option.value;

      // REMOVE NEXT LINE to send ajax
      return;

      axios.patch('/apiendoint/' + option.id, option)
        .then(function (resp) {
          app.loading = false;
        })
        .catch(function (resp) {});
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">

  <table>
    <tr v-for="(option, index) in options">
      <td>
        <div class="custom-control custom-switch">
          <input
            :id="'toggle-'+option.id"
            type="checkbox"
            class="custom-control-input"
            v-model="option.value"
            @click="toggleOption(option)"
          >
          <label class="custom-control-label" :for="'toggle-'+option.id">
            {{ option.value }}
          </label>
        </div>
      </td>

    </tr>
  </table>
</div>

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

1 Comment

that was solutions! Thanks! :-)

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.