Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process the emitted event from the component in v-for of Vuejs

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?

JSFiddle Example

<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}
     }
  }
});
like image 635
Andrew Li Avatar asked Jan 27 '23 10:01

Andrew Li


1 Answers

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>

JSFiddle

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

like image 78
Husam Ibrahim Avatar answered May 06 '23 00:05

Husam Ibrahim