Categories
Vuetify

Vuetify — Date Pickers and Events

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Date Pickers — Multiple

We can pick multiple dates if we add the multiple prop.

For example, we can write:

<template>
  <v-container>
    <v-row justify="center">
      <v-date-picker v-model="dates" multiple></v-date-picker>
    </v-row>
    <v-row justify="center">
      <p>{{dates}}</p>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    dates: ["2020-09-15", "2020-09-20"],
  }),
};
</script>

to show a date picker that lets us pick multiple dates.

The dates is an array of date strings.

Date Pickers — Range

We can add the range prop to set the range.

For example, we can write:

<template>
  <v-container>
    <v-row justify="center">
      <v-date-picker v-model="dates" range></v-date-picker>
    </v-row>
    <v-row justify="center">
      <p>{{dates}}</p>
    </v-row>
  </v-container>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    dates: ["2020-09-15"],
  }),
};
</script>

Now we can choose a range of dates.

Date Pickers — Birthday Picker

We can make a birthday picker by restructuring the date range and closing the date picker after selecting the day.

For example, we can write:

<template>
  <v-menu
    ref="menu"
    v-model="menu"
    :close-on-content-click="false"
    transition="scale-transition"
    offset-y
    min-width="290px"
  >
    <template v-slot:activator="{ on, attrs }">
      <v-text-field
        v-model="date"
        label="Birthday date"
        prepend-icon="event"
        readonly
        v-bind="attrs"
        v-on="on"
      ></v-text-field>
    </template>
    <v-date-picker
      ref="picker"
      v-model="date"
      :max="new Date().toISOString().substr(0, 10)"
      min="1950-01-01"
      @change="save"
    ></v-date-picker>
  </v-menu>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: null,
    menu: false,
  }),
  watch: {
    menu(val) {
      val && setTimeout(() => (this.$refs.picker.activePicker = "YEAR"));
    },
  },
  methods: {
    save(date) {
      this.$refs.menu.save(date);
    },
  },
};
</script>

We set the date picker to change the mode if we open the menu to 'YEAR' .

This way, we’ve to pick the year first and then pick the month and day.

Once the date changes, the save method is called to save the value to the v-model ‘s date, which is date .

Date Pickers Events

We can show events within the date picker.

For example, we can write:

<template>
  <v-row justify="space-between">
    <div>
      <div class="subheading">Defined by array</div>
      <v-date-picker v-model="date1" :events="arrayEvents" event-color="green lighten-1"></v-date-picker>
    </div>
    <div>
      <div class="subheading">Defined by function</div>
      <v-date-picker
        v-model="date2"
        :event-color="date => date[9] % 2 ? 'red' : 'yellow'"
        :events="functionEvents"
      ></v-date-picker>
    </div>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    arrayEvents: null,
    date1: new Date().toISOString().substr(0, 10),
    date2: new Date().toISOString().substr(0, 10),
  }),

  mounted() {
    this.arrayEvents = [...Array(6)].map(() => {
      const day = Math.floor(Math.random() * 30);
      const d = new Date();
      d.setDate(day);
      return d.toISOString().substr(0, 10);
    });
  },

  methods: {
    functionEvents(date) {
      const [, , day] = date.split("-");
      if ([1, 2, 3].includes(parseInt(day, 10))) return true;
      if ([10, 11, 12].includes(parseInt(day, 10))) return ["red", "#00f"];
      return false;
    },
  },
};
</script>

In the first date picker, we set the events prop to the arrayEvents array.

It’s an array with some dates.

We can also set events to a function that returns a boolean.

We can return a function with a boolean or a color string for the dot.

Conclusion

We can add date pickers to display events and change the mode on the fly.

Categories
Vuetify

Vuetify — Month Pickers

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Month Pickers — Allowed Months

We can change the allowed months with the date picker.

For example, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker
      v-model="date"
      :allowed-dates="allowedMonths"
      type="month"
      class="mt-4"
      min="2020-06"
      max="2020-10"
    ></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
  methods: {
    allowedMonths: (val) => parseInt(val.split("-")[1], 10) % 2 === 0,
  },
};
</script>

We have the allowedMonths method to return the condition for the date to be allowed.

Also, we have the min and max props to set the date range.

Multiple Month Pickers

We can allow a month picker to let users select multiple months.

For example, we can write:

<template>
  <v-row justify="center">
    <v-date-picker v-model="months" type="month" multiple></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    months: ["2020-09", "2020-10"],
  }),
};
</script>

to let us pick multiple months.

The multiple prop allows for multiple selections.

v-model ‘s value is an array of date strings instead of a date string.

Month Pickers — Setting Picker Width

