0

When the user selects a machine, I get the id of the machine. From that id I need to get the make and model of the machine.

    <ul class="list-unstyled">
          <li>
              <div v-for="dosimeter in dosimeters">
                 <label>
                    <input type="radio" name="optDosimeter" v-model="dosimeter_id" :value="dosimeter.id" v-on:click="dosimeter_select">{{dosimeter.nickname}}
                 </label>
              </div>
           </li>
     </ul>

Vue.js

    export default {
        data: function() {
            return {
              dosimeters:[],
              dosimeter_id:''
       }
     },
     mounted(){
        axios.get('/dosimeters').then((response) => {
                this.dosimeters=response.data;
            });
     },
     methods: {
         dosimeter_select(){
                not sure what to put here
            }
     }
  }

4 Answers 4

1

i think you should do something like :

  dosimeter_select(){
          let found= this.dosimeters.find(d=>{
                return d.id==this.dosimeter_id
            });

         console.log(found.make);
         console.log(found.model);
        }
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your suggestion and it works but it takes two clicks. The first click says 'undefined' and then selecting another radio button I get the make and model as expected. Any ideas?
try to use on:input event instead of click like <input type="radio" name="optDosimeter" v-model="dosimeter_id" :value="dosimeter.id" v-on:input="dosimeter_select">{{dosimeter.nickname}}
That didn't do it. It seems like the method is stopping on return.
0

try this

dosimeter_select()
{
 let machine =  this.dosimeters.find(dosimeter => dosimeters.id===this.dosimeter_id);
  // now you have the machine object
}

1 Comment

Would this be the proper way to access the object? this.dosimeter_model = machine.model;
0

it should work:

new Vue({
  el: "#app",
  data: {
    dosimeters:[
      { id: 1, name: 'foo' },
      { id: 2, name: 'bar' }
    ]
  },
  methods: {
  	dosimeterSelected (selectedDosimeter) {
    	console.log(`i doing something with dosimeter.id: ${selectedDosimeter.id}`)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <ul class="list-unstyled">
    <li v-for="dosimeter in dosimeters">
      <div>
        <label>
          <input type="radio" name="optDosimeter" :value="dosimeter.name" v-on:click="dosimeterSelected(dosimeter)">{{dosimeter.name}}
        </label>
      </div>
    </li>
  </ul>  
</div>

Comments

0

I made some changes now it works. I used the suggestion from Boussadjra Brahim:

dosimeter_select(){
      let found= this.dosimeters.find(d=>{
            return d.id==this.dosimeter_id
        });

     console.log(found.make);
     console.log(found.model);
    }

and rather than using radio buttons I made it a select drop down. Also, rather than triggering the method with @click, I used @change.

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.