Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make some condition inside v-on:click?

I have a situation is that i want to change the class of the li tag when i click it, but need under some condition is class active originally. How to make it works? Here is my code:

<li v-for="timeslot in hourly.Slots" class="time-slot" 
   v-bind:class="{'selected': timeslot.Status=='selected', 'unavailable': timeslot.Status =='unavailable', 
   'active': timeslot.Status=='available'}" v-on:click="timeslot.Status='selected'">
like image 642
Winston Avatar asked Dec 01 '16 02:12

Winston


2 Answers

This can be done by calling a methods on click, and checking in that method whether timeslot is available or not, like following:

var demo = new Vue({
    el: '#demo',
    data: function(){
        return {
        hourlySlots: [
          { 'Status' : 'unavailable', name: "john" },
          { 'Status' : 'available' , name: "Danny" }        
        ]
      };
    },
    methods: {
      selectSlot: function(timeslot) {
        if(timeslot.Status =='available'){
            timeslot.Status ='selected'
        } 
      }
    }
})

Working fiddle: http://jsfiddle.net/5dkw58ke/

like image 122
Saurabh Avatar answered Sep 28 '22 22:09

Saurabh


I think it can do by using ternary operator.

v-on:click="
  timeslot.Status == 'available' ?
    timeslot.Status = 'selected' :
    ''
"

http://jsfiddle.net/yrejhmp9/1/

or use logical operators.

v-on:click="
  timeslot.Status == 'available' &&
  (timeslot.Status = 'selected')
"

http://jsfiddle.net/mrgt82ke/1/

like image 25
KABA Avatar answered Sep 28 '22 23:09

KABA