We can set the month picker’s width.

The width prop sets the width:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" type="month" width="290" class="mt-4"></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

Also, we can set the full-width prop to make the month picker fill the screen:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" type="month" full-width class="mt-4"></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

Month Pickers Internationalization

The locale prop lets us set the language of the month picker.

For example, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker v-model="date" type="month" locale="sv-se"></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

We set the month picker to display in Swedish with the locale prop.

Month Picker Icons

The month picker icon lets us set the icons with various props.

For example, we can write:

<template>
  <v-row justify="center">
    <v-date-picker
      v-model="date"
      type="month"
      year-icon="mdi-calendar-blank"
      prev-icon="mdi-skip-previous"
      next-icon="mdi-skip-next"
    ></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

We set the year-icon , prev-icon and next-icon props to set the year and navigation icons.

Conclusion

We can set the month picker component with various props to style them.

Categories
Vuetify

Vuetify — Date Picker Customization

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Date Picker’s Elevation

We can set the date picker’s elevation with the flat and elevation props.

To use the flat prop, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker v-model="date" flat></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

to make it flat.

And we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker v-model="date" elevation="15"></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

to add a shadow below the date picker.

Date Pickers — React to Displayed Month/Year Change

We can watch the v-model value with our own watcher.

For instance, we can write:

<template>
  <v-row>
    <v-col cols="12" sm="6" class="my-2 px-1">
      <v-date-picker ref="picker" v-model="date" :picker-date.sync="pickerDate" full-width></v-date-picker>
    </v-col>
    <v-col cols="12" sm="6" class="my-2 px-1">
      <div class="title">Month ({{ date || 'change month...' }})</div>
      <ul class="ma-4">
        <li v-for="note in notes" :key="note">{{ note }}</li>
      </ul>
    </v-col>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
    pickerDate: null,
    notes: [],
    allNotes: ["foo", "bar", "baz"],
  }),
  watch: {
    pickerDate(val) {
      this.notes = [
        this.allNotes[Math.floor(Math.random() * 3)],
        this.allNotes[Math.floor(Math.random() * 3)],
      ];
    },
  },
};
</script>

The picker-date prop has the picked month value.

Then we can watch the pickerDate value and do what we want as the month value changes.

Date Pickers Internationalization

The Vuetify date picker supports internationalization.

For example, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker v-model="date" :first-day-of-week="0" locale="zh-cn"></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

We have the locales with the locale of the date picker.

The first-day-of-week prop sets the first day of the week to the day we want.

0 is Sunday and it goes up to 6 for Saturday.

Date Picker Icons

We can set the date picker icon to what we want.

For example, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker
      v-model="date"
      year-icon="mdi-calendar-blank"
      prev-icon="mdi-skip-previous"
      next-icon="mdi-skip-next"
    ></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

We change the year-icon , prev-icon , and next-icon let us change the year and navigation icons respectively.

Read Only Date Pickers

We can make the date picker read-only with the readonly prop:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" readonly></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

Now we can’t pick anything from the date picker.

Conclusion

We can customize date pickers the way we want with Vuetify.

Categories
Vuetify

Vuetify — Date and Month Picker Format

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Month Pickers Read Only

We can make a month picker read-only with the readonly prop:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" type="month" readonly></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

Month Pickers Current Month Indicator

The current month indicator can be changed with the show-current prop:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" type="month" :show-current="false"></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

The show-current prop set to false makes the selected month lighter.

We can also set it to a month string:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" type="month" show-current="2020-08"></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 7),
  }),
};
</script>

and the selected month will have a border around it.

Date Pickers in Dialog and Menu

We can put a date picker in a dialog box.

For example, we can write:

<template>
  <v-row justify="center">
    <v-col cols="12" sm="6" md="4">
      <v-menu
        ref="menu"
        v-model="menu"
        :close-on-content-click="false"
        :return-value.sync="date"
        transition="scale-transition"
        offset-y
        min-width="290px"
      >
        <template v-slot:activator="{ on, attrs }">
          <v-text-field
            v-model="date"
            label="Picker in menu"
            prepend-icon="event"
            readonly
            v-bind="attrs"
            v-on="on"
          ></v-text-field>
        </template>
        <v-date-picker v-model="date" no-title scrollable>
          <v-spacer></v-spacer>
          <v-btn text color="primary" @click="menu = false">Cancel</v-btn>
          <v-btn text color="primary" @click="$refs.menu.save(date)">OK</v-btn>
        </v-date-picker>
      </v-menu>
    </v-col>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
    menu: false,
  }),
};
</script>

We put our v-date-picker in a v-menu so that we can show the date picker when we click on the menu icon.

