1

I'm trying to achieve adding different rows for each genres array push. The problem is all the rows are simultaneously added for each genre and if I input the text, it will reflect in all rows. How do I separate the entity of each rows?

Here is my code.

Template

<table class="table">
  <v-btn @click="addGenre()">Add Genre</v-btn>

  <tbody>
    <tr v-for="genre in genres" :key="genre.id">
     <td>

       <v-select/>
       <v-btn @click="addRow()">Add Row</v-btn> // add row for each genre added

       <tr v-for="(row, index) in rows" :key="row.id">
         <td><v-textarea/></td>
       </tr>

     </td>
    </tr>  
  </tbody>
</table>

export default {
  data: ()=> ({
    genres: [],
    rows: [],
  }),

methods: {
  addGenre () {
    this.genres.push({
     genre: '',
    });
  },

  addRow () {
    this.rows.push({
      row: '',
    });
  },
}
2

1 Answer 1

2

If I understand your issue, I think you should just store rows inside of the genre objects.

<tr v-for="(genre, genreIndex) in genres" :key="genre.id">
     <td>
       <v-select/>
       <v-btn @click="addRow(genreIndex)">Add Row</v-btn> // add row for each genre added
       <tr v-for="(row, index) in genre.rows" :key="row.id">
         <td><v-textarea v-model="row.row"/></td>
       </tr>
     </td>
</tr>  
  data: ()=> ({
    genres: [],
  }),
methods: {
  addGenre () {
    this.genres.push({
     genre: '',
     rows: [],
    });
  },

  addRow (genreIndex) {
    this.genres[genreIndex].rows.push({
      row: '',
    });
  },
}
Sign up to request clarification or add additional context in comments.

2 Comments

No problem my dude
May I please have a follow-up question? I'm having errors when adding a row when data are displayed Cannot read property 'push' of undefined" this.genres = response.data since index are not called, unlike when pressing the addGenre() function.

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.