2

** For example, here, when I click the button, I will have one more component, it means it will have new data, so I want to gather all info into one array when I press Save Data button, I hope,it's pretty straightforward to understand

<Child v-for="count in btnNumber" :key="count" @showData="getElements" />

<v-btn
  color="primary"
  elevation="10"
  class="space"
  large
  @click="duplicateEl"
  >Add Categ & Key</v-btn
>
v-btn
      color="secondary"
      elevation="13"
      class="btnEl"
      dark
      large
      @click="getResult"
      >Save Data</v-btn

** It's getting data from my child component using Emit

methods:{
               getElements(emitPayload) {
              this.selectedChildCategory = emitPayload.selectedCateg;
              this.selectedChildKey = emitPayload.selectedKey;
              this.selectedChildLanguage = emitPayload.selectedLang;
              this.selectedChildContent = emitPayload.selectedCon;
        }
    }
 duplicateEl() {
  this.btnNumber++;
}

Here is my form please help, I appreciate it

What if I want to add nested child to child component?

6
  • Try saving the data on emit (from get elements) to a new data variable array, and use that array Commented Jan 18, 2022 at 7:01
  • yeah what about if I duplicate the Child Component?it means I need duplicated overall result. If you look at the picture, I want to duplicate child component and save all input result into array at once Commented Jan 18, 2022 at 7:03
  • Same, first do a check, if its same index or key (it depends on your check value), then update, else do a push Commented Jan 18, 2022 at 7:06
  • can you give me some example? I'm beginner in Vue hope you understand Commented Jan 18, 2022 at 7:08
  • I appreciate your answer my friend! Below answer is the exact answer I need thanks! Commented Jan 18, 2022 at 11:33

2 Answers 2

2

You can hold data on parent component, pls take a look at following snippet:

Vue.component('Child', {
  template: `
  <v-form>
    <v-container>
      <v-row>
        <v-col>
          <v-select
            :items="categories"
            label="Category"
            dense
            outlined
            v-model="content.cat"
            @change="setD"
          ></v-select>
          </v-col>
          <v-col>
          <v-select
            :items="keys"
            label="Key"
            dense
            outlined
            v-model="content.key"
            @change="setD"
          ></v-select>
          </v-col>
          <v-col>
          <v-select
            :items="langs"
            label="Lang"
            dense
            outlined
            v-model="content.lang"
            @change="setD"
          ></v-select>
          </v-col>
          <v-col>
          <v-select
            :items="contents"
            label="Cont"
            dense
            outlined
            v-model="content.cont"
            @change="setD"
          ></v-select>
        </v-col>
      </v-row>
    </v-container>
  </v-form>
  `,
  props: ['conte'],
  data() {
    return {
      content: this.conte,
      categories: ['first', 'second', 'third'],
      keys: [1,2,3],
      langs: ['g', 'h', 'j'],
      contents: ['aaa', 'bbb', 'ccc']
    }
  },
  methods: {
   setD() {
      this.$emit('show', this.content);
    },
  },
})

new Vue({
  vuetify: new Vuetify(),
  el: "#app",
  data() {
    return {
      contentFields: [{id: 0, cat: '', key: '', lang: '', cont: ''}],
      showData: false
    }
  },
  methods: {
    addInput() {
      let newI = this.contentFields.length 
      this.contentFields.push({id: newI, cat: '', key: '', lang: '', cont: ''})
    },
    getElements(e){
      const newData = this.contentFields.map(obj => {
        if(obj.id === e.id) 
           return { ...obj }
        return obj
      });
    },
    getResult() {
      this.showData = !this.showData
    }
  }
})
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
</head>
<body>
  <div id="app">
    <v-app>
      <v-main>
        <v-container>
          <v-btn
            color="primary"
            elevation="10"
            class="space"
            large
            @click="addInput"
          >Add Categ & Key</v-btn>
          <v-container v-for="(content, i) in contentFields" :key="i">
            <child :conte="content" @show="getElements" />
          </v-container>
          <v-btn
            color="secondary"
            elevation="13"
            class="btnEl"
            dark
            large
            @click="getResult"
          >Save Data</v-btn>
          <div v-if="showData">{{ contentFields }}</div>
        </v-container>
      </v-main>
    </v-app>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

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

2 Comments

dude sorry for bothering, what if I want to have nested child? Please check the last image, I would be really thankful, if a bit update your answer
I wanna to add nested child component and values each time user add new nested child component. [ { "id": 0, "cat": "", "key": "", "lang": "", "cont": "", "childlang": "", "childCon": "" } ][ { "id": 0, "cat": "", "key": "", "lang": "", "cont": "", "childlang": "", "childCon": "" "childlang": "", "childCon": "" " } ]
1

Try saving the data on emit event (from get elements) to a new data variable array, and use that array

<template>
      <div>
        <Child
          v-for="count in btnNumber"
          :key="count"
          @showData="getElements(count)"
        />
        <!-- BUTTONS HERE -->
      </div>
    </template>
    <script>
    export default {
      data() {
        return {
          elementsEmmitedDataArray: [], // array
        };
      },
      methods: {
        getElements(countIndex, emitPayload) {
          const data = {
            uniqueIndex: countIndex, //or anything unique for each Child component
            selectedChildCategory: emitPayload.selectedCateg,
            selectedChildKey: emitPayload.selectedKey,
            selectedChildLanguage: emitPayload.selectedLang,
            selectedChildContent: emitPayload.selectedCon,
          };
          // check if uniqindex is already exist in array then update it else push new data
            const index = this.elementsEmmitedDataArray.findIndex(
                (element) => element.uniqueIndex === countIndex
            );
            if (index !== -1) {
                this.elementsEmmitedDataArray[index] = data;
            } else {
                this.elementsEmmitedDataArray.push(data);
            }
        },
        duplicateEl() {
          this.btnNumber++;
        },
    
        submitData(){
            // use the array 
            console.log(this.elementsEmmitedDataArray);
        }
    
      },
    };
    </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.