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 requirementsUse 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:
|
<<: Analysis of Linux boot system methods
>>: Detailed steps to install MYSQL8.0 on CentOS7.6
The knowledge points summarized below are all fre...
When using vue to develop projects, the front end...
Table of contents Question: When the button is cl...
The use of ElementUI paging component Pagination ...
1. Inner Join Query Overview Inner join is a very...
Table of contents What utilities does VueUse have...
The JD carousel was implemented using pure HTML a...
This article shares the specific code of js to im...
Basic concepts: Macvlan working principle: Macvla...
Translucent border Result: Implementation code: &...
This article records the detailed installation tu...
Preface Recently, when I was building a project, ...
Table of contents 1. Database design 2. Front-end...
1 Introduction In the article "Start Postgre...
Table of contents 1. Dep 2. Understand obverser 3...