Handling checkboxes in VueJS












-1















I have been working with jQuery in past now I switched to Vuejs. I am unable to find a solution to these problems in Vuejs, I am newbie to vuejs.





  1. I had a range of checkboxes in a table. and I have added a snippet to select multiple checkboxes with shift + click. This way I was able to select many checkboxes at once..



    $chkboxes = $('.chkbox')
    lastChecked = null
    $chkboxes.click (e) ->

    if !lastChecked
    lastChecked = this
    return

    if e.shiftKey
    start = $chkboxes.index(this)
    end = $chkboxes.index(lastChecked)
    $chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1).prop 'checked', lastChecked.checked
    lastChecked = this
    return



  2. on Click I can get the values from checkbox which I have added as data attrs



    $("#cameras_datatables tbody input[type='checkbox']:checked").each (index, control) ->
    camera_id = $(control).attr("data-val-id")
    api_id = $(control).attr("data-val-api-id")
    api_key = $(control).attr("data-val-api_key")
    row = $(this).parents('tr')



Now I have a button such as



<button v-on:click="onModifyClick" class="clear-btn-f" id="btn-modify">Modify</button>


and in methods



  onModifyClick () {
console.log("testing");
this.$notify({
group: "admins",
title: "Please",
type: "error",
text: "At least select one user!",
});
}


on Modify Click I want to get all those checkboxes which are checked on page.. and then I want to get the values which I have been added as attrs



<input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd">


I want to loop through all checked checkboxes and get those data attrs,



This is more like an Informatory question because I have searched but cannot find any solutions about this.



UPDATE:



I am using Vuetable, And I am getting all those values through Vuetable loading in a callback formatter



export default [
{
title: ``,
formatter: (value) => {
return "<input type='checkbox' data-val='" + value.id + "' data-val-username='" + value.username + "' data-val-api-id='" + value.api_id + "' data-val-api_key='" + value.api_key + "'>";
},
titleClass: "user-checkbox"
}
]


Now, I see no way. to do what you are suggesting putting that into data and let Vue attach it to dom through the template. whereas the data is already getting bind to Dom through Vuetable.



After that I only have this



enter image description here










share|improve this question

























  • thats nice when you cannot answer just give it a -1 :) my questions are valid. do anyone know how you can select multiple checkboxes?

    – Junaid Farooq
    Jan 21 at 6:39
















-1















I have been working with jQuery in past now I switched to Vuejs. I am unable to find a solution to these problems in Vuejs, I am newbie to vuejs.





  1. I had a range of checkboxes in a table. and I have added a snippet to select multiple checkboxes with shift + click. This way I was able to select many checkboxes at once..



    $chkboxes = $('.chkbox')
    lastChecked = null
    $chkboxes.click (e) ->

    if !lastChecked
    lastChecked = this
    return

    if e.shiftKey
    start = $chkboxes.index(this)
    end = $chkboxes.index(lastChecked)
    $chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1).prop 'checked', lastChecked.checked
    lastChecked = this
    return



  2. on Click I can get the values from checkbox which I have added as data attrs



    $("#cameras_datatables tbody input[type='checkbox']:checked").each (index, control) ->
    camera_id = $(control).attr("data-val-id")
    api_id = $(control).attr("data-val-api-id")
    api_key = $(control).attr("data-val-api_key")
    row = $(this).parents('tr')



Now I have a button such as



<button v-on:click="onModifyClick" class="clear-btn-f" id="btn-modify">Modify</button>


and in methods



  onModifyClick () {
console.log("testing");
this.$notify({
group: "admins",
title: "Please",
type: "error",
text: "At least select one user!",
});
}


on Modify Click I want to get all those checkboxes which are checked on page.. and then I want to get the values which I have been added as attrs



<input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd">


I want to loop through all checked checkboxes and get those data attrs,



This is more like an Informatory question because I have searched but cannot find any solutions about this.



UPDATE:



I am using Vuetable, And I am getting all those values through Vuetable loading in a callback formatter



