Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
how to delete row with v-for

for that I am filling with a push and also adding an index to each row, what I need is to delete the row.

addTrabajo(state) {
    state.trabajos.push({
        vehicle_id: state.newOrdenTrabajo.vehicle_id,
        km: state.newOrdenTrabajo.km,
        descripcion: state.newOrdenTrabajo.descripcion,
    })
},

enter image description here

OrdenTrabajo.vue

 <table class="table table-hover table-striped mt-3 table-sm text-white bg-dark">
      <thead>
             <tr>
                 <th>#</th>
                 <th>Trabajo</th>
                 <th></th>
             </tr>
      </thead>
      <tbody>
         <tr v-for="(trabajo, index) in trabajos" :key="index">
             <td>{{index + 1}}</td>
             <td>{{ trabajo.descripcion }}</td>
             <td>
                 <a 
                   href="#" 
                   class="btn btn-danger btn-sm"
                   @click.prevent="removeFromTrabajo({ id: index + 1 })"
                   data-toggle="tooltip"
                   data-placement="top"
                   title="Eliminar">
                   <i class="fas fa-ban"></i>
                 </a>                             
              </td>
          </tr>
      </tbody>
 </table>
  

script.js

removeFromTrabajo(state, data) {
    let found = state.trabajos.indexOf(data.id)
    state.trabajos.splice(found)
},

The problem is that when eliminating, the last one is eliminated though it is not the one that's been selected. For example: I select the first one, but last one is being deleted.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You only need to pass the index to the function. In removeFromTrabajo you can directly access the list using this.trabajos, check if the item at it exists, and then use Array#splice to remove the item at this index by passing a second parameter:

deleteCount (Optional): : An integer indicating the number of elements in the array to remove from start. If deleteCount is omitted, or if its value is equal to or larger than array.length - start (that is, if it is equal to or greater than the number of elements left in the array, starting at start), then all the elements from start to the end of the array will be deleted. If deleteCount is 0 or negative, no elements are removed.

new Vue({
  el: "#table",
  data: () => ({
    trabajos: [ { descripcion: '1' }, { descripcion: '2' }, { descripcion: '3' } ]
  }),
  methods: {
    removeFromTrabajo(index) {
      const item = this.trabajos[index];
      if(item) {
        this.trabajos.splice(index, 1);
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>

<table id="table" class="table table-hover table-striped mt-3 table-sm text-white bg-dark">
  <thead>
    <tr><th>#</th> <th>Trabajo</th><th></th></tr>
  </thead>
  <tbody>
    <tr v-for="(trabajo, index) in trabajos" :key="index">
      <td>{{index + 1}}</td>
      <td>{{ trabajo.descripcion }}</td>
      <td>
        <a 
          href="#" 
          class="btn btn-danger btn-sm"
          @click.prevent="removeFromTrabajo(index)"
          data-toggle="tooltip"
          data-placement="top"
          title="Eliminar">
          <i class="fas fa-ban"></i>
        </a>                             
      </td>
    </tr>
  </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

You are passing just id in an object i.e.

removeFromTrabajo({ id: index + 1 })

Live Demo using filter

Codesandbox Demo

So you should receive only single argument with an id and you should remove as:

removeFromTrabajo(data) {
      console.log(this.trabajos);
      this.trabajos = this.trabajos.filter((o, index) => data.id !== index + 1);
    },
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda