0

I'm starting out with vuejs and a vue grid at https://jsfiddle.net/kc11/7fqgavvq/

I want to display the checked row objects in the:

    <pre> {{ selected| json}} </pre>

area of code,

as in the documentation at http://vuejs.org/guide/forms.html#Checkbox in the "Mutiple checkboxes, bound to the same Array:" example

As you can see when I check 1 checkbox, all are selected. Why is this happening? How can I fix this?

2 Answers 2

3

You made a few wrong assumptions in your code (mainly in respect to the scope).

You have your selected array in your main instance, instead of the demo-grid component, where you have your checkboxes:

var demo = new Vue({
    el: '#demo',
    data: {
        searchQuery: '',
        gridColumns: ['name', 'power'],
        gridData: [
            {name: 'Chuck Norris', power: Infinity},
            {name: 'Bruce Lee', power: 9000},
            {name: 'Jackie Chan', power: 7000},
            {name: 'Jet Li', power: 8000}
        ],
        selected: [] // <- This one
    }
})

And there is no selectAll method defined on your demo-grid component either, even though you reference it in your template:

<input @click="selectAll" type="checkbox" v-model="selected" id="{{ entry.name }}" value="{{ entry.name }}"></td>

If you thus pass your selected property into your demo-grid, (and define it in the props), you should be fine:

        <demo-grid
            v-bind:data="gridData"
            v-bind:columns="gridColumns"
            v-bind:filter-key="searchQuery"
            v-bind:selected="selected"> <!-- here -->
        </demo-grid>

And define a selectAll method:

methods: {
    ...
    selectAll: function () {
       ...
    }

Here you can see a working example: https://jsfiddle.net/7fqgavvq/3/

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

4 Comments

Thanks for the detailed answer!
So reading up at vuejs.org/guide/components.html#Passing_Data_with_Props , my guess is that because 'selected' was not passed in from the vue instance to the component it was never initialized and this led to errors?
Yes, because child components don't automatically have access to the properties of their parents (unless they are explicitly passed via props). So your demo-grid was trying to manipulate a selected property that didn't exist (causing errors in the console).
Thanks, that helps a lot.
1

You should add the selected key to the component's data, not to the main instance of vue.

https://jsfiddle.net/7fqgavvq/4/

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.