Vue custom optional time calendar component

Vue custom optional time calendar component

This article example shares the specific code of the vue custom optional time calendar component for your reference. The specific content is as follows

Calendar features:

1. Past time cannot be selected
2. Customizable unselectable time
3. The default display date for this month is the first day, and the first day for other months is the first day. If the time is not selectable, it will be postponed to the next day.

Effect picture:

------- Let's start the show -----------

**First, draw the calendar page layout, refer to the win10 system calendar layout* 6 rows and 7 columns, why is it so, please understand it yourself...* I also "peeked"

beginDay is the day of the week of the first day of the current month, prevMdays is the total number of days in the previous month, and nowMdays is the total number of days in the current month. This achieves the calendar display effect, and some data that may be used is bound to the tag, such as data-dates, etc.

<div class="dateContent-body-day" v-for="item in 42" :key="item">
            <div
              v-if="item - beginDay > 0 && item - beginDay <= nowMdays"
              :class="{
                'active-day': `${year}/${month}/${item - beginDay}` == curDate
              }"
              @click="dayHandler"
              :data-year="year"
              :data-month="month"
              :data-day="item-beginDay"
              :data-dates="year + '/' + month + '/' + (item - beginDay)"
            >
              {{ item-beginDay }}
            </div>
            <div class="other-day" v-else-if="item - beginDay <= 0">
              {{ item - beginDay + prevMdays }}
    </div>
  <div class="other-day" v-else>{{ item - beginDay - nowMdays }}</div>
</div>

— Next…

Data used:

*active-day is the highlighted day, i.e. the selected date. curDate controls which day is selected. Here the default is today. A props data timeArry is opened to allow some custom dates to be passed in as unselectable. If the dates bound to the clicked date exist in the array, they are returned. If selectable, the selected result is exposed through the chooseDate event through $emit.

//Click to switch day dayHandler(e) {
      console.log(e);
      var daNum = e.target.dataset;
      if (this.cantTime.indexOf(daNum.dates) > -1) {
        this.$toast("Not an open date, cannot be selected");
        return;
      }
      if (
        `${daNum.year}/${daNum.month}/${daNum.day}` >=
        `${new Date().getFullYear()}/${new Date().getMonth() +
          1}/${new Date().getDate()}`
      ) {
        this.date = e.target.dataset.day;
        this.$emit(
          "chooseDate",
          this.year + "/" + this.month + "/" + this.date
        );
      } else {
        this.$toast("Past time cannot be selected");
      }
    },
    //Switch to next month``nextMonth() {
      if (this.month == 12) {
        this.month = 1;
        this.year++;
      } else {
        this.month++;
      }
},

The switching of months and days must be observed. The focus is on observing the changes in months. I don't know if the watch has been abused.

```javascript
watch:
    date(val, oldval) {
      if (val) {
        this.curDate = `${this.year}/${this.month}/${this.date}`;
      }
    },
    month(val, oldval) {
      if (val) {
        var ndate;
        for (var i = 1; i <= 31; i++) {
          console.log(`${this.year}/${this.month}/${i}`);
          if (this.cantTime.indexOf(`${this.year}/${this.month}/${i}`) < 0) {
            console.log("No value exists, stop, date stays at " + i);
            ndate = i;
            break;
          }
        }
        console.log(ndate, `${this.year}/${this.month}/${ndate}`);
        //Compare the current month with the current day. The default for future months is 1, and the default for the current month is today if (
          `${this.year}/${this.month}/1` >
          `${new Date().getFullYear()}/${new Date().getMonth() +
            1}/${new Date().getDate()}`
        ) {
          this.curDate = `${this.year}/${this.month}/${ndate}`;
          this.date = ndate;
        } else {
          for (var i = new Date().getDate(); i <= 31; i++) {
            console.log(2`${this.year}/${this.month}/${i}`);
            if (this.cantTime.indexOf(`${this.year}/${this.month}/${i}`) < 0) {
              this.curDate = `${new Date().getFullYear()}/${new Date().getMonth() +
            1}/${i}`;
          this.date = i;
              break;
            }
          }
        }
        this.$emit(
          "chooseDate",
          this.year + "/" + this.month + "/" + this.date
        );
      }
    }
  },

Called in the parent component

<calendar :timeArray="timeArray" @chooseDate="chooseHandler"></calendar>
import { calendar ,alertBox} from '@/components/index.js';
export default {
    components:{calendar,alertBox
    },

This completes the calendar.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue implements calendar component
  • Vue implements vertical infinite sliding calendar component
  • Encapsulation method of Vue calendar component
  • Write a vue calendar component from scratch
  • Sample code using the element calendar component in Vue
  • Improved calendar component based on Vue2-Calendar (including Chinese instructions)
  • Vue implements a cool calendar component
  • VUE implements calendar component function
  • Vue version of handwritten calendar component

<<:  Usage of MySQL time difference functions TIMESTAMPDIFF and DATEDIFF

>>:  How to enable or disable SSH for a specific user or user group in Linux

Recommend

Detailed explanation of common methods of JavaScript String

Table of contents 1. charAt grammar parameter ind...

How to perfectly implement the grid layout with intervals on the page

Typical layout examples As shown in the above pic...

Analysis of Mysql data migration methods and tools

This article mainly introduces the analysis of My...

How to use Font Awesome 5 in Vue development projects

Table of contents Install Dependencies Configurat...

Simple setup of VMware ESXi6.7 (with pictures and text)

1. Introduction to VMware vSphere VMware vSphere ...

Analysis of the advantages and disadvantages of MySQL stored procedures

MySQL version 5.0 began to support stored procedu...

In-depth analysis of Nginx virtual host

Table of contents 1. Virtual Host 1.1 Virtual Hos...

Analysis of MySQL's planned tasks and event scheduling examples

This article uses examples to describe MySQL'...

Weird and interesting Docker commands you may not know

Intro Introduces and collects some simple and pra...

How to use explain to query SQL execution plan in MySql

The explain command is the primary way to see how...

Count the list tags in HTML

1. <dl> defines a list, <dt> defines ...

Nginx local directory mapping implementation code example

Sometimes you need to access some static resource...