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

Vue.js framework implements shopping cart function

This article shares the specific code of Vue.js f...

Detailed explanation of JavaScript function introduction

Table of contents Function Introduction function ...

MySQL 8.0.12 installation and configuration method graphic tutorial

Record the installation and configuration method ...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

How to configure VMware multi-node environment

This tutorial uses CentOS 7 64-bit. Allocate 2GB ...

How to use a game controller in CocosCreator

Table of contents 1. Scene layout 2. Add a handle...

CSS optimization skills self-practice experience

1. Use css sprites. The advantage is that the smal...

JS quickly master ES6 class usage

1. How to construct? Let's review the common ...

nginx automatically generates configuration files in docker container

When a company builds Docker automated deployment...

A brief discussion on size units in CSS

The compatibility of browsers is getting better a...

How to query duplicate data in mysql table

INSERT INTO hk_test(username, passwd) VALUES (...

Detailed steps to install Nginx on Linux

1. Nginx installation steps 1.1 Official website ...

Solution to forget password when installing MySQL on Linux/Mac

Preface This article mainly introduces the releva...