export default [
{
title: ``,
formatter: (value) => {
return "<input type='checkbox' data-val='" + value.id + "' data-val-username='" + value.username + "' data-val-api-id='" + value.api_id + "' data-val-api_key='" + value.api_key + "'>";
},
titleClass: "user-checkbox"
}
]


Now, I see no way. to do what you are suggesting putting that into data and let Vue attach it to dom through the template. whereas the data is already getting bind to Dom through Vuetable.



After that I only have this



enter image description here










share|improve this question

























  • thats nice when you cannot answer just give it a -1 :) my questions are valid. do anyone know how you can select multiple checkboxes?

    – Junaid Farooq
    Jan 21 at 6:39














-1












-1








-1








I have been working with jQuery in past now I switched to Vuejs. I am unable to find a solution to these problems in Vuejs, I am newbie to vuejs.





  1. I had a range of checkboxes in a table. and I have added a snippet to select multiple checkboxes with shift + click. This way I was able to select many checkboxes at once..



    $chkboxes = $('.chkbox')
    lastChecked = null
    $chkboxes.click (e) ->

    if !lastChecked
    lastChecked = this
    return

    if e.shiftKey
    start = $chkboxes.index(this)
    end = $chkboxes.index(lastChecked)
    $chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1).prop 'checked', lastChecked.checked
    lastChecked = this
    return



  2. on Click I can get the values from checkbox which I have added as data attrs



    $("#cameras_datatables tbody input[type='checkbox']:checked").each (index, control) ->
    camera_id = $(control).attr("data-val-id")
    api_id = $(control).attr("data-val-api-id")
    api_key = $(control).attr("data-val-api_key")
    row = $(this).parents('tr')



Now I have a button such as



<button v-on:click="onModifyClick" class="clear-btn-f" id="btn-modify">Modify</button>


and in methods



  onModifyClick () {
console.log("testing");
this.$notify({
group: "admins",
title: "Please",
type: "error",
text: "At least select one user!",
});
}


on Modify Click I want to get all those checkboxes which are checked on page.. and then I want to get the values which I have been added as attrs



<input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd">


I want to loop through all checked checkboxes and get those data attrs,



This is more like an Informatory question because I have searched but cannot find any solutions about this.



UPDATE:



I am using Vuetable, And I am getting all those values through Vuetable loading in a callback formatter



export default [
{
title: ``,
formatter: (value) => {
return "<input type='checkbox' data-val='" + value.id + "' data-val-username='" + value.username + "' data-val-api-id='" + value.api_id + "' data-val-api_key='" + value.api_key + "'>";
},
titleClass: "user-checkbox"
}
]


Now, I see no way. to do what you are suggesting putting that into data and let Vue attach it to dom through the template. whereas the data is already getting bind to Dom through Vuetable.



After that I only have this



enter image description here










share|improve this question
















I have been working with jQuery in past now I switched to Vuejs. I am unable to find a solution to these problems in Vuejs, I am newbie to vuejs.





  1. I had a range of checkboxes in a table. and I have added a snippet to select multiple checkboxes with shift + click. This way I was able to select many checkboxes at once..



    $chkboxes = $('.chkbox')
    lastChecked = null
    $chkboxes.click (e) ->

    if !lastChecked
    lastChecked = this
    return

    if e.shiftKey
    start = $chkboxes.index(this)
    end = $chkboxes.index(lastChecked)
    $chkboxes.slice(Math.min(start, end), Math.max(start, end) + 1).prop 'checked', lastChecked.checked
    lastChecked = this
    return



  2. on Click I can get the values from checkbox which I have added as data attrs



    $("#cameras_datatables tbody input[type='checkbox']:checked").each (index, control) ->
    camera_id = $(control).attr("data-val-id")
    api_id = $(control).attr("data-val-api-id")
    api_key = $(control).attr("data-val-api_key")
    row = $(this).parents('tr')



Now I have a button such as



<button v-on:click="onModifyClick" class="clear-btn-f" id="btn-modify">Modify</button>


