uniapp 微信小程序 自界说日历组件

打印 上一主题 下一主题

主题 1022|帖子 1022|积分 3066

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
结果图

功能:可以记载当天是否有某些任务大概某些记载
具体利用:
子组件代码
  1. <template>
  2.         <view class="Accumulate">
  3.                 <view class="bx">
  4.                         <view class="bxx">
  5.                                 <view class="plank">
  6.                                 </view>
  7.                                 <view class="calendar">
  8.                                         <view class="calendarbx">
  9.                                                 <view class="calendarview flex jc ac">
  10.                                                         <u-icon name="arrow-left" color="#232323" size="18" @click="addmonth(1)"></u-icon>
  11.                                                         <view class="title">
  12.                                                                 {{viewday}}
  13.                                                         </view>
  14.                                                         <u-icon name="arrow-right" color="#232323" size="18" @click="addmonth(2)"></u-icon>
  15.                                                 </view>
  16.                                                 <view class="week flex  ac">
  17.                                                         <view class="weekli" v-for="(item,index) in week">
  18.                                                                 {{item.title}}
  19.                                                         </view>
  20.                                                 </view>
  21.                                                 <view class="data">
  22.                                                         <view class=" flex  ac flexwrap" v-if="show">
  23.                                                                 <view class="datali flex jc ac" v-for="(item,index) in days" :key="item">
  24.                                                                         <!-- 三层判断条件 -->
  25.                                                                         <view class="dataliradius flex jc ac flexwrap"
  26.                                                                                 :class="newday==item&&isnewmonth&&chooseday==item?'dataliradiusc':newday==item&&isnewmonth&&chooseday!=item?'dataliradiuscc':newday!=item&&chooseday==item?'dataliradiusc':''"
  27.                                                                                 @click.stop="funchoosedayy(item)">
  28.                                                                                 {{item==newday&&isnewmonth?'今':item}}
  29.                                                                                 <view class="rounds">
  30.                                                                                         <view class="round" v-if="recordlist.includes(item)&&item!=chooseday">
  31.                                                                                         </view>
  32.                                                                                 </view>
  33.                                                                         </view>
  34.                                                                 </view>
  35.                                                         </view>
  36.                                                         <!-- 以下是为了防止切换闪烁而复制了一份用于展示 -->
  37.                                                         <view class=" flex  ac flexwrap" v-else>
  38.                                                                 <view class="datali flex jc ac" v-for="(item,index) in days" :key="item">
  39.                                                                         <!-- 三层判断条件 -->
  40.                                                                         <view class="dataliradius flex jc ac flexwrap"
  41.                                                                                 :class="newday==item&&isnewmonth&&chooseday==item?'dataliradiusc':newday==item&&isnewmonth&&chooseday!=item?'dataliradiuscc':newday!=item&&chooseday==item?'dataliradiusc':''">
  42.                                                                                 {{item==newday&&isnewmonth?'今':item}}
  43.                                                                                 <view class="rounds" v-if="recordlist.includes(item)&&item!=chooseday">
  44.                                                                                         <view class="round">
  45.                                                                                         </view>
  46.                                                                                 </view>
  47.                                                                         </view>
  48.                                                                 </view>
  49.                                                         </view>
  50.                                                 </view>
  51.                                         </view>
  52.                                 </view>
  53.                         </view>
  54.                 </view>
  55.         </view>
  56. </template>
  57. <script>
  58.         export default {
  59.                 data() {
  60.                         return {
  61.                                 day: Number(new Date()), //用于记录今天
  62.                                 showday: null, //用来存储要加减的日期
  63.                                 viewday: null, //页面上展示的日期
  64.                                 newday: null, //当天几号
  65.                                 isnewmonth: true, //是否是当月
  66.                                 chooseday: null, //选中的那一天
  67.                                 show: true,
  68.                                 week: [{
  69.                                                 title: '一'
  70.                                         },
  71.                                         {
  72.                                                 title: '二'
  73.                                         },
  74.                                         {
  75.                                                 title: '三'
  76.                                         },
  77.                                         {
  78.                                                 title: '四'
  79.                                         },
  80.                                         {
  81.                                                 title: '五'
  82.                                         },
  83.                                         {
  84.                                                 title: '六'
  85.                                         },
  86.                                         {
  87.                                                 title: '日'
  88.                                         }
  89.                                 ],
  90.                                 days: [], //天数数组
  91.                                 height: 0
  92.                         };
  93.                 },
  94. props:{
  95.                         recordlist:{
  96.                                 default: [], //是否有练习记录,需要每次切换月份的时候查询,那几天有练习记录,下标会记录
  97.                                 stype:Array
  98.                         }
  99.                 },
  100.                 onLoad() {
  101.                 },
  102.                 onReady() {
  103.                         let time = this.yearFormat(this.day, 'obj')
  104.                         this.newday = time.day; //当天日期只用初始化一次就不用再初始化了
  105.                         this.showday = time.year + '-' + time.month + '-' + time.day; //用来存储要加减的日期
  106.                         this.viewday = this.yearFormat(this.showday) //页面上展示的日期
  107.                         this.chooseday = time.day;
  108.                         this.addmonth(0) //初始化时间
  109.                 },
  110.                 methods: {
  111.                         //选中日期
  112.                         funchoosedayy(item) {
  113.                                 // console.log(item, '选中几号');
  114.                                 if (item == '') {
  115.                                         return
  116.                                 }
  117.                                 this.chooseday = item;
  118.                                 let dataobj = this.showday.split('-');
  119.                                 let month;
  120.                                 let year;
  121.                                 let day;
  122.                                 let sendtime = dataobj[0] + '-' + dataobj[1] + '-' + this.chooseday;
  123.                                 this.$emit('sendtime', '选中的时间:' + sendtime);
  124.                         },
  125.                         //加减月份,初始化月份
  126.                         addmonth(type) {
  127.                                 this.show = false;
  128.                                 setTimeout(() => {
  129.                                         this.show = true;
  130.                                 }, 100)
  131.                                 let newmonth = this.yearFormat(this.day, 'obj')
  132.                                 let dataobj = this.showday.split('-');
  133.                                 let month;
  134.                                 let year;
  135.                                 let day;
  136.                                 //加
  137.                                 if (type == 2) {
  138.                                         if (dataobj[1] == 12) {
  139.                                                 month = 1;
  140.                                                 year = (dataobj[0] * 1) + 1
  141.                                         } else {
  142.                                                 year = (dataobj[0] * 1);
  143.                                                 month = (dataobj[1] * 1) + 1
  144.                                         }
  145.                                 }
  146.                                 //减
  147.                                 if (type == 1) {
  148.                                         if (dataobj[1] == 1) {
  149.                                                 month = 12;
  150.                                                 year = (dataobj[0] * 1) - 1
  151.                                         } else {
  152.                                                 year = (dataobj[0] * 1);
  153.                                                 month = (dataobj[1] * 1) - 1
  154.                                         }
  155.                                 }
  156.                                 //初始化使用
  157.                                 if (type == 0) {
  158.                                         month = (dataobj[1] * 1);
  159.                                         year = (dataobj[0] * 1);
  160.                                 }
  161.                                 let daynum = this.getDaysInYearMonth(year, month);
  162.                                 this.days = daynum; //获取天数
  163.                                 let week = this.getDayOfWeek(year, month, 1) //获取每个月1号星期几
  164.                                 //0 相当于星期日
  165.                                 let addday;
  166.                                 if (week == 0) {
  167.                                         addday = 6;
  168.                                 } else {
  169.                                         addday = (week - 1)
  170.                                 }
  171.                                 for (let i = 0; i < addday; i++) {
  172.                                         this.days.unshift('')
  173.                                         this.$forceUpdate()
  174.                                 }
  175.                                 //判断是否是当月,是当月的话,选中当天日期,不是则是当月1号
  176.                                 if (newmonth.month == month) {
  177.                                         day = dataobj[2];
  178.                                         this.chooseday = newmonth.day;
  179.                                         this.isnewmonth = true;
  180.                                         this.showday = year + '-' + month + '-' + day;
  181.                                         this.viewday = year + '年' + month + '月';
  182.                                         // console.log(this.showday, '当月');
  183.                                         this.$forceUpdate()
  184.                                 } else {
  185.                                         day = 1;
  186.                                         this.chooseday = day;
  187.                                         this.isnewmonth = false;
  188.                                         this.showday = year + '-' + month + '-' + day;
  189.                                         this.viewday = year + '年' + month + '月';
  190.                                         this.$forceUpdate()
  191.                                 }
  192.                                 // console.log(this.days);
  193.                                 this.$emit('state_sendtime', '初始化时间:' + this.showday);
  194.                                 this.$forceUpdate()
  195.                         },
  196.                         //获取年月
  197.                         yearFormat(num, obj) {
  198.                                 //过滤器 用于格式化时间
  199.                                 let date = new Date(num); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
  200.                                 let year = date.getFullYear();
  201.                                 let month = ("0" + (date.getMonth() + 1)).slice(-2);
  202.                                 let sdate = ("0" + date.getDate()).slice(-2);
  203.                                 let hour = ("0" + date.getHours()).slice(-2);
  204.                                 let minute = ("0" + date.getMinutes()).slice(-2);
  205.                                 let second = ("0" + date.getSeconds()).slice(-2);
  206.                                 let result;
  207.                                 if (obj) {
  208.                                         // 拼接
  209.                                         result = {
  210.                                                 year: year,
  211.                                                 month: month,
  212.                                                 day: sdate
  213.                                         }
  214.                                 } else {
  215.                                         result = year + '年' + month + '月'
  216.                                 }
  217.                                 // 返回
  218.                                 return result;
  219.                         },
  220.                         // 获取该年该月的天数
  221.                         getDaysInYearMonth(year, month) {
  222.                                 // 每一次进行更新前,先清空日期数组
  223.                                 month = parseInt(month, 10);
  224.                                 // javascript中Date()对象的getDate()是获取时间对象对应于一个月中的某一天(1~31),当设置为0的时候,getDate()返回的就是最后一天,刚好对应我们需要的值。
  225.                                 var date = new Date(year, month, 0);
  226.                                 let arr = [];
  227.                                 for (var i = 1; i <= date.getDate(); i++) {
  228.                                         arr.push(i)
  229.                                 }
  230.                                 return arr;
  231.                         },
  232.                         //获取周几
  233.                         // console.log(getDayOfWeek(2023, 10, 5)); // 输出:星期四
  234.                         // 0就是 周日
  235.                         getDayOfWeek(year, month, day) {
  236.                                 console.log(year, (month * 1), day);
  237.                                 const date = new Date(year + '-' + (month * 1) + '-' + day).getDay(); // 注意月份是从0开始计数
  238.                                 const options = {
  239.                                         weekday: 'long'
  240.                                 };
  241.                                 //new Intl.DateTimeFormat('zh-CN', options).format(date)
  242.                                 return date;
  243.                         }
  244.                 }
  245.         }
  246. </script>
  247. <style lang="less" scoped>
  248.         .Accumulate {
  249.                 // position: relative;
  250.         }
  251.         .back {
  252.                 width: 100%;
  253.                 height: 360rpx;
  254.                 border-radius: 0 0 60rpx 60rpx;
  255.                 background: #3366ff;
  256.                 position: absolute;
  257.         }
  258.         .Evaluationlist {
  259.                 margin-top: 30rpx;
  260.                 padding: 0 30rpx;
  261.                 box-sizing: border-box;
  262.                 overflow: scroll;
  263.                 .Evaluationlistli {
  264.                         padding: 30rpx 20rpx;
  265.                         box-sizing: border-box;
  266.                         border-radius: 30rpx;
  267.                         background: #fff;
  268.                         margin-bottom: 30rpx;
  269.                         .left {
  270.                                 // display: block;
  271.                                 width: 70%;
  272.                                 max-height: 100rpx;
  273.                         }
  274.                         .right {
  275.                                 padding: 10rpx 30rpx;
  276.                                 height: 60rpx;
  277.                                 box-sizing: border-box;
  278.                                 border-radius: 20rpx;
  279.                                 background: #3366ff;
  280.                                 color: #fff;
  281.                         }
  282.                         .rightb {
  283.                                 padding: 10rpx 30rpx;
  284.                                 height: 60rpx;
  285.                                 box-sizing: border-box;
  286.                                 border-radius: 20rpx;
  287.                                 background: #fff;
  288.                                 color: #3366ff;
  289.                                 border: 1rpx solid #3366ff;
  290.                         }
  291.                 }
  292.         }
  293.         .bx {
  294.                 width: 100%;
  295.                 // position: absolute;
  296.                 z-index: 999;
  297.                 .plank {
  298.                         width: 100%;
  299.                         height: 30rpx;
  300.                 }
  301.         }
  302.         .viewdata {
  303.                 width: 100%;
  304.                 height: 130rpx;
  305.                 padding: 0 30rpx;
  306.                 box-sizing: border-box;
  307.                 .viewdatali {
  308.                         width: 46%;
  309.                         height: 100%;
  310.                         border-radius: 30rpx;
  311.                         .num {
  312.                                 width: 100%;
  313.                                 font-size: 38rpx;
  314.                                 font-weight: bold;
  315.                                 text-align: center;
  316.                         }
  317.                         .title {
  318.                                 width: 100%;
  319.                                 font-size: 24rpx;
  320.                                 margin-top: 5rpx;
  321.                                 text-align: center;
  322.                         }
  323.                 }
  324.                 .viewdataliA {
  325.                         background: #f5f5f5;
  326.                         color: skyblue;
  327.                 }
  328.                 .viewdataliB {
  329.                         background: #f5f5f5;
  330.                         color: pink;
  331.                 }
  332.         }
  333.         .calendar {
  334.                 width: 100%;
  335.                 // padding: 30rpx 30rpx 0rpx 30rpx;
  336.                 box-sizing: border-box;
  337.                 // border-radius: 30rpx;
  338.                 .calendarbx {
  339.                         width: 100%;
  340.                         padding: 30rpx 30rpx 0rpx 30rpx;
  341.                         box-sizing: border-box;
  342.                         border-radius: 30rpx;
  343.                         background: #fff;
  344.                         .calendarview {
  345.                                 background: lightgoldenrodyellow;
  346.                                 padding: 10rpx 20rpx;
  347.                                 box-sizing: border-box;
  348.                                 .title {
  349.                                         color: #232323;
  350.                                         font-size: 28rpx;
  351.                                         margin-left: 100rpx;
  352.                                         margin-right: 100rpx;
  353.                                 }
  354.                         }
  355.                         .week {
  356.                                 font-size: 26rpx;
  357.                                 color: #999;
  358.                                 margin-top: 30rpx;
  359.                                 .weekli {
  360.                                         width: 12.2%;
  361.                                         height: 30rpx;
  362.                                         text-align: center;
  363.                                         margin-right: 2.4%;
  364.                                         line-height: 30rpx;
  365.                                 }
  366.                                 .weekli:nth-child(7n+7) {
  367.                                         margin-right: 0% !important;
  368.                                 }
  369.                         }
  370.                         .data {
  371.                                 font-size: 28rpx;
  372.                                 color: #999;
  373.                                 margin-top: 40rpx;
  374.                                 font-weight: bold;
  375.                                 min-height: 410rpx;
  376.                                 .datali {
  377.                                         width: 12.2%;
  378.                                         height: 50rpx;
  379.                                         // text-align: center;
  380.                                         margin-right: 2.4%;
  381.                                         margin-bottom: 40rpx;
  382.                                         .dataliradius {
  383.                                                 width: 50rpx;
  384.                                                 height: 50rpx;
  385.                                                 border-radius: 999%;
  386.                                                 background: #fff;
  387.                                                 line-height: 50rpx;
  388.                                                 color: #232323 !important;
  389.                                                 .rounds {
  390.                                                         width: 100%;
  391.                                                         height: 10rpx;
  392.                                                         .round {
  393.                                                                 width: 10rpx;
  394.                                                                 height: 10rpx;
  395.                                                                 border-radius: 999%;
  396.                                                                 background: #3366ff;
  397.                                                                 margin: auto;
  398.                                                         }
  399.                                                 }
  400.                                         }
  401.                                         .dataliradiuscc {
  402.                                                 color: #3366ff !important;
  403.                                         }
  404.                                         .dataliradiusc {
  405.                                                 width: 50rpx;
  406.                                                 height: 50rpx;
  407.                                                 border-radius: 999%;
  408.                                                 background: #3366ff;
  409.                                                 color: #fff !important;
  410.                                                 line-height: 50rpx;
  411.                                         }
  412.                                 }
  413.                                 .datali:nth-child(7n+7) {
  414.                                         margin-right: 0% !important;
  415.                                 }
  416.                         }
  417.                 }
  418.         }
  419. </style>
复制代码
父组件
  1.                         <customCalendar @sendtime="sendtime" @state_sendtime="state_sendtime" :recordlist="[27, 25, 26, 29, 28]" ></customCalendar>
复制代码
有需求可以自行修改。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

科技颠覆者

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表