How can I combine vue tel input with input text vuetify? - vue.js

My code like this :
<v-row>
<v-col cols="12" sm="6" md="3">
<v-text-field
label="Phone"
outlined
dense
></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="3">
<vue-tel-input v-model="phone"></vue-tel-input>
</v-col>
</v-row>
My codepen like this : https://codepen.io/positivethinking639/pen/qBBKYON?&editable=true&editors=101
I want to combine it to be like this :
The blue sign is taken from vue tel input. The text field next to it is taken from the vuetify component
How do I combine 2 different components into one like the image above?

Yes, it is possible to set the country code next to dropdown
Here is the working codepen: https://codepen.io/chansv/pen/pooZJey?editors=1010
<div id="app">
<v-app id="inspire">
<v-form>
<v-container>
<v-row>
<v-col cols="12" sm="6" md="3">
<v-text-field
label="Phone"
outlined
dense
></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="3">
<vue-tel-input v-model="phone" #country-changed="countrySelected">
<template v-slot:arrow-icon>
<v-icon>arrow_drop_down</v-icon>
<strong>+{{countryCode}}</strong>
</template>
</vue-tel-input>
</v-col>
</v-row>
</v-container>
</v-form>
</v-app>
</div>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
phone: null,
countryCode: null,
}
},
methods: {
countrySelected(val) {
this.countryCode = val.dialCode;
}
}
})

Related

How to push data into an array from a v-form?

At the moment, I have the following code:
<template>
<v-container>
<v-form v-model="valid" ref="form" v-for="(resposta,index) in formData.body" :key="index">
<v-row>
<v-col cols="8">
<v-text-field
v-model="resposta.answer"
:rules="idRules"
:counter="200"
label="Texto da Resposta"
required
></v-text-field>
</v-col>
</v-row>
<v-row>
<v-col xl="2" lg="3" md="3" sm="4" class="mt-4">
<p class="grey--text text--darken-1">Resposta Correta:</p>
</v-col>
<v-radio-group
v-model="resposta.correction"
row
mandatory
>
<v-col md="4" sm="4">
<v-radio
label="0"
value="0"
></v-radio>
</v-col>
<v-col md="4" sm="4">
<v-radio
label="1"
value="1"
></v-radio>
</v-col>
</v-radio-group>
</v-row>
<v-row>
<v-col xl="2" lg="3" md="3" sm="4" class="mt-4">
<p class="grey--text text--darken-1">Resposta Obrigatória:</p>
</v-col>
<v-radio-group
v-model="resposta.mandatory"
row
mandatory
>
<v-col md="4" sm="4">
<v-radio
label="0"
value="0"
></v-radio>
</v-col>
<v-col md="4" sm="4">
<v-radio
label="1"
value="1"
></v-radio>
</v-col>
</v-radio-group>
</v-row>
<v-row>
<v-col xl="2" lg="3" md="3" sm="4" class="mt-4">
<p class="grey--text text--darken-1">Resposta Eliminatória:</p>
</v-col>
<v-radio-group
v-model="resposta.eliminative"
row
mandatory
>
<v-col md="4" sm="4">
<v-radio
label="0"
value="0"
></v-radio>
</v-col>
<v-col md="4" sm="4">
<v-radio
label="1"
value="1"
></v-radio>
</v-col>
</v-radio-group>
</v-row>
<v-row>
<v-col cols="5">
<v-select :items="pontos"
v-model="resposta.points"
label="Pontos"
dense
></v-select>
</v-col>
<v-col cols="7" align="center">
<v-btn #click="addAnswer" class="white--text" color="#2A3F54" grey--text>
Add Answer
</v-btn> {{formData.body}}
</v-col>
</v-row>
</v-form>
</v-container>
</template>
<script>
export default {
data(){
return{
formData:{
body: [{
answer: '',
correction: '',
mandatory: '',
eliminative: '',
points:''
}]
}
}
},
methods: {
addAnswer(){
this.formData.body.push({answer: '',
correction: '',
mandatory: '',
eliminative: '',
points:''})
alert("inserida")
}
},
watch: {
formData: {
handler: function() {
this.$emit('newdataRespostas', this.formData.body);
},
deep: true
}
},
}
</script>
The goal with this is to be able to get the information in the array in another component, which I'm being able to do. The problem here is that everytime I click on the Add Answer button, it creates a new identical form right under the previous one. The data is being pushed correctly into the array, the other component is being able to watch for the information inside that array, but I'm just not able to get rid of the new forms that get created. If I add 4 new answers, I'll have 5 forms by the end, which is clearly not optimal. Also any tips regarding any part of the code are welcome. I'm completely new to Vue.js and Vuetify.
Sorry for the long code, but almost all of it is just the different form components.
The form uses v-for on formData.body[], so each array element (each answer) renders a new form. It sounds like you want only the newest form to be shown, in which case you don't need v-for.
I would replace the v-for with a different data property (e.g., named resposta to match the current variable used in your template), and append that property to formData.body[] in addAnswer():
<template>
<v-form v-model="valid" ref="form">
<!--...-->
<v-text-field v-model="resposta.answer" />
</v-form>
</template>
<script>
export default {
data() {
return {
formData: {
body: [],
},
resposta: {},
};
},
methods: {
addAnswer() {
this.formData.body.push(this.resposta)
this.resposta = {}
},
},
}
</script>
demo

