0

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>

2 Answers 2

1

You have to pass argument (Id) to getId method

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

1 Comment

Then how would I do that? Can a method have 2 properties coming from different methods? For examplesaveCheckbox(event, id) where event is coming from addEvent and id is coming from getId
0

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

2 Comments

I tried console.log(this.getId(id)), but I just got id is not defined. I don't know how to call both of the properties I need into one method
Can you provide an example of the category object. and what exactcly do you need? it feels like maybe we can rethink the approach of the problem for a easier solution

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.