We put the v-text-field in the activator slot so that we can use the on object to listen to date picker events.

And we use the attrs object to set the props for the v-text-field .

This way, when we pick a date from the date picker, its value will be shown in the text field.

prepend-icon shows the icon.

Date Pickers — Formatting Date

We can format dates from the date picker.

For example, we can write:

<template>
  <v-row justify="center">
    <v-col cols="12" sm="6" md="4">
      <v-menu
        ref="menu"
        v-model="menu"
        :close-on-content-click="false"
        :return-value.sync="date"
        transition="scale-transition"
        offset-y
        min-width="290px"
      >
        <template v-slot:activator="{ on, attrs }">
          <v-text-field
            v-model="computedDateFormatted"
            label="Picker in menu"
            prepend-icon="event"
            readonly
            v-bind="attrs"
            v-on="on"
          ></v-text-field>
        </template>
        <v-date-picker v-model="date" no-title scrollable>
          <v-spacer></v-spacer>
          <v-btn text color="primary" @click="menu = false">Cancel</v-btn>
          <v-btn text color="primary" @click="$refs.menu.save(date)">OK</v-btn>
        </v-date-picker>
      </v-menu>
    </v-col>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
    menu: false,
  }),
  computed: {
    computedDateFormatted() {
      return this.formatDate(this.date);
    },
  },
  methods: {
    formatDate(date) {
      if (!date) return null;

      const [year, month, day] = date.split("-");
      return `${month}/${day}/${year}`;
    },
  },
};
</script>

We set the v-model of the v-text-field to the computedDateFormatted computed property.

It formats the date string with the formatDate method, which splits the original date string, and combines the parts with slashes.

Then we should see the formatted date with the text field.

Conclusion

We can set the month picker to be read-only and format the date to what we want.

Categories
Vuetify

Vuetify — Date and Month Picker

Vuetify is a popular UI framework for Vue apps.

In this article, we’ll look at how to work with the Vuetify framework.

Date Picker’s Current Date Indicator

We can change the current date indicator with the show-current prop.

For example, we can write:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" :show-current="false"></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

We make the circle around the current date lighter with it.

It can also be set to a date string:

<template>
  <v-row justify="center">
    <v-date-picker v-model="date" show-current="2020-07-13"></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

Then we’ll see the given date have a circle around it.

Date Pickers — DOM Events for Year, Month, and Date Buttons

Date pickers emit various events for year, month, and date buttons.

We can listen to events like @click, @dblclick, @mouseenter, and more events on the date picker.

For example, we can write:

<template>
  <v-row>
    <v-col class="my-2 px-1" cols="12" sm="6">
      <v-date-picker
        v-model="date"
        @contextmenu:year="contextMenu"
        @dblclick:date="dblClick"
        @mouseenter:month="mouseEnter"
        @mouseleave:month="mouseLeave"
      ></v-date-picker>
    </v-col>

    <v-col class="my-2 px-1" cols="12" sm="6">
      <div class="title mb-2">{{ mouseMonth }}</div>
    </v-col>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
    mouseMonth: null,
  }),

  methods: {
    contextMenu(year, event) {
      event.preventDefault();
      alert(`You have activated context menu for year ${year}`);
    },
    dblClick(date) {
      alert(`You have just double clicked the following date: ${date}`);
    },
    mouseEnter(month) {
      this.mouseMonth = month;
    },
    mouseLeave() {
      this.mouseMonth = null;
    },
  },
};
</script>

We listen to the mouseenter and mouseleave events on the month picker and get its value with the handler.

The month modifier indicates that we listen to month picker actions.

The contextmenu has the year modifier to listen to the year.

Month Pickers

We can create a month picker by setting v-date-picker ‘s type prop to month .

For example, we can write:

<template>
  <v-row>
    <v-col class="my-2 px-1" cols="12" sm="6">
      <v-date-picker v-model="date" type="month"></v-date-picker>
    </v-col>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

The landscape prop makes the date picker display in landscape mode.

For example, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker v-model="date" type="month" landscape></v-date-picker>
  </v-row>
</template>
<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

Month Pickers Colors

We can change the color of the month picker with the color and header-color props.

For example, we can write:

<template>
  <v-row justify="space-around">
    <v-date-picker v-model="date" type="month" color="green lighten-1"></v-date-picker>
  </v-row>
</template>

<script>
export default {
  name: "HelloWorld",
  data: () => ({
    date: new Date().toISOString().substr(0, 10),
  }),
};
</script>

The header-color sets the color of the date picker header.

Conclusion

We can change the date picker with various styles.

The v-date-picker component can also be a month picker.