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

Summary of flex layout compatibility issues

1. W3C versions of flex 2009 version Flag: displa...

A brief discussion on size units in CSS

The compatibility of browsers is getting better a...

Explanation and example usage of 4 custom instructions in Vue

Four practical vue custom instructions 1. v-drag ...

Introduction to Javascript DOM, nodes and element acquisition

Table of contents DOM node Element node: Text nod...

Vue 2.0 Basics in Detail

Table of contents 1. Features 2. Examples 3. Opti...

Detailed explanation of Linux command file overwrite and file append

1. The difference between the command > and &g...

Detailed tutorial on integrating Apache Tomcat with IDEA editor

1. Download the tomcat compressed package from th...

echars 3D map solution for custom colors of regions

Table of contents question extend Solving the pro...

Docker solves the problem that the terminal cannot input Chinese

Preface: One day, I built a MySQL service in Dock...

Native js to achieve star twinkling effect

This article example shares the specific code of ...

Detailed analysis of mysql MDL metadata lock

Preface: When you execute a SQL statement in MySQ...

Methods and techniques for designing an interesting website (picture)

Have you ever encountered a situation where we hav...

MySQL changes the default engine and character set details

Table of contents 1. Database Engine 1.1 View dat...

Detailed explanation of MySQL date string timestamp conversion

The conversion between time, string and timestamp...