Vue.js(19)之 封装calendar组件
效果
需求
1、实现一个日历组件,如图:
2、显示某天的事项:
3、事项是模拟父组件请求接口返回的,数据格式如下:
[{id: '232',date: '2019-06-01',info: '我要去吃大餐'},{id: '292',date: '2019-06-06',info: '我要去吃大餐'},{id: '292',date: '2019-06-07',info: '我要去吃大餐'},{id: '369',date: '2019-06-30',info: '我要去吃大餐'}]
4、把事项添加到日历组件中,数据格式如下:
代码解析
父组件页面:
<template><div class="test-container"><h1>Test页面,测试组件</h1><!-- 日历 --><calendar v-if="calendarVisible" @getDateInfo="getDateInfo" :propsInfoList="propsInfoList" :propsTime="propsTime"></calendar></div> </template><script> import calendar from '@/components/Calendar/Calendar.vue' export default {name: 'test',components: {"calendar": calendar},data() {return {calendarVisible: true,propsTime: '',propsInfoList: '',middle: [{id: '232',date: '2019-06-01',info: '我要去吃大餐'},{id: '292',date: '2019-06-06',info: '我要去吃大餐'},{id: '292',date: '2019-06-07',info: '我要去吃大餐'},{id: '369',date: '2019-06-30',info: '我要去吃大餐'}]}},created() {this.propsInfoList = JSON.stringify(this.middle)this.propsTime = this.getToday()},mounted() {window.alert('测试时间为19年 5、6、7月,完成是在6月')},methods: {// 格式化当前日期 YYYY-MM-DD getToday() {let nowDate = new Date()let yy = nowDate.getFullYear().toString()let mm = (nowDate.getMonth() + 1 + '').padStart(2,'0')let dd = (nowDate.getDate() + '').padStart(2,'0')// let hh = nowDate.getHours().toString().padStart(2,'0')// let mt = (nowDate.getMinutes() + '').padStart(2,'0')// let ss = (nowDate.getSeconds() + '').padStart(2,'0')return `${yy}-${mm}-${dd}` // -${hh}-${mt}-${ss} },// 组件传值 getDateInfo(year, month) {let _this = this_this.propsTime = `${year}-${month}`_this.calendarVisible = falsesetTimeout(() => {_this.propsInfoList = []let middleif(month == '05') {middle = [{id: '232',date: '2019-05-10',info: '我要去吃小餐'}]} else if (month == '06') {middle = _this.middle} else if (month == '07') {middle = [{id: '232',date: '2019-07-10',info: '我要去吃小餐'}]} else {middle = ''}_this.propsInfoList = JSON.stringify(middle)_this.calendarVisible = true}, 100)}} } </script>
日历子组件:
<template><div class="calendar-container"><h1>calendar</h1><div class="show-date" @click="clickData">{{showDate}}</div><div class="now-time">今日:{{exactTime}}</div><div class="calendar"><ul class="calendar-header"><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class="calendar-body"><li class="calendar-row" v-for="(item, index) in JSON.parse(calendarData)" :key="index"><span v-for="(subItem, subIndex) in item" :class="[subIndex == 0 || subIndex == 6? 'weekend': 'weekday', subItem.type == '1'? 'exact-time': '', subItem.type == '0'? 'already-time': '', subItem.type == '2'? 'soon-time': '']" @click="showInfo(subItem)" :key="subIndex">{{subItem.date}}</span></li></ul></div><mt-popup v-model="popupVisible" position="bottom"><mt-picker :slots="slots" :showToolbar="true" :visibleItemCount="5" :itemHeight="itemsHeight" ref="picker"><img src="@/assets/images/picker_cancel.png" class="picker_cancel" v-on:click="cancelFunc()"><img src ="@/assets/images/picker_sure.png" class="picker_sure" v-on:click="sureFunc()"></mt-picker></mt-popup></div> </template>
日历子组件逻辑:
import { MessageBox } from 'mint-ui' export default {name: "calendar",props: {propsTime: String,propsInfoList: String},data() {return {time: '',infoList: '',calendarData: [],showDate: '',exactTime: '',itemsHeight: 95 * window.screen.height / 1334,popupVisible: false,slots: []}},created() {this.infoList = this.propsInfoListthis.time = this.propsTime.split('-')const date = this.getToday()this.exactTime = date.slice(0,3).join('-')this.getCalendar(...(this.time))this.getSlotsArray(...(date.slice(0,2)))},methods: {// 日历组件 getCalendar(year, month) {let _this = thisconst rightNow = _this.exactTime_this.showDate = `${year}-${monthconst firstDate = new Date(year, month - 1, 1)const firstDay = firstDate.getDay()const isLeapYear = year % 100 == 0? year % 400 == 0? 1: 0: year % 4 == 0 ? 1: 0const monthArray = [31, 28 + isLeapYear, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]const weeekLines =Math.ceil((monthArray[month - 1] + firstDay)/7)let calendar = []for(let i = 0; i < weeekLines; i++) {let weeekLinesInfo = []for(let j = 0; j < 7; j++) {const cellNo = i * 7 + jconst datePerLine = cellNo - firstDay + 1if(datePerLine <= 0 || datePerLine > monthArray[month - 1]) {let outOfMonth = {"type" : 'null',"date" : ''}weeekLinesInfo[j] = outOfMonth} else {let day = (datePerLine + '').padStart(2,'0')let inOfMonth = {"type" : '',"date" : day,"isDone": '',"infor": ''}const propsDate = `${year}-${month}-${day}`if(propsDate == rightNow){inOfMonth.type = "1"}const reservations = JSON.parse(_this.infoList)for(let k = 0; k < reservations.length; k++) {if(propsDate == reservations[k].date){// inOfMonth.type = "1"inOfMonth.infor = reservations[k].infoif(rightNow == reservations[k].date) {inOfMonth.type = "1"inOfMonth.isDone = "doing"} else if (rightNow > reservations[k].date) {inOfMonth.type = "0"inOfMonth.isDone = "pass"} else if (rightNow < reservations[k].date) {inOfMonth.type = "2"inOfMonth.isDone = "will"}}}weeekLinesInfo[j] = inOfMonth}}calendar.push(weeekLinesInfo)}window.console.log(calendar)_this.calendarData = JSON.stringify(calendar)},// 格式化当前日期 YYYY-MM-DD getToday() {let nowDate = new Date()let yy = nowDate.getFullYear().toString()let mm = (nowDate.getMonth() + 1 + '').padStart(2,'0')let dd = (nowDate.getDate() + '').padStart(2,'0')let hh = nowDate.getHours().toString().padStart(2,'0')let mt = (nowDate.getMinutes() + '').padStart(2,'0')let ss = (nowDate.getSeconds() + '').padStart(2,'0')return [yy, mm, dd, hh, mt, ss]// return `${yy}-${mm}-${dd}-${hh}-${mt}-${ss}` },// 组装 picker 数组 getSlotsArray(year, month){let _this = thislet yearArray = []for(let i = -10 ; i <= 10 ; i ++){yearArray.push(year - 1 + i)}let monthArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]let slots = [{values:yearArray,className:"slot1",defaultIndex: 11},{values:monthArray,className:"slot2",defaultIndex:month - 1}]_this.slots = slots},// 显示日期弹窗 clickData(){this.popupVisible = true},// 取消按钮 cancelFunc(){this.popupVisible = false;},// 确认按钮 sureFunc() {let _this = this_this.popupVisible = falseconst clickData = _this.$refs.picker.getValues()const year = clickData[0] + ''const month = (clickData[1] + '').padStart(2,'0')const day = _this.time[2]_this.getDateInfo(year, month)_this.getCalendar(year, month)},// 调用父组件定义的方法 getDateInfo(year, month) {this.$emit('getDateInfo', year, month)},// 点击展示某天的事项信息 showInfo(info) {let _this = thisconst infor = infoif(infor.infor) {const [year, month] = _this.showDate.split('-')console.log(year, month, info)const titleDate = `${year}-${month}-${info.date}`const preview = info.inforMessageBox({title: titleDate,message: preview,showCancelButton: false,closeOnClickModal: true})}}} }
其他:为了减少篇幅,省略样式
github地址
转载于:https://www.cnblogs.com/houfee/p/10987447.html
Vue.js(19)之 封装calendar组件相关推荐
- vue.js仿微信聊天窗口展示组件
原链接:https://www.jianshu.com/p/0faf8e78d0a5 源码:https://github.com/doterlin/vue-wxChat 演示地址:https://do ...
- Vue.js(十) element-ui PC端组件库
一:简介 饿了么公司基于Vue开发了两套UI组件库,PC端组件库 和 移动端组件库. 一部分组件库是对原生的HTML标签元素的封装,增加了一些新的功能. 另一部分组件库是原生HTML标签元素没有的,是 ...
- Vue.js(5)- 全局组件
全局组件 定义组件的语法 Vue.component('组件的名称', { 组件的配置对象 }) 在组件的配置对象中:可以使用 template 属性指定当前组件要渲染的模板结构: 使用组件的语法 把 ...
- Vue.js 3 Step 创建一个组件
Step1:Vue.extend()创建组件 Step2:Vue.component()注册组件,注册的标签一定要用小写 Step3:挂载点使用组件 <!doctype html> < ...
- Vue.js(8)- 父组件给子组件传值
父组件给子组件传值原理:属性绑定 第一步:建立连接 B.vue <template><div><h3>这是B.vue文件内容</h3><h4> ...
- 【Vue.js】Vue.js中常用的UI组件库和Vue Router
1.Vue生态中常用的UI组件库 1. vant 介绍 轻量级.可靠的移动端 Vue 组件库 有赞前端团队出品 GitHub地址:https://github.com/youzan/vant 特性 拥 ...
- vue.js中<Transition> 组件
在vue.js中发现 这个过渡动画的组件 简单的可以使用 如果需要复杂的动画效果可以自己引入动画库
- Vue实例——使用canvas封装polyline组件绘制一个五角星
1. 需求及效果 在看高德API时,发现有一个polyline组件,通过接收点的坐标数组,来绘制折线,正在看canvas API,就让我们一起试试吧. 2. 实施 思路:首先子组件需要接收父组件传递进 ...
- Vue.js中的8种组件间的通信方式;3个组件实例是前6种通信的实例,组件直接复制粘贴即可看到运行结果
文章目录 一.$children / $parent 二.props / $emit 三.eventBus 四.ref 五.provide / reject 六.$attrs / $listeners ...
最新文章
- Domino URL Command 详解
- java string... 参数_Java String.Format() 方法及参数说明
- jdbc连接oracle数据库
- php根据时间搜索的控件,yii2 crud生成的搜索中 自定义按 时间 搜索
- java开发_读写txt文件操作
- linux怎么新建系统用户名,在 Linux 中不使用 useradd 命令如何创建用户账号
- Mysql授权root账户允许远程连接访问
- Linux服务器开发学习方法
- Android 4.1 Netd详细分析(一)概述与应用实例
- cfree 上面工具栏消失解决办法(不用重下!!!!!)
- 上传文件submit提交form表单 success返回数据多了div获取不到正确的返回数据
- safari看html5卡顿,MacOS下Safari 10浏览器卡顿解决方案整理 - YangJunwei
- 寒心啊!我的威望值居然是-1,整个论坛估计没几人像我这样
- 利用Python提取PDF文件中的文本信息
- 回顾2022! 链上NFT精彩项目大盘点
- 计算机管理员注销,肿么注销计算机管理员
- 一文聊透Netty核心引擎Reactor的运转架构
- python脚本来控制securecrt_SecureCRT 使用python脚本
- Marked.js - HTML 中直接解析显示 Markdown
- 超超超超写实的数字人!让你24小时不停播
热门文章
- python定义/使用函数(方法)
- 服装店用什么收银软件?
- 【Front. Plant Sci.】同时对三个PPO基因进行CRISPR/Cas9编辑可减缓茄子果肉褐变
- Python实现制作原创图片的方法(文字转图片)
- linux下链接so提示skipping incompatible ../bin/xxx.so when searching for -lxxx
- 前端开发-Vue-element 电商管理系统
- 阿里云ACA认证考试
- Faster-RCNN PRN网络解析
- 关于VCP(Virtual Com Port)拓展的调试经历(三)
- 三种计算开关电源控制器结温的方法