Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!