and in methods



  onModifyClick () {
console.log("testing");
this.$notify({
group: "admins",
title: "Please",
type: "error",
text: "At least select one user!",
});
}


on Modify Click I want to get all those checkboxes which are checked on page.. and then I want to get the values which I have been added as attrs



<input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd">


I want to loop through all checked checkboxes and get those data attrs,



This is more like an Informatory question because I have searched but cannot find any solutions about this.



UPDATE:



I am using Vuetable, And I am getting all those values through Vuetable loading in a callback formatter



export default [
{
title: ``,
formatter: (value) => {
return "<input type='checkbox' data-val='" + value.id + "' data-val-username='" + value.username + "' data-val-api-id='" + value.api_id + "' data-val-api_key='" + value.api_key + "'>";
},
titleClass: "user-checkbox"
}
]


Now, I see no way. to do what you are suggesting putting that into data and let Vue attach it to dom through the template. whereas the data is already getting bind to Dom through Vuetable.



After that I only have this



enter image description here







javascript vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 20:48









marc_s

575k12811101257




575k12811101257










asked Jan 19 at 13:53









Junaid FarooqJunaid Farooq

6361723




6361723













  • thats nice when you cannot answer just give it a -1 :) my questions are valid. do anyone know how you can select multiple checkboxes?

    – Junaid Farooq
    Jan 21 at 6:39



















  • thats nice when you cannot answer just give it a -1 :) my questions are valid. do anyone know how you can select multiple checkboxes?

    – Junaid Farooq
    Jan 21 at 6:39

















thats nice when you cannot answer just give it a -1 :) my questions are valid. do anyone know how you can select multiple checkboxes?

– Junaid Farooq
Jan 21 at 6:39





thats nice when you cannot answer just give it a -1 :) my questions are valid. do anyone know how you can select multiple checkboxes?

– Junaid Farooq
Jan 21 at 6:39












2 Answers
2






active

oldest

votes


















3














As is very common for new SPA framework users, you're doing this exactly backwards. You'll need to get out of the habit of touching the DOM directly when using Vue -- instead of thinking in terms of "I have these checkboxes, I need to read the values from them" it should be "I have these state variables in my component, and vue will automatically reflect those values inside the DOM." Manipulate the component data and let Vue worry about the DOM.



So, for example, instead of using jQuery to search the DOM for checkbox elements and read from / write to their attributes, use Vue to store those values and let its template draw them into the DOM, as in this simplified example:






new Vue({
el: '#app',
data: {
myCheckboxes: [{
val: 11172,
username: 'junaid@evercam.io',
apiId: 'dddd',
apiKey: 'xxxx',
checked: false
},
{
val: 123,
username: 'foo@example.com',
apiId: 'dddd',
apiKey: 'xxxx',
checked: true
},
{
val: 456,
username: 'bar@example.com',
apiId: 'dddd',
apiKey: 'xxxx',
checked: true
}
]
},
methods: {
onModifyClick() {
// Examine the data, not the DOM:
var ret = this.myCheckboxes.map(x => {
if (x.checked) return x.val
}).filter(x => {return x})
console.log("Values of checked items: ", ret)
}
}
});

<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<div id="app">
<template v-for="box in myCheckboxes">
<input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
</template>
</div>





As you can see, most of what you were trying to do manually with jQuery now happens automatically -- at any time you can just check the contents of the component's data to see the state of the "myCheckboxes" list, you never read anything from the DOM, and you never write to the DOM directly, just change the contents of the data and let Vue handle the rest.



In frameworks like Vue, React, or Angular, the DOM is a side effect, not the source of truth as you're used to in jQuery code. I'd strongly recommend not using jQuery alongside Vue, at least until you understand the framework well enough to know when it's safe and necessary to do so (not often).






share|improve this answer
























  • your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

    – Junaid Farooq
    Jan 19 at 15:30











  • It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

    – Daniel Beck
    Jan 19 at 15:51













  • Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

    – Junaid Farooq
    Jan 19 at 15:57











  • I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

    – Junaid Farooq
    Jan 19 at 16:26











  • Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

    – Daniel Beck
    Jan 19 at 17:02



















