Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
Field value is getting pushed to wrong array

In a vue app form, I have two text inputs and a file input.

What I want to happen: The text inputs should go into a fieldHeader array, and the file input should go into the fieldsArray array.

What does happen: Both text fields and the file input go into both the fieldHeader and the fieldsArray. The console.log for both arrays after clicking "add field" is

0: {field: 0, name: 'text', desc: 'text', imgOne: File}

It should be fieldHeader:

0: {field: 0, name: 'text', desc: 'text'}

and fieldsArray:

0: {imgOne: File}

I would think the v-model would push the items into the correct arrays, but that's not it. The think I am working through now is to restructure the v-for as that's the only place where I can currently see the code as mixing the two array concepts.

<template>
  <div v-for="(field, i) in fieldsArray"  :key="i" >
    <q-input
      label="Name"
      v-model="fieldHeader[i].name"
      type="text"
      dense
    />
    <q-input
      v-model="fieldHeader[i].desc"
      type="textarea"
    />
    <q-file
      v-model="fieldsArray[i].imgOne"
      @update:model-value="onSelected()"
    ></q-file>
  </div>
  <div @click="handleAddField">Add Field</div>
</template>
<script>
const handleAddFields = () => {
  let i = fieldsArray.value.length;

  const item = {
    field: i,
  };
  fieldHeader.value.push(item);
  fieldsArray.value.push(item);
  
};
</script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try like following snippet:

const { ref } = Vue
const app = Vue.createApp({
  setup () {
    const fieldsArray = ref([])
    const fieldHeader = ref([])
    const handleAddField = () => {
      fieldHeader.value.push({name: '', desc: ''});
      fieldsArray.value.push({imgOne: null});
    };
    const onSelected = () => { }
    return { fieldsArray, fieldHeader, handleAddField, onSelected }
  }
})
app.use(Quasar)
app.mount('#q-app')
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/quasar@2.4.12/dist/quasar.prod.css" rel="stylesheet" type="text/css">
<div id="q-app">
  <div v-for="(field, i) in fieldsArray" :key="i" >
    <q-input
       label="Name"
       v-model="fieldHeader[i].name"
       type="text"
       dense
     ></q-input>
     <q-input
       v-model="fieldHeader[i].desc"
       type="textarea"
     ></q-input>
     <q-file
       v-model="fieldsArray[i].imgOne"
       @update:model-value="onSelected()"
     ></q-file>
 </div>
 <q-btn @click="handleAddField">Add Field</q-btn>
 <p>fieldsArray: {{fieldsArray}}</p>
 <p>fieldHeader: {{fieldHeader}}</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script src="https://cdn.jsdelivr.net/npm/quasar@2.4.12/dist/quasar.umd.prod.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda