1

I'm trying to create a form where users edit their comments. The input should display the current value of the comment (which is dynamic data coming from a v-for), but I also need to bind the value to a model so that I may send it with my patch request.

Currently the form looks like this

<p @click="editing = true">
     Edit comment
</p>
<form v-if="editing" @submit.prevent="editComment(comment._id)">
   <input type="text" v-model="comment.message"/>
   <button type="submit">
     Edit Comment
   </button>
 </form>

Would there be any way to combine the v-model and :value binding? Or a way to bind the model value aswell and setting the name of the data?

1

1 Answer 1

1

Assuming every comment is rendered within it's own comment, this would be an approach you can use (not tested):

Parent component:

<template>
  <div>
    <Comment 
      v-for="comment in comments"
      :key="comment._id"
      :comment="comment"
      @on-update="onUpdateComment"
    />
  </div>
</template>

<script>
  export default {
    data () {
      return {
        comments: [] // Could be also a computed property, or you populate the array somewhere in this component
      }
    },

    methods: {
      onUpdateComment({ commentId, value }) {
        // Perform update
      }
    }
  }
</script>

Child component (Comment):

<template>
  <div>
    {{ comment.message }}

    <p @click="onClickEdit">
        Edit comment
    </p>

    <form v-if="editing" @submit.prevent="editComment">
      <input type="text" :value="updatedCommentMessage"/>

      <button type="submit">
        Edit Comment
      </button>
    </form>
  </div>
</template>

<script>
  export default {
    props: {
      comment: {
        type: Object
      }
    },

    data () {
      return {
        editing: false,
        updatedCommentMessage: ''
      }
    }

    methods: {
      onClickEdit () {
        this.editing = true
        this.updatedCommentMessage = this.comment.message
      },

      editComment () {
        this.$emit('on-update', { commentId: this.comment.id, value: this.updatedCommentMessage})
      }
    }
  }
</script>

The benefit of storing the message bound to the input in it's own variable is, that you easily can cancel the action, by just resetting the updatedCommentMessage variable.

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

4 Comments

This returns error "TypeError: Cannot read properties of undefined (reading 'message')" when the updatedCommentMessage is set. However I don't see how the comment prop will be able to access the dynamic data
I've updated my post, so you better see what I mean. The parent component holds the actual comments, which gets passed down to the child component. If any update occurs, the parent component needs to get notified
I see. This was not the structure I've been doing it in though. I have been having the Post and the Comments in the same component, but I guess splitting them up will be the best way to make this action work
I would suggest to do so. It's way easier to maintain. However, it should also work with having everything in the same component. It's just a bit trickier, because you need to track, which comment currently gets updated (and therefore you need take care of setting and resetting values)

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.