2

I have two components one is User component and inside the user component there is edit user component(Modal box), so my problem is how to pass the user object to edit user component after click edit user button.

User view

<div>
    <v-layout row wrap>
        <v-flex sm3 xs2 v-for="user in users">
            <v-card>
                <v-card-media src="https://vuetifyjs.com/static/doc-images/cards/plane.jpg"
                              height="200px">
                </v-card-media>
                <v-card-title primary-title>
                    <div>
                        <h3 class="headline mb-0">{{user.name}}</h3>
                        <div><b>Email : </b>{{user.email}}</div>
                    </div>
                </v-card-title>
                <v-card-actions>
                    <v-spacer></v-spacer>
                    <v-btn primary dark @click="editUser(user)">Edit</v-btn>
                    <v-btn error dark>View</v-btn>
                </v-card-actions>
            </v-card>
        </v-flex>
    </v-layout>

    <edit-modal></edit-modal>

</div>

edit modal

<v-layout row justify-center>
    <v-dialog persistent width="30%">
        <v-card>
            <v-card-title>
                <span class="headline">User Profile</span>
            </v-card-title>
            <v-card-text>
                <v-container grid-list-md>
                    <v-layout wrap>
                        <v-flex xs12 sm6 md12>
                            <v-text-field label="Legal first name" required></v-text-field>
                        </v-flex>
                        <v-flex xs12 md6>
                            <v-text-field label="Email" required ></v-text-field>
                        </v-flex>
                    </v-layout>
                </v-container>
                <small>*indicates required field</small>
            </v-card-text>
            <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn light @click.native="dialog">Close</v-btn>
                <v-btn primary @click.native="saveUser">Save</v-btn>
            </v-card-actions>
        </v-card>
    </v-dialog>
</v-layout>

Screen shot

2 Answers 2

2

What you can do is initially define a empty user model in User component which is just like the dummy object and pass it as a props to edit modal component and use v-if to hide and show the model:

<edit-modal v-if="isShowModel" :userObj="userObj"></edit-modal>

In script part:

data() {
   return {
      userObj: {
          name: '',
          email: ''
      },
      isShowModel: false
   }
},
methods: {
  editUser(user) {
    this.userObj.name = user.name;
    this.userObj.email = user.email;
    this.isShowModel = true;
  }
}

In edit component access it with props:

props:['userObj'],
data() {
  return {
    newUserObj: Object.assign({}, this.userObj),
  }
}

This will work. But there is another method you can use $refs also Give a ref attribute to the edit modal:

<edit-modal ref="editme"></edit-modal>

Then in script part access this ref:

methods: {
  editUser(user) {
    this.$refs.editme.name = user.name;
    this.$refs.editme.email = user.email;
  }
}

In edit component define name and email in data()

data() {
    return {
       name: '',
       email: ''
    }
}

Now to update those edited values in parent component you can use $emit:

<v-btn primary @click.native="saveUser">Save</v-btn>

in script:

methods: {
  saveUser() {
    this.$emit('onEditValue', this.name, this.email);
  }
}

In parent component:

<edit-modal
  ref="editme"
  @onEditValue="editCall"
></edit-modal>

In script:

methods: {
  editCall(name, email) {
    this.user.name = name;
    this.user.email = email;
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Its working fine. but not met the my requirement . I want to avoid parent data change.. ( I change the value from modal box real time update parent view) watch: { edit: function (nv, ov) { this.func = nv } },
ok so you want to pass those updated values to parent component also. gotcha, I will update my code for it.
No. I dont want to send edited data to parent. i want to avoid parent data changes. check the image i have attached
In that case in you child data() made a copy of the props passed from the parent userObj: Object.assign({}, this.userObjFromProps), this will do the trick
Updated my answer accordingly
|
0

If the two components are siblings, you can use Event Bus provided by Vuejs

Reference Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.