1

i am new to vue js, i have one "iban" entry that I want to do and when "add iban" button is clicked, first entry "delete" range should be added and when I want to delete it starts from above, I think I made a mistake. Thanks already for your help.

enter image description here

<template>
    <div class="col-12" v-for="(section, index) in sections">
        <div class="mb-1 row">
            <div class="col-sm-3">
                <label class="col-form-label" for="iban"><span><i data-feather='file-text'></i></span>IBAN NUMBER</label>
            </div>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="iban" v-model="section.item">
            </div>
        </div>

        <div class="mb-1 row" v-for="(addition, index) in section.additionals">
            <div class="col-sm-3">
                <label class="col-form-label" for="iban"><span><i data-feather='file-text'></i></span>IBAN NUMBER</label>
            </div>
            <div class="col-sm-6">
                <input type="text" class="form-control" placeholder="Item" v-model="addition.item">
                <span class="float-right" style="cursor:pointer" @click="removeItem(index)">X</span>
            </div>
        </div>
        <button class="btn btn-success mt-5 mb-5" @click="addNewItem(index)">New Iban</button>
    </div>
</template>
<script>
export default {
    data() {
        return {
            data: {
                iban: "",
            },
            sections: [
                {
                    item: '',
                    additionals: [] 
                }
            ]
        }
    },
    methods: {
        addNewItem(id) {
            this.sections[id].additionals.push({
                item: ''
            })
        },
        removeItem(index){
            this.sections[index].additionals.splice(index,1)
        },
    },
}
</script>
0

2 Answers 2

1

Looking at your updated post & images, I think you are looking for the following:

new Vue({
  el: "#app",
  data() {
    return {
      data: {
        iban: "",
      },
      sections: [{
        ibans: [{item: ""}]
      }]
    }
  },
  methods: {
    addNewItem(id) {
      this.sections[id].ibans.push({item: ''});
    },
    removeItem(sectionIndex, ibanIndex) {
      this.sections[sectionIndex].ibans.splice(ibanIndex, 1);
    },
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <template>
    <div class="col-12 sections" v-for="(section, sectionIndex) in sections">
      <div class="mb-1 row" v-for="(iban, ibanIndex) in section.ibans">
        <div class="col-sm-3">
          <label class="col-form-label" for="iban"><span><i data-feather='file-text'></i></span>IBAN NUMBER</label>
        </div>
        <div class="col-sm-6">
          <input type="text" class="form-control" placeholder="Item" v-model="iban.item">
          <span v-if="section.ibans.length>1"
          class="float-right" style="cursor:pointer" @click="removeItem(sectionIndex, ibanIndex)">X</span>
        </div>
      </div>
      <button class="btn btn-success mt-5 mb-5" @click="addNewItem(sectionIndex)">New Iban</button>
    </div>
  </template>
</div>

Sign up to request clarification or add additional context in comments.

20 Comments

thank you for your reply but the first input "delete" button is not added
hold on, let me update the answer. @SelaminkoElam
Quick question @SelaminkoElam what should be the initial state? Just a New Iban button? Also, what should happen when you click on the delete of the first input? Could you please elaborate on what do you mean by 'first entry "delete" range should be added and when I want to delete it starts from above'?
There is the first iban or I want the original input "deletion" added.
okay, and what should happen on click of the deletion button next to the first input box? should all the additional inputs be removed? Can you check this demo and see if this is the output you want?
|
0

I think this code could help you.

<template>
  <div class="col-12">
    <div
      class="mb-1 row"
      v-for="(item, index) in sections.additionals"
      :key="index"
    >
      <div class="col-sm-3">
        <label for="iban" class="col-form-label">
          <span><i data-feather="file-text"></i></span>IBAN NUMBER
        </label>
      </div>
      <div class="col-sm-6">
        <input type="text" class="form-control" id="iban" v-model="item.item" />
        <span
          class="float-right"
          style="cursor: pointer"
          @click="removeItem(index)"
          >X</span
        >
      </div>
      <button class="btn btn-success mt-5 mb-5" @click="addNewItem">
        New Iban
      </button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: {
        iban: "",
      },
      sections: { additionals: [{ item: "" }] },
    };
  },
  methods: {
    addNewItem() {
      this.sections.additionals.push({
        item: this.data.iban,
      });
    },
    removeItem(index) {
      this.sections.additionals.splice(index, 1);
    },
  },
};
</script>

Comments

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.