1

I want to ask the user how many points he wants to create and then get each points coordinates.

I tried creating a initial text field to get how many points and then a loop to create each form. It works but I don't know how to get each form value.

How can I get each form values? Or is there a better way to do it? enter image description here

   <template>
      <div>
        <v-card class="mb-3">
          <v-card-text>
            <v-text-field label="How many nodes" :value="nodes" @input="onInput" type="number"></v-text-field>
          </v-card-text>
        </v-card>
        <v-container fluid grid-list-md>
          <v-layout row wrap>
            <template v-for="i in nodes">
              <v-flex :key="i" xs12 md3>
                <div>
                  <v-card class="mb-3">
                    <v-card-text>
                      <div>Node {{i}}</div>
                      <v-text-field label="Coord X" value="x1" @input="getValues" type="number" v-model="no1"></v-text-field>
                      <v-text-field label="Coord Y" :value="y1" @input="getValues" type="number"></v-text-field>
                    </v-card-text>
                  </v-card>
                </div>
              </v-flex>
            </template>
          </v-layout>
        </v-container>
      </div>
    </template>
    <script>
      export default {
        data () {
          return {
            nodes: 2
          }
        },
        methods: {
          onInput (val) {
            this.nodes = parseInt(val)
          }
        }
      }
    </script>

1 Answer 1

1

I think this would be a better way of doing it:

   <template>
      <div>
        <v-card class="mb-3">
          <v-card-text>
            <v-text-field label="How many nodes" v-model="nodes" type="number"></v-text-field>
          </v-card-text>
        </v-card>
        <v-container fluid grid-list-md>
          <v-layout row wrap>
            <template v-for="(node, index) in nodesArr">
              <v-flex :key="i" xs12 md3>
                <div>
                  <v-card class="mb-3">
                    <v-card-text>
                      <div>Node {{index + 1}}</div>
                      <v-text-field label="Coord X" v-model="node[index].coordX" type="number" v-model="no1"></v-text-field>
                      <v-text-field label="Coord Y" v-model="node[index].coordY" type="number"></v-text-field>
                    </v-card-text>
                  </v-card>
                </div>
              </v-flex>
            </template>
          </v-layout>
        </v-container>
      </div>
    </template>
    <script>
      export default {
        data () {
          return {
            nodes: 0,
            nodesArr: []
          }
        },
        watch: {
            nodes(newVal) {
                this.nodesArr = [];
                for(var i=0; i<this.nodes; i++){
                    this.nodesArr.push({coordX: "", coordY: ""});
                }
            }
        },
        methods: {
        }
      }
    </script>

Whats going on:

  1. Set up a v-model on the input which takes the number of nodes and bind it to nodes property.

  2. initialized a new property nodesArr : [] which will be used to loop through to display each Coord input

  3. set up a watcher on nodes which loops through the number of nodes entered and pushes those many objects {coordX: "", coordY: ""} to nodesArr array

  4. we loop through nodesArr using v-for="(node, index) in nodesArr" to display the inputs for x-coord and y-coord

  5. The x-coord input is bound to the corresponding coordX property making use of the index we get in v-for
  6. Similarly the y-coord input is bound to the corresponding coordY property making use of the index we get in v-for
  7. since the inputs are two way bound using v-model you have all the input data in nodesArr property which can be used as you wish
Sign up to request clarification or add additional context in comments.

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.