Reputation: 1055
I'm trying to process the emitted the event from the component rendered by the v-for
.
For example,
I've made a combobox
component that emits the event when changing the value.
It emits the event by this.$emit('item_change', item);
.
I want to process this event for the corresponded user.
In the below code, I want to change the status
value of the user when changing the value of the combobox
of user
.
It gets item
as parameter when using v-on:item_change="status_change"
example
But it doesn't get the item
as parameter in v-on:item_change="status_change(item , user)"
though combobox
emits the event with item
, and status
of user
keeps the original value.
How could I solve this issue?
<div id="mainapp">
<table>
<thead>
<th>Name</th><th>Status</th>
</thead>
<tbody>
<tr v-for="user in users">
<td>{{user.name}}</td>
<td><combobox v-bind:default="user.status" v-bind:data="status_codes" v-on:item_change="status_change(item, user)"></combobox></td>
</tr>
</tbody>
</table>
</div>
JS code
var combobox = Vue.component('combobox', {
data: function () {
return {
selected_item:{title:'Select', value:-1},
visible:false
}
},
props:['data','default','symbol'],
template: `
<div class="combobox">
<span class="symbol" v-if="!symbol">
<i class="fa fa-chevron-down" aria-hidden="true" ></i>
</span>
<span class="main" v-on:click="toggleVisible">{{selected_item.title}}</span>
<ul class="combodata" v-if="visible">
<li class="item" v-for="item in data" v-on:click="select(item)">{{item.title}}</li>
</ul>
</div>
`,
created:function(){
if(this.data.length>0){
if(this.default == null || this.default == undefined || this.default =='') this.default=0;
this.selected_item = this.data[this.default];
}
},
methods:{
toggleVisible:function(){
this.visible = !this.visible;
},
select:function(item){
if(this.selected_item != item){
this.selected_item= item;
this.$emit('item_change', item);
}
this.visible = false;
}
}
});
var app=new Vue({
el:"#mainapp",
data:{
status_codes:[{title:'Inactive', value:0},{title:'Active', value:1}],
users:[{name:'Andrew', status:1},{name:'Jackson', status:0},{name:'Tom', status:1}]
},
methods:{
status_change:function(item,user){ //This gets only the parameter from the event. How could I pass the additional parameters to this function?
console.log(item,user);
try{
user.status = item.value;
}catch(e){ console.log}
}
}
});
Upvotes: 5
Views: 5773
Reputation: 7187
You need to pass $event
to your status_change
handler instead of item
<div id="mainapp">
<table>
<thead>
<th>Name</th>
<th>Status</th>
</thead>
<tbody>
<tr v-for="user in users">
<td>{{user.name}}</td>
<td>
<combobox v-bind:default="user.status" v-bind:data="status_codes" v-on:item_change="status_change($event, user)"></combobox>
</td>
</tr>
</tbody>
</table>
</div>
See the Vue docs here about event handling: Sometimes we also need to access the original DOM event in an inline statement handler. You can pass it into a method using the special $event variable
Upvotes: 4
Reputation: 20845
Use $event
What you need is actually v-on:item_change="status_change($event , user)"
.
When you do this.$emit('item_change', whatever);
, whatever
will become $event
in the event listener.
https://jsfiddle.net/jacobgoh101/bLsw085r/1/
Upvotes: 4
Reputation: 1754
Try passing parameters to your function like this:
v-on:item_change="status_change(item, user)"
And in your function declaration, specify the parameters:
status_change: function (item, user) {
console.log(item, user);
}
Upvotes: 1