0














You could take advantage of getAttribute element function, which return the value of the attribute passed as parameter, so we add method handler of change event which takes the event as parameter and we could access target property that represents the current element (check box input) by calling the getAttribute method as follows:






new Vue({
el: '#app',
data: {

},
methods: {
onModifyClick(e) {
let t=e.target;
console.log(t.getAttribute('data-val'));
console.log(t.getAttribute('data-val-username'));
console.log(t.getAttribute('data-val-api-id'));
console.log(t.getAttribute('data-val-api_key'));
}
}
});

<script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
<div id="app">

<input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

<input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

</div>








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267798%2fhandling-checkboxes-in-vuejs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    As is very common for new SPA framework users, you're doing this exactly backwards. You'll need to get out of the habit of touching the DOM directly when using Vue -- instead of thinking in terms of "I have these checkboxes, I need to read the values from them" it should be "I have these state variables in my component, and vue will automatically reflect those values inside the DOM." Manipulate the component data and let Vue worry about the DOM.



    So, for example, instead of using jQuery to search the DOM for checkbox elements and read from / write to their attributes, use Vue to store those values and let its template draw them into the DOM, as in this simplified example:






    new Vue({
    el: '#app',
    data: {
    myCheckboxes: [{
    val: 11172,
    username: 'junaid@evercam.io',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: false
    },
    {
    val: 123,
    username: 'foo@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    },
    {
    val: 456,
    username: 'bar@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    }
    ]
    },
    methods: {
    onModifyClick() {
    // Examine the data, not the DOM:
    var ret = this.myCheckboxes.map(x => {
    if (x.checked) return x.val
    }).filter(x => {return x})
    console.log("Values of checked items: ", ret)
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">
    <template v-for="box in myCheckboxes">
    <input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
    </template>
    </div>





    As you can see, most of what you were trying to do manually with jQuery now happens automatically -- at any time you can just check the contents of the component's data to see the state of the "myCheckboxes" list, you never read anything from the DOM, and you never write to the DOM directly, just change the contents of the data and let Vue handle the rest.



    In frameworks like Vue, React, or Angular, the DOM is a side effect, not the source of truth as you're used to in jQuery code. I'd strongly recommend not using jQuery alongside Vue, at least until you understand the framework well enough to know when it's safe and necessary to do so (not often).






    share|improve this answer
























    • your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

      – Junaid Farooq
      Jan 19 at 15:30











    • It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

      – Daniel Beck
      Jan 19 at 15:51













    • Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

      – Junaid Farooq
      Jan 19 at 15:57











    • I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

      – Junaid Farooq
      Jan 19 at 16:26











    • Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

      – Daniel Beck
      Jan 19 at 17:02
















    3














    As is very common for new SPA framework users, you're doing this exactly backwards. You'll need to get out of the habit of touching the DOM directly when using Vue -- instead of thinking in terms of "I have these checkboxes, I need to read the values from them" it should be "I have these state variables in my component, and vue will automatically reflect those values inside the DOM." Manipulate the component data and let Vue worry about the DOM.



    So, for example, instead of using jQuery to search the DOM for checkbox elements and read from / write to their attributes, use Vue to store those values and let its template draw them into the DOM, as in this simplified example:






    new Vue({
    el: '#app',
    data: {
    myCheckboxes: [{
    val: 11172,
    username: 'junaid@evercam.io',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: false
    },
    {
    val: 123,
    username: 'foo@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    },
    {
    val: 456,
    username: 'bar@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    }
    ]
    },
    methods: {
    onModifyClick() {
    // Examine the data, not the DOM:
    var ret = this.myCheckboxes.map(x => {
    if (x.checked) return x.val
    }).filter(x => {return x})
    console.log("Values of checked items: ", ret)
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">
    <template v-for="box in myCheckboxes">
    <input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
    </template>
    </div>





    As you can see, most of what you were trying to do manually with jQuery now happens automatically -- at any time you can just check the contents of the component's data to see the state of the "myCheckboxes" list, you never read anything from the DOM, and you never write to the DOM directly, just change the contents of the data and let Vue handle the rest.



    In frameworks like Vue, React, or Angular, the DOM is a side effect, not the source of truth as you're used to in jQuery code. I'd strongly recommend not using jQuery alongside Vue, at least until you understand the framework well enough to know when it's safe and necessary to do so (not often).






    share|improve this answer
























    • your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

      – Junaid Farooq
      Jan 19 at 15:30











    • It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

      – Daniel Beck
      Jan 19 at 15:51













    • Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

      – Junaid Farooq
      Jan 19 at 15:57











    • I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

      – Junaid Farooq
      Jan 19 at 16:26











    • Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

      – Daniel Beck
      Jan 19 at 17:02














    3












    3








    3







    As is very common for new SPA framework users, you're doing this exactly backwards. You'll need to get out of the habit of touching the DOM directly when using Vue -- instead of thinking in terms of "I have these checkboxes, I need to read the values from them" it should be "I have these state variables in my component, and vue will automatically reflect those values inside the DOM." Manipulate the component data and let Vue worry about the DOM.



    So, for example, instead of using jQuery to search the DOM for checkbox elements and read from / write to their attributes, use Vue to store those values and let its template draw them into the DOM, as in this simplified example:






    new Vue({
    el: '#app',
    data: {
    myCheckboxes: [{
    val: 11172,
    username: 'junaid@evercam.io',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: false
    },
    {
    val: 123,
    username: 'foo@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    },
    {
    val: 456,
    username: 'bar@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    }
    ]
    },
    methods: {
    onModifyClick() {
    // Examine the data, not the DOM:
    var ret = this.myCheckboxes.map(x => {
    if (x.checked) return x.val
    }).filter(x => {return x})
    console.log("Values of checked items: ", ret)
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">
    <template v-for="box in myCheckboxes">
    <input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
    </template>
    </div>





    As you can see, most of what you were trying to do manually with jQuery now happens automatically -- at any time you can just check the contents of the component's data to see the state of the "myCheckboxes" list, you never read anything from the DOM, and you never write to the DOM directly, just change the contents of the data and let Vue handle the rest.



    In frameworks like Vue, React, or Angular, the DOM is a side effect, not the source of truth as you're used to in jQuery code. I'd strongly recommend not using jQuery alongside Vue, at least until you understand the framework well enough to know when it's safe and necessary to do so (not often).






    share|improve this answer













    As is very common for new SPA framework users, you're doing this exactly backwards. You'll need to get out of the habit of touching the DOM directly when using Vue -- instead of thinking in terms of "I have these checkboxes, I need to read the values from them" it should be "I have these state variables in my component, and vue will automatically reflect those values inside the DOM." Manipulate the component data and let Vue worry about the DOM.



    So, for example, instead of using jQuery to search the DOM for checkbox elements and read from / write to their attributes, use Vue to store those values and let its template draw them into the DOM, as in this simplified example:






    new Vue({
    el: '#app',
    data: {
    myCheckboxes: [{
    val: 11172,
    username: 'junaid@evercam.io',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: false
    },
    {
    val: 123,
    username: 'foo@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    },
    {
    val: 456,
    username: 'bar@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    }
    ]
    },
    methods: {
    onModifyClick() {
    // Examine the data, not the DOM:
    var ret = this.myCheckboxes.map(x => {
    if (x.checked) return x.val
    }).filter(x => {return x})
    console.log("Values of checked items: ", ret)
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">
    <template v-for="box in myCheckboxes">
    <input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
    </template>
    </div>





    As you can see, most of what you were trying to do manually with jQuery now happens automatically -- at any time you can just check the contents of the component's data to see the state of the "myCheckboxes" list, you never read anything from the DOM, and you never write to the DOM directly, just change the contents of the data and let Vue handle the rest.



    In frameworks like Vue, React, or Angular, the DOM is a side effect, not the source of truth as you're used to in jQuery code. I'd strongly recommend not using jQuery alongside Vue, at least until you understand the framework well enough to know when it's safe and necessary to do so (not often).






    new Vue({
    el: '#app',
    data: {
    myCheckboxes: [{
    val: 11172,
    username: 'junaid@evercam.io',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: false
    },
    {
    val: 123,
    username: 'foo@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    },
    {
    val: 456,
    username: 'bar@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    }
    ]
    },
    methods: {
    onModifyClick() {
    // Examine the data, not the DOM:
    var ret = this.myCheckboxes.map(x => {
    if (x.checked) return x.val
    }).filter(x => {return x})
    console.log("Values of checked items: ", ret)
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">
    <template v-for="box in myCheckboxes">
    <input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
    </template>
    </div>





    new Vue({
    el: '#app',
    data: {
    myCheckboxes: [{
    val: 11172,
    username: 'junaid@evercam.io',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: false
    },
    {
    val: 123,
    username: 'foo@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    },
    {
    val: 456,
    username: 'bar@example.com',
    apiId: 'dddd',
    apiKey: 'xxxx',
    checked: true
    }
    ]
    },
    methods: {
    onModifyClick() {
    // Examine the data, not the DOM:
    var ret = this.myCheckboxes.map(x => {
    if (x.checked) return x.val
    }).filter(x => {return x})
    console.log("Values of checked items: ", ret)
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">
    <template v-for="box in myCheckboxes">
    <input @change="onModifyClick()" type="checkbox" v-model="box.checked" :data-val="box.val" :data-val-username="box.username" :data-val-api-id="box.apiId" :data-val-api_key="box.apiKey"> {{box.username}}<br>
    </template>
    </div>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 19 at 14:37









    Daniel BeckDaniel Beck

    12.9k51642




    12.9k51642













    • your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

      – Junaid Farooq
      Jan 19 at 15:30











    • It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

      – Daniel Beck
      Jan 19 at 15:51













    • Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

      – Junaid Farooq
      Jan 19 at 15:57











    • I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

      – Junaid Farooq
      Jan 19 at 16:26











    • Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

      – Daniel Beck
      Jan 19 at 17:02



















    • your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

      – Junaid Farooq
      Jan 19 at 15:30











    • It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

      – Daniel Beck
      Jan 19 at 15:51













    • Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

      – Junaid Farooq
      Jan 19 at 15:57











    • I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

      – Junaid Farooq
      Jan 19 at 16:26











    • Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

      – Daniel Beck
      Jan 19 at 17:02

















    your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

    – Junaid Farooq
    Jan 19 at 15:30





    your answer is good but there is a slight issue here. I am using vuetable and I am getting those values as a result of data loading in Vuetable.. and I have only one way. Put those values into Dom.. I am explaining in my question again

    – Junaid Farooq
    Jan 19 at 15:30













    It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

    – Daniel Beck
    Jan 19 at 15:51







    It looks like you're misusing vuetable's formatter; instead of using that to output an HTML string, create a component that renders that table cell and its necessary actions in Vue (working from the same data that you're currently feeding to the formatter). If you need specific help with the vuetable library it might be better as a separate question.

    – Daniel Beck
    Jan 19 at 15:51















    Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

    – Junaid Farooq
    Jan 19 at 15:57





    Yeah I think I am on dead end because no one is willing to help, I posted a question about it in past no one answered not even on the repo.

    – Junaid Farooq
    Jan 19 at 15:57













    I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

    – Junaid Farooq
    Jan 19 at 16:26





    I understand what you say but is there any solution for my current situation using vuejs? only? gettting that data?

    – Junaid Farooq
    Jan 19 at 16:26













    Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

    – Daniel Beck
    Jan 19 at 17:02





    Sorry, I'm sure it's not that people aren't willing to help, just that vuetable isn't afaik widely used so people may not know about it. The example repo I linked to above looks pretty straightforward to follow along though, this component shows how to use a sub-component cell.

    – Daniel Beck
    Jan 19 at 17:02













    0














    You could take advantage of getAttribute element function, which return the value of the attribute passed as parameter, so we add method handler of change event which takes the event as parameter and we could access target property that represents the current element (check box input) by calling the getAttribute method as follows:






    new Vue({
    el: '#app',
    data: {

    },
    methods: {
    onModifyClick(e) {
    let t=e.target;
    console.log(t.getAttribute('data-val'));
    console.log(t.getAttribute('data-val-username'));
    console.log(t.getAttribute('data-val-api-id'));
    console.log(t.getAttribute('data-val-api_key'));
    }
    }
    });

    <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
    <div id="app">

    <input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

    <input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

    </div>








    share|improve this answer




























      0














      You could take advantage of getAttribute element function, which return the value of the attribute passed as parameter, so we add method handler of change event which takes the event as parameter and we could access target property that represents the current element (check box input) by calling the getAttribute method as follows:






      new Vue({
      el: '#app',
      data: {

      },
      methods: {
      onModifyClick(e) {
      let t=e.target;
      console.log(t.getAttribute('data-val'));
      console.log(t.getAttribute('data-val-username'));
      console.log(t.getAttribute('data-val-api-id'));
      console.log(t.getAttribute('data-val-api_key'));
      }
      }
      });

      <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
      <div id="app">

      <input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

      <input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

      </div>








      share|improve this answer


























        0












        0








        0







        You could take advantage of getAttribute element function, which return the value of the attribute passed as parameter, so we add method handler of change event which takes the event as parameter and we could access target property that represents the current element (check box input) by calling the getAttribute method as follows:






        new Vue({
        el: '#app',
        data: {

        },
        methods: {
        onModifyClick(e) {
        let t=e.target;
        console.log(t.getAttribute('data-val'));
        console.log(t.getAttribute('data-val-username'));
        console.log(t.getAttribute('data-val-api-id'));
        console.log(t.getAttribute('data-val-api_key'));
        }
        }
        });

        <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
        <div id="app">

        <input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

        <input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

        </div>








        share|improve this answer













        You could take advantage of getAttribute element function, which return the value of the attribute passed as parameter, so we add method handler of change event which takes the event as parameter and we could access target property that represents the current element (check box input) by calling the getAttribute method as follows:






        new Vue({
        el: '#app',
        data: {

        },
        methods: {
        onModifyClick(e) {
        let t=e.target;
        console.log(t.getAttribute('data-val'));
        console.log(t.getAttribute('data-val-username'));
        console.log(t.getAttribute('data-val-api-id'));
        console.log(t.getAttribute('data-val-api_key'));
        }
        }
        });

        <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
        <div id="app">

        <input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

        <input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

        </div>








        new Vue({
        el: '#app',
        data: {

        },
        methods: {
        onModifyClick(e) {
        let t=e.target;
        console.log(t.getAttribute('data-val'));
        console.log(t.getAttribute('data-val-username'));
        console.log(t.getAttribute('data-val-api-id'));
        console.log(t.getAttribute('data-val-api_key'));
        }
        }
        });

        <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
        <div id="app">

        <input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

        <input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

        </div>





        new Vue({
        el: '#app',
        data: {

        },
        methods: {
        onModifyClick(e) {
        let t=e.target;
        console.log(t.getAttribute('data-val'));
        console.log(t.getAttribute('data-val-username'));
        console.log(t.getAttribute('data-val-api-id'));
        console.log(t.getAttribute('data-val-api_key'));
        }
        }
        });

        <script src="https://unpkg.com/vue@latest/dist/vue.min.js"></script>
        <div id="app">

        <input type="checkbox" data-val="11172" data-val-username="junaid@evercam.io" data-val-api-id="dddd" data-val-api_key="dddd" @change="onModifyClick" /> 1

        <input type="checkbox" data-val="54545" data-val-username="brahim@evercam.io" data-val-api-id="eee" data-val-api_key="fdsdf" @change="onModifyClick" /> 2

        </div>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 19 at 18:25









        Boussadjra BrahimBoussadjra Brahim

        7,3203632




        7,3203632






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54267798%2fhandling-checkboxes-in-vuejs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage