2

Working on a view that has checkboxes inside a v-for loop:

<ul class="object administrator-checkbox-list">
    <li v-for="module in modules">
        <label v-bind:for="module.id">
            <input type="checkbox" v-model="form.modules" v-bind:value="module.id" v-bind:id="module.id" v-if=>
            <span>@{{ module.name }}</span>
        </label>
    </li>
</ul>

I also have a data of 'form' (using Laravels Spark Form Object) that has access to a 'currentModules' property that consists of the current resources relationship to a modules table.

I am looking to check the checkboxes that exist in the currentModules property.

Example of the 'modules' data used in the v-for the data:

[
    { 
        "id": 1, 
        "name": "Business", 
        "created_at": "2016-11-23 09:57:03", 
        "updated_at": "2016-11-23 09:57:03" 
    },
    { 
        "id": 2, 
        "name": "Houses", 
        "created_at": "2016-11-23 09:57:03", 
        "updated_at": "2016-11-23 09:57:03" 
    }
]

And the data from 'form.currentModules' is the exact same format. How can I check the checkboxs if the module id is in the currentModules using Vue?

2
  • Do you also want to add/remove modules from form.currentModules when the checkbox is clicked? Commented Dec 6, 2016 at 16:21
  • Nope, thats handled elsewhere just need to set them as checked on initial page load. Currently looking to see if a filter will allow me to do this easiest. @asemahle Commented Dec 6, 2016 at 16:23

1 Answer 1

1

To select the boxes, you form.modules to contain the ids of the selected items, i.e.:

data: function () {
  return {
    form: {
      modules: [1, 2]
    },
    // ...
  }
}

Check out this bin to see an example :-)

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.