How to display input with the click of a button in Vue?

I've been reading about text-fields but I can't figure it out.. I want to be able to type in a name and age, and then when I press the button "add", I want to be able to see the name and age displayed below. What is the easiest or best way of doing this?
My code:
<v-container>
<v-row>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Name" solo></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Age" solo></v-text-field>
</v-col>
<div class="addbutton">
<v-btn color="green darken-1" large v-on:click="add">Add</v-btn>
</div>
</v-row>
First you should bind inputs to the component data using v-model.
script:
export default {
data(){
return {
name: null,
age: null
}
}
}
html:
<v-col cols="12" sm="6" md="3">
<v-text-field label="Name" solo v-model="name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Age" solo v-model="age"></v-text-field>
</v-col>
Now you can display current inputs values: <p>{{ name + ', ' + age }}</p>
If you want to show/hide them with a button, you can add a data property to store their state, create a method to toggle it and bind their visibility to the property using v-if:
script:
export default {
data(){
return {
name: null,
age: null,
showValues: false
},
methods: {
toggle() {
this.showValues = !this.showValues
}
}
}
html:
<v-row>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Name" solo v-model="name"></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Age" solo v-model="age"></v-text-field>
</v-col>
<div class="addbutton">
<v-btn color="green darken-1" large #click="toogle">Add</v-btn>
</div>
<p v-if="showValues">{{ name + ', ' + age }}</p>
</v-row>
Store the variables in the data of your vue component and the use it to render
<v-row>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Name" solo></v-text-field>
</v-col>
<v-col cols="12" sm="6" md="3">
<v-text-field label="Age" solo></v-text-field>
</v-col>
<div class="addbutton">
<v-btn color="green darken-1" large v-on:click="add">Add</v-btn>
</div>
<v-col cols="12" sm="6" md="3" v-if="render!==null">
<p>{{ render.name }}</p>
<p>{{ render.age }}</p>
</v-row>
<script>
export default {
data(){
return {
name:null,
age:null,
render:null,
},
methods(){
add(){
const name = this.name;
const age = this.age;
this.render = {
name,
age
}
},
}
}
</script>

Push component in vue

I've been trying to push a text field whenever the "add button" is pressed.
This is my code so far.
<v-container fluid>
<v-row>
<v-col cols="7">
<v-row class= "mx-1 my-1">
<v-text-field outlined label="Test 1" v-model="test1"></v-text-field>
</v-row>
</v-col>
<v-col cols="5">
<v-row class= "mx-4 my-1">
<v-text-field type="number" outlined label="Test 2" v-model="test2"></v-text-field>
</v-row>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-row class= "mx-1 my-n8">
<v-btn
#click="doWork()"
width = 100%
small
outlined
color="#0000b0"
>
<v-icon dark>mdi-plus</v-icon></v-btn>
</v-row>
</v-col>
</v-row>
</v-container>
Here is my javascript
<script>
export default {
data () {
return {
test1:'',
test2:''
}
},
methods: {
doWork () {
//just for debugging purposes
console.log(this.valor)
console.log(this.precio)
}
}
}
</script>
What should I add in the "doWork()" method in order to push another pair of text fields
Thanks in advance <3
You can transform your fields into a fields array:
data () {
return {
inputs: []
}
}
Your addWork method becomes a method that push a new value in this inputs array:
methods: {
doWork () {
this.inputs.push({
label: 'Test',
value: ''
})
}
}
And you display those inputs with a v-for directive:
<v-row>
<v-col cols="2" v-for="(input, index) in inputs" :key="index">
<v-row class="mx-1 my-1">
<v-text-field outlined :label="input.label" v-model="input.value"></v-text-field>
</v-row>
</v-col>
</v-row>
Working example: https://codesandbox.io/s/festive-dream-gbo6t?file=/src/App.vue

Vue SPA Vuetify v-text-field outlined field labels not positioning correctly

Would anyone know how to fix label positioning for Vuetify outlined text fields. Here's a simple example. On focus, the label doesn't position themselves correctly.
<div id="js-nomination-form">
<v-form v-model="valid">
<v-row>
<v-col cols="6">
<v-text-field
outlined
v-model="companyname"
label="Company name"
></v-text-field>
</v-col>
<v-col cols="6">
<v-text-field
v-model="companyaddress"
label="Address"
outlined
></v-text-field>
</v-col>
</v-row>
</v-form>
</div>
new Vue({
el: '#js-nomination-form',
vuetify: new Vuetify(),
data: () => ({
valid: false,
companyname: '',
companyaddress: '',
}),
methods: {
submit () {
},
clear () {
}
}
})
https://codepen.io/mkrisch76/pen/YzPbrpy
Wrap v-form with v-app like this:
<div id="js-nomination-form">
<v-app>
<v-form v-model="valid">
<v-row>
<v-col cols="6">
<v-text-field
outlined
v-model="companyname"
label="Company name"
></v-text-field>
</v-col>
<v-col cols="6">
<v-text-field
v-model="companyaddress"
label="Address"
outlined
></v-text-field>
</v-col>
</v-row>
</v-form>
</v-app>
</div>
But it's easy solution, just to see changes in your CodePen example.
It's better to use v-app in your main template (usually App.vue) like this:
<!-- App.vue -->
<template>
<v-app id="app">
<router-view/>
</v-app>
</template>
...
v-app should only be used within your application ONCE.
Read more about v-app

VueJs use attribute value to get array value

In a form, I would like to get the name attribute value to find error message, I wrote manually the name on the errors messages array and it works.
But I would like to not have to write the input name each time.
Exemple :
<v-container>
<v-row>
<v-col cols="12">
<v-text-field v-model="label" name="label" :error-messages="errors[NAME_ATTRIBUTE_VALUE]" label="Label" #change="resetFormInputValidation" required></v-text-field>
</v-col>
</v-row>
<v-row>
<v-col cols="9">
<v-text-field v-model="mimeType" name="mime_type" :error-messages="errors['mime_type']" label="MIME Type" required></v-text-field>
</v-col>
<v-col cols="3">
<v-text-field v-model="extension" name="file_extension" :error-messages="errors['file_extension']" label="Extension" required></v-text-field>
</v-col>
</v-row>
</v-container>
There is no way you can access the name attribute in that way. To further elabourate my comment, a way will be to declare an array containing all the values for the name attribute, and then iterating through them using v-for. This gives you access to the dynamic name attribute. A proof-of-concept code:
JS:
data: function() {
return {
names: ['name1', 'name2', 'name3']
};
}
Template:
<v-container v-for="(name, i) in names" :key="i">
<v-row>
<v-col cols="12">
<v-text-field v-model="label" :name="name" :error-messages="errors[name]" label="Label" #change="resetFormInputValidation" required></v-text-field>
</v-col>
</v-row>
<!-- Other markup -->
</v-container>