Room calculator
HTML
<div id="vueRoot">
<reservation v-for="myReservation in store.reservations" :reservation="myReservation"></reservation>
<a class="myPlus" v-on:click="addRow">+</a>
</div>
CSS
.myPlus{
width : 40px;
height : 40px;
background-color : green;
font-size : 35px;
border-radius : 20px;
text-align: center;
color: white;
float: right;
margin-top: 80px;
margin-right: 80px;
}
JavaScript
var vueStore = {
rooms : [
{label : 'palatial suite', price : 1000.73},
{label : 'presidential suite', price : 2000.36}
],
reservations : [{
selectedRoom : null,
numAdults : 1,
numChildren : 0
}]
};
Vue.component("reservation",{
template : `<div style="padding:12px">
Room :
<select v-model="reservation.selectedRoom">
<option v-for="room in rooms" v-bind:value="room.price">
{{room.label}}
</option>
</select>
Number of adults :
<select v-model="reservation.numAdults">
<option>1</option>
<option>2</option>
</select>
Number of children :
<select v-model="reservation.numChildren">
<option>0</option>
<option>1</option>
<option>2</option>
</select>
Total Price : {{totalPrice}}
</div>`,
data : function(){
return {
rooms : vueStore.rooms
}
},
props : [ 'reservation' ],
computed : {
totalPrice : function(){
if(this.reservation.selectedRoom){
return this.reservation.selectedRoom
+ this.reservation.numAdults * 500
+ this.reservation.numChildren * 200
}
else return '';
}
}
});
vm = new Vue({
el : "#vueRoot",
data : {store : vueStore},
methods : {
addRow : function(){
this.store.reservations.push({
selectedRoom : null,
numAdults : 1,
numChildren : 0
})
}
}
});