I am a beginner in VueJs. I want to show the same component multiple times on one view. So I added this code:
<StarRatingReadonly :rating="i" v-for="i in 5" :key="i"></StarRatingReadonly>
My StarRating-Script component looks like this:
https://jsfiddle.net/s3dbfhr9/2/
There should always be 5 stars. And depending on the rating it should contains as many green stars. Example: If rating is three, there has to be three green stars and two grey ones.
But the value is just showed once.
The reason is simple: radio inputs are meant to chose a single value among multiple choices. It means that within a fieldset, only one radio can be checked at the time.
It's clearer if you remove your .rate > input { display: none; }, you'll see you have only one radio checked.
Solution
Just use checkbox instead of radio. They do the same except that multiple checkboxes can be selected at the same time.
Also, you should update your :checked="" conditions so that it's checked if the rating is actually greater than or equal to the input value:
<fieldset class="rate">
<input
type="checkbox"
name="rating"
:checked="rating >= 5"
><label for="rating10" title="5 stars"></label>
<input
type="checkbox"
name="rating"
value="4.5"
:checked="rating >= 4.5"
><label class="half" for="rating9" title="4 1/2 stars"></label>
<input
type="checkbox"
name="rating"
value="4"
:checked="rating >= 4"
><label for="rating8" title="4 stars"></label>
<input
type="checkbox"
name="rating"
value="3.5"
:checked="rating >= 3.5"
><label class="half" for="rating7" title="3 1/2 stars"></label>
<input
type="checkbox"
name="rating"
value="3"
:checked="rating >= 3"
><label for="rating5" title="3 stars"></label>
<input
type="checkbox"
name="rating"
value="2.5"
:checked="rating >= 2.5"
><label class="half" for="rating4" title="2 1/2 stars"></label>
<input
type="checkbox"
name="rating"
:checked="rating >= 2"
><label for="rating3" title="2 stars"></label>
<input
type="checkbox"
name="rating"
:checked="rating >= 1.5"
><label class="half" for="rating2" title="1 1/2 star"></label>
<input
type="checkbox"
name="rating"
:checked="rating >= 1"
><label for="rating1" title="1 star"></label>
</fieldset>
Working fork of your jsfiddle: https://jsfiddle.net/Kapcash/vzwpc5br/
Note: you could use a v-for inside your rating component to render your inputs and simplify the template.
As you added :checked="ratingValue == 1", It will always checked the radio input which verify this condition.
As I don't have your full child template code. Right now it is just showing simple radio inputs.
Demo :
Vue.component('StarRatingReadonly', {
props: ['rating'],
template: `<input
type="radio"
id="rating1"
name="rating"
:checked="rating === 1"
>`
});
var app = new Vue({
el: '#app'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<star-rating-readonly v-for="i in 5" :key="i" :rating="i"></star-rating-readonly>
</div>
Can you please check above code snippet and complete the remaining code you have. So that I can look into the requirement/issue you are facing.
Related
<template>
<p>FrontEnd Items</p>
<label for="vue">Vue.js</label>
<input type="checkbox" name="vue" id="vue" v-model="frontendItems">
<label for="React">React.js</label>
<input type="checkbox" name="React" id="React" v-model="frontendItems">
<label for="Angular">Angular.js</label>
<input type="checkbox" name="Angular" id="Angular" v-model="frontendItems">
<p>
You have selected :- {{ frontendItems }}
</p>
<p>For radio Buttons</p>
<p>Who am I</p>
<label for="developer">Developer</label>
<input type="radio" name="developer" id="developer" v-model="whoAmI">
<label for="programmer">Programmer</label>
<input type="radio" name="programmer" id="programmer" v-model="whoAmI">
<p>
I am :- {{ whoAmI }}
</p>
</template>
<script>
export default {
name : 'CheckRadiobinding',
data(){
return {
frontendItems:[],
whoAmI : null
}
}
}
</script>
In frontend items i want to get stored array values 'vue', 'react' and 'angular' and in whoAmI varaiable i want to get either developer or programmer whenever i click check and radio button but I am only getting 'on' as a value
You're missing value on each of your inputs. When a checkbox or radio input is selected value is what gets added to your v-model array
<template>
<p>FrontEnd Items</p>
<label for="vue">Vue.js</label>
<input
id="vue"
v-model="frontendItems"
value="Vue.js"
type="checkbox"
name="vue"
/>
<label for="React">React.js</label>
<input
id="React"
v-model="frontendItems"
value="React.js"
type="checkbox"
name="React"
/>
<label for="Angular">Angular.js</label>
<input
id="Angular"
v-model="frontendItems"
value="Angular.js"
type="checkbox"
name="Angular"
/>
<p>You have selected :- {{ frontendItems }}</p>
<p>For radio Buttons</p>
<p>Who am I</p>
<label for="developer">Developer</label>
<input
id="developer"
v-model="whoAmI"
value="Developer"
type="radio"
name="developer"
/>
<label for="programmer">Programmer</label>
<input
id="programmer"
v-model="whoAmI"
value="Programmer"
type="radio"
name="programmer"
/>
<p>I am :- {{ whoAmI }}</p>
</template>
I have a downpayment form that have two inputs. One for dollar amount and one for percentage. I also have a slider that changes the percentage.
<div class="form-group">
<label for="">Down Payment</label>
<div class="flex-row d-flex">
<input class="form-control" type="text" v-model="downPaymentComputed" />
<input class="form-control" type="text" v-model="dpPercent">
</div>
<div class="mt-3 pt-4">
<vue-slider
v-model="dpPercent"
:tooltip="'always'"
:min="0"
:max="100"
:tooltip-formatter="formatter1"
></vue-slider>
</div>
</div>
in the script
let app = new Vue({
el: '#app',
delimiters: ['${', '}'],
components: {
VueSlider: window['vue-slider-component']
},
data: {
formatter2: v => `$${('' + v).replace(/\B(?=(\d{3})+(?!\d))/g, ',')}`,
formatter1: '{value}%',
payment:null,
downPayment: {{ property.price*0.2 }},
dpPercent:'20',
price: {{ property.price }}
},
computed:{
downPaymentComputed(){
return this.price*(this.dpPercent/100)
}
},
In this configuration when I use the slider or enter the percentage, the downpayment changes. But when I enter new downpayment, nothing happens.
How do I bind so that when I change either the downpayment or the percent or the slider all inputs get updated?
EDIT: Thanks Phil, you pointed me in the right direction. I binded the values and then used on:change methods to update the other values
<input class="form-control" type="text" v-on:keyup="updateDP()" v-model="price" />
<input class="form-control" type="text" v-on:keyup="updatePercent()" v-model="downPayment" />
<input type="text" class="form-control" v-on:keyup="updateDP()" v-model="dpPercent">
In a web form, using Vuejs, I would like to implement a multiple choice question with a sub-question for each item.
For instance, the question could look like this:
What type of vehicle do you use, and how often ?
A) Aeroplane (chekbox), Every day / Once a month / once a year (radio),
B) Train (checkbox), Every day / Once a month / Once a year (radio),
C) Donkey (checkbox), Every day / Once a month / Once a year (radio),
D) (and so on).
I can imagine that my html could look something like this :
<div class="form-group">
<div class="line" v-for="(type, index) in listOfVehicles">
<input type="checkbox" v-bind:value="index" v-bind:id="index" v-model="???">
<label v-bind:for="index">{{type}}</label>
<div class="subquestion" v-for="(sub, id) in listOfSubQuestions">
<input type="radio"
v-bind:value="id"
v-bind:id="'sub-' + index + '-' + id"
v-model="???">
<label v-bind:for="'sub-' + index + '-' + id">{{sub}}</label>
</div>
</div>
</div>
where my Vue instance would have listOfVehicles and listOfSubQuestions as arrays in my data.
Ideally, I would like all the respondent's input to be stored in a single array called for instance answers.
As an example, if answers A and C have been ticked, answers could look something like: [{vehicle: 'A', frequency: 'Once a year'},{vehicle: 'C', frequency: 'Every day'}].
But I have no clue how to make this work.
I quickly made something if this is what you need. Its my first time answering a question hope this can help you out. You can add a button and store a new object every-time you click the button,
which can result something like the example you mentioned.
new Vue({
el: "#app",
data: {
vehicle: [],
frequency: '',
result: []
},
methods: {
save() {
const result = {
vehicle: this.vehicle,
frequency: this.frequency
}
this.result.push(result)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>What type of vehicle do you use, and how often ?</h2>
<div>
<label for="plane">Aeroplane</label>
<input id="plane" type="checkbox" value="aeroplane" v-model="vehicle">
<label for="car">car</label>
<input id="car" type="checkbox" value="car" v-model="vehicle">
<label for="train">train</label>
<input id="train" type="checkbox" value="train" v-model="vehicle">
<div>
<label for="plane">everyday</label>
<input type="radio" name="options" value="everyday" v-model="frequency">
<label for="plane">monthly</label>
<input type="radio" name="options" value="monthly " v-model="frequency">
<label for="plane">yearly</label>
<input type="radio" name="options" value="yearly" v-model="frequency">
</div>
<button #click="save">submit</button>
<p>{{result}}</p>
</div>
</div>
I am new to VUE and I am wondering is there any way I can have a unique id per row with this kind of implementation
<template slot="display" scope="row">
<table-display>
<input type="checkbox" />
</table-display>
<table-display class="j-td-code">
<input type="text" v-model="row.data.code" />
</table-display>
<table-display class="j-td-name" value="">
<input type="text" v-model="row.data.name"/>
</table-display>
<table-display >
<img :src="updateIcon">
</table-display>
<table-display >
<img :src="deleteIcon" #click="destroy(row.data)">
</table-display>
</template>
is a component and slotted on my parent component.
Hopefully someone could help me been working on this since yesterday.
Thanks! :)
I'm using Vue for the first time, with Vue Validator. Here is an example of my code:
<label for="first_name">First name:
<span v-if="$validation1.first_name.required" class="invalid">Enter your first name.</span>
<input id="first_name" placeholder="e.g. Christopher" class="" v-validate:first_name="['required']" v-model="first_name" name="first_name" type="text">
</label>
The only issue at the moment is that when I land on the page with my form, the whole thing is covered in errors. Is there a way I can suppress the errors and only show them on input blur / form submit?
Argh, the Google-able word isn't about blur, or on submit – its about timing and initial:
http://vuejs.github.io/vue-validator/en/timing.html
<input id="first_name" initial="off" placeholder="e.g. Christopher" class="" v-validate:first_name="['required']" v-model="first_name" name="first_name" type="text">
you need to add .dirty or .touched to your validation
<label for="first_name">First name:
<span v-if="$validation1.first_name.required && $validation1.first_name.touched" class="invalid">Enter your first name.</span>
<input id="first_name" placeholder="e.g. Christopher" class="" v-validate:first_name="['required']" v-model="first_name" name="first_name" type="text">
</label>
I was dealing with a similar problem. I had to have an initialized variable for the input name: "" but I also wanted to have a required attribute in element.
So I add required when the event onblur occurs.
<input name="name" type="number" v-model="name" #blur="addRequired" />
const app = Vue.createApp({
data() {
return {
name: ""
}
},
methods:{
addRequired: function(event){
event.target.setAttribute("required", true);
}
}
});