vue 基于element的日历面板 比较实用简单的希望可以帮的大家

本文介绍如何在Vue项目中使用ElementUI库创建一个实用的日历组件。通过CSS进行样式调整,使得日历面板更加符合需求。提供了一张展示效果的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<div>
            <i class="el-icon-arrow-left weekStr-icon" @click="yearClick(0)"></i>
            <samp>{{dateYear}}</samp>
            <i class="el-icon-arrow-right weekStr-icon" @click="yearClick(1)"></i>
          </div>
          <div>
            <i
              class="el-icon-arrow-left weekStr-icon"
              :style="dateMonth == 1 && 'color:#ddd' "
              @click="monthClick(0)"
            ></i>
            <samp>{{dateMonth}}月</samp>
            <i
              class="el-icon-arrow-right weekStr-icon"
              :style="dateMonth == 12 && 'color:#ddd' "
              @click="monthClick(1)"
            ></i>
          </div>
        </div>
      </div>
      <el-calendar
        :scoped-slot="calendarDate"
        class="calendar-sty"
        style="height:320px"
        v-model="calendarDate"
      ></el-calendar>

在这里插入图片描述

.calendar-sty{
  .el-calendar__header{
    display: none;
  }
  .el-calendar-table .el-calendar-day{
    height: 40px;
  }
}

先用css把这块隐藏
在这里插入图片描述

date: new Date(),
calendarDate: '',
dateYear: '',
dateMonth: 0,
dateday: 0,
whetherClick: false,

  watch: {
    'dateYear' () {
      this.calendarDate = this.dateYear + '/' + this.dateMonth + '/' + this.dateday
    },
    'dateMonth' () {
      this.calendarDate = this.dateYear + '/' + this.dateMonth + '/' + this.dateday
    },
    'calendarDate' () {
      // console.log('单元格点击')
      // console.log(typeof (this.calendarDate))
      if (typeof (this.calendarDate) !== 'string') {
        this.whetherClick = true
        this.dateYear = this.calendarDate.getFullYear()
        this.dateMonth = (this.calendarDate.getMonth() + 1)
        this.dateday = this.calendarDate.getDate()
        this.whetherClick = false
      }
    }
  },
    /** 年份选择 */
    yearClick (res) {
      if (res === 1) {
        this.dateYear = this.dateYear + 1
        this.dateday = 1
      } else {
        this.dateYear = this.dateYear - 1
        this.dateday = 1
      }
    },
    /** 月份选择 */
    monthClick (res) {
      if (res === 1 && this.dateMonth !== 12) {
        this.dateMonth = this.dateMonth + 1
        this.dateday = 1
      } else if (this.dateMonth !== 1) {
        this.dateMonth = this.dateMonth - 1
        this.dateday = 1
      }
    },



效果图
在这里插入图片描述

### Vue 实现日历控件选择起止时间 在 Vue 中,`element-ui` 提供了 `el-date-picker` 组件来实现日期的选择功能。对于需要设置起始时间和结束时间的情况,可以利用该组件的 `type="daterange"` 属性[^1]。 下面是一个具体的例子展示如何配置此组件以及处理其变化事件: ```html <template> <div class="date-range-selector"> <!-- 表单项 --> <el-form-item label="选择日期"> <!-- el-date-picker 组件用于选择日期范围 --> <el-date-picker v-model="selectedDates" type="daterange" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd" @change="handleChange" /> </el-form-item> </div> </template> <script> export default { data() { return { selectedDates: [], // 存储所选中的日期区间 pickerOptions: { // 配置选项以限制可选的时间范围 disabledDate(time) { const now = new Date().getTime(); return time.getTime() > now; } }, }; }, methods: { handleChange(value) { console.log('Selected dates:', value); // 可在此处添加更多逻辑,比如验证输入的有效性等操作 } } }; </script> ``` 上述代码片段展示了怎样通过绑定 `v-model` 到一个数组变量上来获取用户选定的具体时间段,并且定义了一个方法 `handleChange()` 来响应当用户改变选择时触发的变化事件。此外还设置了 `disabledDate` 函数作为 `pickerOptions` 的一部分,用来禁用未来日期的选择[^2]。 #### 关键属性解释 - **unlink-panels**: 解除两个面板之间的关联关系,默认情况下它们是同步滚动的。 - **range-separator**: 自定义分隔符,在这里设为了中文字符“至”,可以根据需求调整为其他符号或文字。 - **start-placeholder 和 end-placeholder**: 分别指定了开始和结束位置占位提示信息。 - **value-format**: 定义返回值的数据格式化方式;此处指定的是年月日形式 (即 `"yyyy-MM-dd"`)[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值