Home » vue » Dynamic V-model name in a v-for loop Vue

Dynamic V-model name in a v-for loop Vue

Posted by: admin November 26, 2021 Leave a comment

Questions:

I am developing an application and I am using Vue and laravel , inside a v-for loop I need error.id in the loop to be bound to the v-model name of the elements, this my code:

 <tbody class="bodycol">
 <tr v-for="error in errors.error" :key="error.id" >

  <td>{{ error.client }}</td>
  <td>{{ error.session }}</td>
  <td>{{ error.info_session }}</td>
  <td>{{ error.UPROC }}</td>
  <td>{{ error.num_session }}</td>
  <td>{{ error.num_exec }}</td>
  <td>{{ error.date_debut }}</td>
  <td>{{ error.heure_debut }}</td>
  <td>{{ error.date_fin }}</td>
  <td>{{ error.heure_fin }}</td>
    <td>{{ error.status }}
    <form @submit="addComment(error.id)" >
    <div class="form-group">
      <input v-model="form.comment[error.id]" type="text" name="comment"
                  class="form-control area" :class="{ 'is-invalid': form.errors.has('comment') }" 
      required>
                  <has-error :form="form" field="comment"></has-error>
      </div>
        </form>
    </td>
</tr>

   data(){
  return{
   comment:[],
    }
     }

the problem is the output of the errors.status is:

[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"the comment is here"]
Answers:

Change your data to this. You are referencing form.comment not comment

export default {
    data() {
        return {
            form: {
                comment: []
            }
        }
    }
}