Dedy Chaidir
Dedy Chaidir

Reputation: 937

Vue JS : Getting Unique ID and Value of an array of input at the same time

I am new in Vue and still learning using it. I am learning to use the Element UI for Vue UI. Specifically, I'm trying the Input Number Component, for an array of data. Let say I have my data like this:

dataList = [{
    id: 1,
    productName: "ABC",
    qty: 1
}, {
    id: 2,
    productName: "DEF",
    qty: 2
}];

And the element goes like this:

<div v-for="(item, index) in dataList" v-bind:key="item.id">
    <el-input-number v-model="item.qty" @change="handleChange"></el-input-number>    
</div>

And for the script goes like this:

<script type="text/javascript">
    handleChange = function (value) {
        console.log(value); /* I need the item.id also not just the qty value */
    };
</script>

from the function handleChange() I only can get the value of input number, but not the item id that I've assign in the element. How can I get both of that (item.id and value)?

I was expecting a js function like this could work, but it didnt:

handleChange = function(item, value) { /* */ }

I've been trying to google for some answer, but the answer always showing only 1 parameter that i can acquire from change event.

Any help would be appreciated, thank you.

Upvotes: 1

Views: 955

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You could pass your value ($event) as first parameter and the other parameter as the second one :

 <div v-for="(item, index) in dataList" v-bind:key="item.id">
   <el-input-number v-model="item.qty" @change="handleChange($event,item.id)"></el-input-number>    
</div>

Script :

 <script type="text/javascript">
     handleChange = function (value,id) {
        console.log(value,id); 
     };
</script>

Upvotes: 1

Related Questions