首页 > 其他分享 >uni-app 日历组件的实现

uni-app 日历组件的实现

时间:2023-04-07 15:25:40浏览次数:36  
标签:flex app 日历 month date 组件 uni calendar

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">
        <view
          class="calendar-weekday"
          v-for="(weekday, index) in weekdays"
          :key="index"
        >
          {{ weekday }}
        </view>
      </view>
      <view class="calendar-dates">
        <view
          class="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 方法实现了父子组件之间的数据传递。根据需要,可以对该组件进行进一步的定制和扩展。

标签:flex,app,日历,month,date,组件,uni,calendar
From: https://www.cnblogs.com/yuzhihui/p/17295722.html

相关文章

  • Approval action add link to model driven record
    Whenyouaredecidingifyouwanttoapproveanaction,youprobablywantmoreinformationthenadescription.LuckilywecanaddalinktoourApprovalrequest!Thisisafollowuponmy previouspost whereIshowedhowtotriggeranApprovalinsideaBu......
  • 火山引擎数智平台拆解 APP改版场景中蕴含的数据思维
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群 你有没有遇到过,打开一款经常使用的APP,它却跳出一条需要你升级版本的提示? 大多数情况下,用户可能会直接点击「现在升级」或「稍后提醒我」,如果选择了后者,那接下来每一次使用APP,用户就会......
  • 火山引擎数智平台拆解 APP改版场景中蕴含的数据思维
     更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群你有没有遇到过,打开一款经常使用的APP,它却跳出一条需要你升级版本的提示?大多数情况下,用户可能会直接点击「现在升级」或「稍后提醒我」,如果选择了后者,那接下来每一次使用APP,用户就会再次收到......
  • 待办事项APP选择哪个
    对于很多网友来说,自己每天需要完成的事情不仅非常多,而且很杂乱,无论是生活中还是工作上的任务都需要按时完成。但是自己的记忆力是非常有限的,所以就会经常忘记待办的任务,对今天需要完成的事项不够清晰明确,无法很好的处理每项任务的优先级,这应该怎么办呢?  其实使用一款好用......
  • 直播app开发,使用koa和MongoDB实现分页和模糊查询
    直播app开发,使用koa和MongoDB实现分页和模糊查询1.分页per_page:一页多少条数据page:第几页 //index.jsconstKoa=require('koa')constapp=newKoa()constRouter=require('koa-router')constusersRouter=newRouter({prefix:'/users'})//MongoDB数据库Us......
  • AppleScript key code
    https://eastmanreference.com/complete-list-of-applescript-key-codestellapplication"SystemEvents"keycode49endtellPlay:tellapplication"iTunes"toplayPause:tellapplication"iTunes"topause......
  • SpringBoot @SpringBootApplication(exclude={DataSourceAutoConfiguration.calss})注
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.calss})该注解的作用是,排除自动注入数据源的配置,用exclude属性进行排除指定的类,在springBoot中使用多数据源时,加上@SpringBootApplication(exclude={DataSourceAutoConfiguration.calss})DataSourceAutoConfigur......
  • uniapp引入阿里巴巴矢量图标
    1、先去图标库下载图标 2.解压下载的文件进入  、 3、输入npminstall-giconfont-tools,再输入iconfont-tools最后无脑回车  4.文件夹会多一个iconfont-weapp的文件夹  5.iconfont-weapp-icon.css放入项目随机位子    6.在App.vue文件夹中,引......
  • UVA - 116 Unidirectional TSP 多段图的最短路
    题目大意:给出一个矩阵,要求每列都要路过,起点必须是第一列,求经过的最短路径的上面的数字和最小解题思路:公式为d[i][j]=min(d[i][j+1],d[i+1][j+1],d[i-1][j+1])+a[i][j],本题要注意,因为可以从最上面一行到最后一行,或者从最后一行到第一行,注意i的变化#include<cstdio>#include<alg......
  • uniapp修改数组对象属性,视图不刷新问题
    uniapp最大的坑遇到修改数组属性视图不更新的问题 有两种解决方法一:this.$forceUpdate();强制更新,这个确实在H5端更新了,但是微信小程序还是没有更新二:this.$set(),一样不行修改数组后视图依旧不刷新最后用$.elmit()方法在子组件修改父组件传给子组件的值达到父子组件双向......