Vue.js implements calendar function

Vue.js implements calendar function

This article example shares the specific code of Vue.js to implement the calendar function for your reference. The specific content is as follows

Github

Functional requirements

Use Vue.js to implement a calendar table for a specified year and month, and use the background interface data to add information such as the air quality description for the corresponding date. The background transmits the data of the air quality index for the current month, and the front end generates a calendar and adds the air quality index to the corresponding date.

Example of air quality data:

data.json

{
  "code": 200,
  "msg": "",
  "data": [{
      "time": "2020-08-01",
      "kqzl": "Excellent"
    },
    {
      "time": "2020-08-02",
      "kqzl": "good"
    },
    {
      "time": "2020-08-03",
      "kqzl": "good"
    }
  ]
}

Implementation

<template>
  <div id="app">
    <h1 style="text-align:center;">
      2020-08</h1>
    <div class="calendar-container">
      <div class="calendar-week">
        <div class="cw-inner">
          <div class="cw-item"
               :style="{width: setItemWidth}"
               v-for="(item, index) of week"
               :key="index">
            {{item}}
          </div>
        </div>
      </div>
      <div class="calendar-day">
        <div class="cd-list"
             v-for="(item, index) of day"
             :key="index">
          <div class="cl-item"
               :style="{width: setItemWidth}"
               v-for="(child,index) of item"
               :key="index"
               :class="[{has: child}]">
            <div class="ci-inner"
                 v-if="child">
              <span>{{child.date}}</span>
              <span v-if="child.text"
                    class="aqi"
                    :class="child.text.kqzl">
                {{child.text.kqzl}}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import kqzlData from './assets/data.json' // Air quality data export default {
  name: 'app',
  data() {
    return {
      week: [
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
      ],
      day: [],
    }
  },
  computed: {
    setItemWidth() {
      return 100 / 7 + '%'
    },
  },
  mounted() {
    this.createCalendar(2020, 8)
  },
  methods: {
    /**
     * @name: format date * @param {date}
     */
    dateFormat(date) {
      let dateArr = date.split('-')
      let mounth = dateArr[1] >= 10 ? dateArr[1] : '0' + dateArr[1]
      let day = dateArr[2] >= 10 ? dateArr[2] : '0' + dateArr[2]
      return dateArr[0] + '-' + mounth + '-' + day
    },

    /**
     * @name: date information * @param {date}
     */
    getDayInfo(date) {
      let kqzl = kqzlData.data
      let formatDate = this.dateFormat(date)
      let txt = kqzl[kqzl.findIndex(item => item.time === formatDate)]
      return txt
    },

    /**
     * @name: Generate calendar table * @param {year}
     * @param {mounth}
     */
    createCalendar(year, mounth) {
      // How many days are there in a month let allDay = new Date(year, mounth, 0).getDate()
      // What day of the week is the 1st of a month let firstDay = this.judjeFirstDay(year, mounth)
      // How many rows are needed to display let row = Math.ceil((allDay + firstDay) / 7)
      let k = firstDay - 1
      let result = []
      let count = 1
      // Generate calendar two-dimensional array for (let i = 0; i < row; i++) {
        result[i] = new Array(7)
        do {
          if (count <= allDay) {
            result[i][k] = {
              date: count,
              // Match the information corresponding to the date according to the interface text: this.getDayInfo(year + '-' + mounth + '-' + count),
            }
            k++
            count++
          } else {
            break
          }
        } while (k < 7)
        k = 0
      }
      this.day = result
    },

    /**
     * @name: Determine what day of the week the 1st of a certain month is* @param {year}
     * @param {mounth}
     */
    judjeFirstDay(year, mounth) {
      let date = new Date(year, mounth - 1, 1)
      let week = date.getDay()
      let weekArr = [1, 2, 3, 4, 5, 6, 7]
      return weekArr[week - 1]
    },
  },
}
</script>

<style lang="scss">
.calendar-container {
  text-align: center;
  .calendar-week {
    margin-bottom: 4px;
    padding: 0 4px;
    color: #fff;
    .cw-inner {
      overflow: hidden;
      background: #a0a0a0;
      .cw-item {
        float: left;
        padding: 8px 0;
      }
    }
  }
  .calendar-day {
    .cd-list {
      overflow: hidden;
      .cl-item {
        float: left;
        min-height: 30px;
        box-sizing: border-box;
        padding: 4px;
        .ci-inner {
          background: #f5f5f5;
          padding: 8px 0;
          span {
            display: inline-block;
            &.aqi {
              color: #fff;
              background: #a7cf8c;
              padding: 0 4px;
              border-radius: 4px;
            }
            &.excellent{
              background: #a7cf8c;
            }
            &.good{
              background: #f7da64;
            }
            &. Mild {
              background: #f29e39;
            }
            &.Moderate
              background: #da555d;
            }
            &.Severe
              background: #b9377a;
            }
            &.serious{
              background: #881326;
            }
          }
        }
      }
    }
  }
}
</style>

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.js+boostrap project practice (case detailed explanation)
  • Vue.js implements tab switching and color change operation explanation
  • Detailed explanation of the use of $emit in Vue.js
  • Detailed explanation of the usage of scoped slots in Vue.js slots
  • Vue.js implements timeline function
  • Vue.js manages the encapsulation of background table components
  • Ideas and practice of multi-language solution for Vue.js front-end project
  • 10 Best Practices for Building and Maintaining Large-Scale Vue.js Projects

<<:  Analysis of Linux boot system methods

>>:  Detailed steps to install MYSQL8.0 on CentOS7.6

Recommend

A comprehensive summary of frequently used statements in MySQL (must read)

The knowledge points summarized below are all fre...

How to use Dayjs to calculate common dates in Vue

When using vue to develop projects, the front end...

Detailed explanation of the role of key in React

Table of contents Question: When the button is cl...

How to use ElementUI pagination component Pagination in Vue

The use of ElementUI paging component Pagination ...

Have you really learned MySQL connection query?

1. Inner Join Query Overview Inner join is a very...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

Pure HTML and CSS to achieve JD carousel effect

The JD carousel was implemented using pure HTML a...

js to realize a simple disc clock

This article shares the specific code of js to im...

Docker deploys Macvlan to achieve cross-host network communication

Basic concepts: Macvlan working principle: Macvla...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

CentOS 7 installation and configuration method graphic tutorial

This article records the detailed installation tu...

Simple encapsulation of axios and example code for use

Preface Recently, when I was building a project, ...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...

Solution for creating multiple databases when Docker starts PostgreSQL

1 Introduction In the article "Start Postgre...

Implement a simple data response system

Table of contents 1. Dep 2. Understand obverser 3...