0
Good night! I’m a beginner with Vue.js, I was trying to do some tests and small projects to train and decided to make a program that simulates a Clock Point. Anyway, I ended up locking this part of the code (right at the beginning --'), where I want to take the value of Input and insert it as an Option of a Select, where later I will add a button to mark the Checkin/Checkout of the selected employee.
Follows the code:
<template>
<div class="container">
<div class="theButtons">
<!-- This button shows the input -->
<button @click="toggleButton1 = !toggleButton1" class="btn btn-danger" type="button">New Collab</button>
<!-- This button show the select -->
<button @click="toggleButton2 = !toggleButton2" class="btn btn-danger" type="button">Collabs</button>
</div>
<!-- This form insert the name -->
<form v-show="toggleButton1" class="newCollabForm">
<div class="form-group">
<label>Employee</label>
<input type="text" class="form-control" placeholder="Collab Name" id="collabName">
<!-- This button submit a new collab name -->
<button @click="submitCollab" class="btn btn-dark" type="submit">Submit</button>
</div>
</form>
<!-- This form show the names -->
<form v-show="toggleButton2" class="newCollabForm">
<div class="form-group">
<label for="namesSelect">Search Employee</label>
<select class="form-control" id="namesSelect">
<!-- <option selected> -- </option> -->
<option v-for="collab in collabNames" v-bind:key="collab.value" />
</select>
</div>
</form>
</div>
</template>
<script>
export default {
name: "ButtonsAndForm",
data() {
return {
collabNames: [],
toggleButton1: false,
toggleButton2: false
}
},
methods: {
submitCollab() {
let newCollabName = document.querySelector("#collabName");
let option = document.querySelector("option");
let selectMenu = document.querySelector("#namesSelect").value;
this.collabNames.push(newCollabName.value);
option.innerHTML = newCollabName.value;
selectMenu.appendChild(option);
alert(this.newCollabName);
}
}
}
</script>
The problems:
1 - In the Select tag, when inserting a new value, a blank value comes out before each name inserted;
2 - Is it "right" or good practice to use this v-for in the option tag? I wondered if it would not be in the Select tag;
3 - When I use Option without v-for, it only shows the name that was entered at the time of Input, but when I test an "Alert(array)" to show the contents of the Array, all inserted names are shown, only not shown in Select. e_and
Can someone shed some light? I already read the documentation (https://br.vuejs.org/v2/guide/forms.html), I didn’t find anything like this. :/
Thank you for your attention
If you did the code part all wrong
– novic