uni-app 日历组件的实现
以下是一个基于 uni-app 的简单日历组件实现代码,包括了日历的基本布局和日期选择功能:
<template><view class="calendar"><view class="calendar-header"><view class="calendar-prev" @click="prevMonth">上个月</view><view class="calendar-title">{{ year }}年{{ month }}月</view><view class="calendar-next" @click="nextMonth">下个月</view></view><view class="calendar-body"><view class="calendar-weekdays"><viewclass="calendar-weekday"v-for="(weekday, index) in weekdays":key="index">{{ weekday }}</view></view><view class="calendar-dates"><viewclass="calendar-date"v-for="(date, index) in dates":key="index":class="{ 'calendar-date-today': isToday(date), 'calendar-date-selected': isSelected(date) }"@click="selectDate(date)">{{ date || '' }}</view></view></view></view>
</template><script>export default {name: "calendar",data() {return {year: new Date().getFullYear(),month: new Date().getMonth() + 1,weekdays: ["日", "一", "二", "三", "四", "五", "六"],selectedDate: null,};},computed: {dates() {const firstDayOfMonth = new Date(this.year, this.month - 1, 1);const lastDayOfMonth = new Date(this.year, this.month, 0);const dates = [];for (let i = 1; i <= lastDayOfMonth.getDate(); i++) {dates.push(i);}for (let i = 1; i < firstDayOfMonth.getDay(); i++) {dates.unshift(null);}return dates;},},methods: {prevMonth() {if (this.month === 1) {this.year -= 1;this.month = 12;} else {this.month -= 1;}},nextMonth() {if (this.month === 12) {this.year += 1;this.month = 1;} else {this.month += 1;}},isToday(date) {const today = new Date();return (this.year === today.getFullYear() &&this.month === today.getMonth() + 1 &&date === today.getDate());},isSelected(date) {return this.selectedDate && this.selectedDate === date;},selectDate(date) {this.selectedDate = date;this.$emit("select", new Date(this.year, this.month - 1, date));},},};
</script><style scoped>.calendar {width: 280px;border: 1px solid #ccc;border-radius: 4px;font-size: 14px;background: #fff;}.calendar-header {display: flex;justify-content: space-between;padding: 8px;font-weight: 600;}.calendar-title {text-align: center;flex: 1;}.calendar-prev,.calendar-next {cursor: pointer;}.calendar-body {display: flex;flex-direction: column;}.calendar-weekdays {display: flex;}.calendar-weekday {flex: 1;padding: 8px;text-align: center;border-bottom: 1px solid #ccc;font-weight: 600;}.calendar-dates {display: flex;flex-wrap: wrap;}.calendar-date {width: calc(100% / 7);padding: 8px;text-align: center;border-bottom: 1px solid #ccc;cursor: pointer;}.calendar-date-today {color: blue;font-weight: 600;}.calendar-date-selected {background-color: rgb(60, 156, 255);color: rgb(255, 255, 255);}
</style>
使用方法:
<template><view><calendar @select="handleDateSelect" /></view>
</template><script>// 在根目录下的 components 文件夹添加的组件无需导入export default {methods: {handleDateSelect(date) {console.log(date);},},};
</script>
该组件实现了基本的日历布局和日期选择功能,并用 props 和 emit 方法实现了父子组件之间的数据传递。根据需要,可以对该组件进行进一步的定制和扩展。
uni-app 日历组件的实现相关推荐
- 切换 uniapp_万能前端框架uni app初探03:底部导航开发
前言 本节我们使用uni app的底部导航功能,点击不同tab会显示不同页面,这个功能在实际项目开发中几乎是必备的. 一.基础知识 1.tabBar 如果应用是一个多 tab 应用,可以通过 tabB ...
- uni app map 地图 漂浮问题及方案
uni app map 地图 漂浮问题及方案 文章页有图片导致的问题,图片没加载出来,导致文章内容高度不固定,如果图片没加载出来,高度就是0,如果此时开始加载map,那么map就在那里加载,map原生 ...
- android 携程日历控件,仿携程酒店日历组件for小程序
仿携程酒店日历 接受日历组件开发之前,本来是拒绝的,日历组件,表单组件绝逼是前端开发的一个噩梦,尤其要做好一个旅游项目的日历,产品的收货标准只有一条,你看携程都实现了哦, MMP的.要在小程序中实现携 ...
- 可能是东半球最好看的vue3日历组件vue-baidu-calendar
前几天面试,被问到如何实现一个日历组件,然后发现百度的日历组件比较看,决定自己用vue3实现一下,并且还能巩固一下vue3的知识. 代码全部采用compositionAPI script-setup风 ...
- uni app中使用图表
关于在uni app中运用图表 今天写项目,需要在uni app中使用图表,我使用的是ucharts. 具体操作如下 1.下载 ucharts可以直接在uni app的插件市场下载安装. 先在插件市场 ...
- uni app 自动化索引列表
uni app 自动化索引列表,官方推荐的第三方插件 https://ext.dcloud.net.cn/plugin?id=375 [{"letter": "A&quo ...
- 开发amis工作日历组件
环境准备 amis-editor-demo 本次组件开发是基于amis-editor-demo这个项目来开展的,首先需要搭建该工程,项目地址:amis-editor-demo toast-ui-cal ...
- uni app 视频播放功能
视频播放功能,需求:编译成纯h5网页,运行在任意容器里,如浏览器.安卓原生和iOS等 媒体组件video 首先使用video组件,然后高度需要自己设置 uni.createVideoContext 官 ...
- uni app 开发微信小程序及上线体验
uni app 开发微信小程序及上线体验 项目创建及微信小程序AppId的申请 本次开发的是电商类的微信小程序,这里用到的是HBuilderX这个编辑器.之前用的Visual Studio Code ...
- 教你如何写一个符合自己需求的小程序日历组件
1|0 前言 很多时候,我们生活中会有各种打卡的情况,比如 keep 的运动打卡.单词的学习打卡和各种签到打卡或者酒店的入住时间选择,这时候就需要我们书写一个日历组件来处理我们这种需求. 但是更多时候 ...
最新文章
- 有关STM32外设配置的几个常见问题
- 协作通信-af df的matlab仿真,协作通信-AF、DF的MATLAB仿真(解压密码yuema1086)
- 对discuz的代码分析学习(三)mysql驱动
- awk输出指定行,awk如何取反
- python机器学习常用包下载安装以及使用案例汇总
- 从物理空间到数字世界,数字孪生打造智能化基础设施
- Win10系统如何设置开机启动密码
- 结巴分词python教程_Python笔记:用结巴分词制作词云图
- 如何获得静态IP资源?
- 软件测试周刊(第26期):从喜欢里得到力量和快乐
- 双十二|Solidigm官方店铺 惊喜大放送 福利享不停
- Java面试宝典(2019版)
- Python:将list写入Excel
- python画八角星_Goc-N角星的绘制
- 如何让微信丢骰子永远只出“666”
- 404页面该怎么做?
- NTP网络时间服务器应用“智能交通‘边缘云脑’系统
- java 斑马线检测,行人过斑马线监测预警系统解决方案
- python划分有限元网格_关于有限元网格划分
- SDN平台搭建,ovs+floodlight 亲测有效