Search code examples
javascriptvue.jsdatepickervcalendar

How to catch events is vue.js v-calendar


I am using the vuejs v-calender plugin so I have a daterange picker. It all renders fine and I can select dates but that is is it.

What I want to do is just log out the selected dates so later I can store them in database, update form etc but I don't know how to achieve this. I can't find any examples in the documentation of how to do this.

Does anyone know how to get the start and end dates of a selected date range?

Here is what I have so far...

<template>
  <v-date-picker mode='range' v-model='range' is-inline :columns="$screens({ default: 1, lg: 2 })" />
</template>

<script>

  import { DatePicker } from 'v-calendar'

  export default {
    name: 'Booking',
    components: {
      DatePicker
    },
    data() {
      return {
        range: {
          start: new Date(),
          end: null
        }
      }
    },
    mounted() {
      this.$root.$on('input', (value) => {
        console.log('dxggdfg');
      });
    }
  }

</script>

Solution

  • Add input event

     <v-date-picker mode='range' v-model='range' @input="onDateRangeChange" is-inline :columns="$screens({ default: 1, lg: 2 })" />
    
    {
       ...
       methods: {
         onDateRangeChange() {
           console.log(this.range)
         }
       },
       mounted() {
          this.$root.$on('input', (value) => {
            console.log('dxggdfg');
          });
        }
    }
    

    Alternatively you can use watch, which works well if you also update your v-model externally:

    {
       ...
       watch: {
         range: {
            handler: function () {
                console.log(this.range)
            },
            deep: true
         }
       },
       mounted() {
          this.$root.$on('input', (value) => {
            console.log('dxggdfg');
          });
        }
    }