2

Here is the complete example first: https://jsfiddle.net/3bzzpajh/

What I am trying to achieve is, to pass the whole person object to my method showSelectedDat, for some reason attaching the person object to a data attribute like: :data-person="person" changes the object to something like [object Object] which becomes useless inside my method:

<div id="app">
  <select name="" id="" @change="showSelectedData($event)" >
    <option :value="person.name" :data-name="person.name"  v-for="person in people[0]"> {{ person.name }}</option>
  </select>
</div>

As shown in the above code, I am currently "passing person" name like :data-name="person.name" but this becomes cumbersome when person has many properties.

This is where my vuejs application:

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

      people: [{
        '1': {
          'code': 1010,
          'name': 'sam',
          'status': 'ACTIVE',
          'class': 'RED',
          'currencyCode': 'CHF'
        },
        '2': {
          'code': 1210,
          'name': 'jane',
          'status': 'ACTIVE',
          'class': 'WHiTE',
          'currencyCode': 'NA'
        },
        '3': {
          'code': 7777,
          'name': 'luis',
          'status': 'ACTIVE',
          'class': 'BLUE',
          'currencyCode': 'DE'
        },
        '4': {
          'code': 443,
          'name': 'dave',
          'status': 'ACTIVE',
          'class': 'GREEN',
          'currencyCode': 'FR'
        }
      }]

    }
  },
  methods: {
    showSelectedData: function (event) {
      console.log(event.target.selectedOptions[0].dataset.name)
    }
  }
})

So, to recape, how do I get person inside showSelectedData when a dropdown is selected?

2
  • Why not use v-model instead of change? Commented Sep 21, 2017 at 19:53
  • What do you mean? I am a beginner, but I don't think I can use v-model at least not with onchange Commented Sep 21, 2017 at 19:55

2 Answers 2

3

This is typically done in Vue by binding a data element to the select using v-model.

Define a data element called selectedPerson.

data:{
  selectedPerson: null,
  ...
}

Reference that using v-model.

<select v-model="selectedPerson">

Use the person as the value in the options.

<option :value="person" v-for="person in people[0]"> {{ person.name }}</option>

Now, whenever a person is selected, selectedPerson will be the entire person. You don't need to use the change event at all, you don't need to bind it to data, and you don't need to look for it by index. When you need the selected person, just reference the data value.

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

Comments

1

You could store the selected object key with v-model and get the person object later with the saved key. Of course you could use any other identification, for example you could save the object ID if it actually has one, and later on just find the object by that ID.

<div id="app">
  <select name="" id="" @change="showSelectedData($event)" v-model="current_person_key">
    <option :value="index" :data-name="person.name"  v-for="(person, index) in people[0]"> {{ person.name }}</option>
  </select>
</div>

new Vue({
  el: '#app',
  data () {
    return {
      current_person_key: -1,
      people: [{
        '1': {
          'code': 1010,
          'name': 'sam',
          'status': 'ACTIVE',
          'class': 'RED',
          'currencyCode': 'CHF'
        },
        '2': {
          'code': 1210,
          'name': 'jane',
          'status': 'ACTIVE',
          'class': 'WHiTE',
          'currencyCode': 'NA'
        },
        '3': {
          'code': 7777,
          'name': 'luis',
          'status': 'ACTIVE',
          'class': 'BLUE',
          'currencyCode': 'DE'
        },
        '4': {
          'code': 443,
          'name': 'dave',
          'status': 'ACTIVE',
          'class': 'GREEN',
          'currencyCode': 'FR'
        }
      }]

    }
  },
  methods: {
    showSelectedData: function (event) {
        var person = this.people[0][this.current_person_key];
        console.log(person)
    }
  }
})

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.