I have a group of fields that are generated in a loop an indefinite number of times. Basically 2 input fields, a label, and a delete button. Then another button that says Add More.
Every time they hit that, one more set of the above-mentioned appears. They can do this as many times as they want. It looks like this. (I'm using quasar, hence why the q-"" elements)
// Click more button
<q-btn @click="onAddBarcodes" icon="add" no-caps color="primary">Add More</q-btn>
// Form
<div v-for="(barcode, index) in barcodes" :key="index">
<div class="row q-pr-lg items-center">
<div class="col-3">
<label class="text-weight-medium">Starting Roll #:</label>
<q-input v-model="barcode.start" :id="barcode.id"></q-input>
</div>
<div class="col-3">
<label class="text-weight-medium">Ending Roll #:</label>
<q-input v-model="barcode.end" :id="barcode.id"></q-input>
</div>
<div class="col-5">
<label class="text-weight-medium">Agency Name:</label>
<div v-if="barcode.agencyName">
{{ barcode.agencyName }}
</div>
</div>
<div class="col-1">
<q-btn @click="onDeleteBarcodes(barcode.id)" icon="close"/>
</div>
</div>
</div>
export default {
data() {
return {
barcodes: []
}
},
methods: {
onAddBarcodes() {
const newBarcode = {
id: Math.random() * Math.random() * 1000,
start: "",
end: "",
agencyName: ""
};
this.barcodes.push(newBarcode);
},
onDeleteBarcodes(id) {
this.barcodes = this.barcodes.filter(barcode => barcode.id !== id);
},
}
}
Now this works at its most basic form. What I want to do is to load the component with a set number of iterations already started. For example, 5 of them.
The Add More button will add the 6th on forward if they need more. The delete button should delete down to zero if they wanted to (not that they would) but it should definitely delete down to 1 at least.
How can I start the v-for
at a specific number of iteration?