Nix
Nix

Reputation: 37

Calling a method into another method in vue

I'm trying to call a method from inside another method in vue.

What I get is an undefined in my console, but what I really want is the id that is called in the getId function

In a whole what I'm tring to do is use the addEvent function to get the checkbox events so that I can get a true or false from it and then send that to the saveCheckbox function and from the saveCheckbox function call the getId function to get the ID of that specific checkbox.

I hope I was able to explain it properly. If it's still unclear please let me know.

This is what I have

<template>
   <div class="card-body">
       <table class="table">
           <thead class="thead-dark">
               <tr>
                   <th scope="col">Active</th>
                    <th scope="col">Title</th>
               </tr>
           </thead>

           <tbody>
               <tr v-for="(category, index) in categories" >
                    <td>
                        <input name="active" type="checkbox" v-model="category.active" @change="getId(category.id)" @click="addEvent">
                    </td>

                    <td>
                        {{ category.title }}
                    </td>
               </tr>
           </tbody>
       </table>
   </div>
</template>

<script>

    export default {

        props: [
            'attributes'

        ],

        data(){
            return {
                categories: this.attributes,
            }
        },

        methods: {
            getId(id){

                console.log(id);

                return id
            },

            saveCheckbox(event){

                console.log(this.getId());

            },

            addEvent ({ type, target }) {
              const event = {
                  type,
                  isCheckbox: target.type === 'checkbox',
                  target: {
                    value: target.value,
                    checked: target.checked
                  }
              }

                this.saveCheckbox(event.target.checked)

            }
        },

        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

Upvotes: 0

Views: 2604

Answers (2)

Nika Kurashvili
Nika Kurashvili

Reputation: 6462

You have to pass argument (Id) to getId method

Upvotes: 1

Jorge Urosa
Jorge Urosa

Reputation: 11

Having a sort overview, you are not passing any Id to the method, and it trys to return that id. so maybe, that is what is not defined ?

The method calling is done well. with the this. keyword before it

Upvotes: 0

Related Questions