Element UI中el-calendar日历的年月快捷选择(可下拉选框选择)

51 篇文章 0 订阅
20 篇文章 0 订阅

需求:

以日历形式展现当前页面。其中,年月可进行下拉选择,默认选中当月,用户可以自由点选日期

实现思路:右上角年月元素隐藏掉,写一个下拉选框,通过css定位到正确位置,通过vue计算属性进行数据绑定

实现效果:
在这里插入图片描述
实现代码:

   <!-- 日历表 -->
            <div class="block Calendarselect">
              <el-date-picker
                v-model="monthValue"
                type="month"
                size="mini"
                style="width:150px"
                @change="changeMonth"
                :clearable="false"
              ></el-date-picker>
            </div>
            <el-calendar style="margin: 5px 10px" v-model="nowdate"
              ><template #dateCell="{ data }"
                ><div style="width: 100%; height: 100%">
                  <div class="spandate">{{ data.day.slice(-2) }}</div>
                  <div
                    v-for="item in arrTime"
                    :key="item.date"
                    class="calendar-count"
                  >
                    <div v-if="data.day == item.date">
                      <span> {{ item.count }} </span>
                    </div>
                  </div>
                </div></template
              ></el-calendar>
 computed: {
    monthValue: {
      get: function () {
        return this.nowdate;
      },
      set: function (newValue) {
        this.nowdate = newValue;
      },
    },
  },

methods方法

  //月下拉选框
    changeMonth() {
      this.nowdate = new Date(this.nowdate);
    },

选择框样式:

.Calendarselect {
  top: 22px;
  position: relative;
  text-align: right;
  left: 46%;
}
//去掉日历中自带的年月按钮
.el-button-group {
  display: none !important;
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 首先需要在 el-calendar 组件设置特定日期的样式,可以通过 slot-scope 获取到每个日期的具体信息,包括日期、农历、节日等。 ```html <el-calendar :value="date" :range="range" @change="change"> <template slot-scope="{ date, lunar, solarTerm, festival }"> <div class="date"> <div class="day">{{ date.getDate() }}</div> <div class="lunar">{{ lunar.day }}</div> <div class="festival">{{ festival }}</div> </div> </template> </el-calendar> ``` 2. 然后在后端返回的数据,添加一个标志字段,表示该日期是否需要特殊样式。例如,设置一个 holiday 字段,如果为 true,则该日期需要显示特定样式。 ```json { "date": "2022-01-01", "lunar": { "month": "十一", "day": "廿六" }, "solarTerm": null, "festival": "元旦", "holiday": true } ``` 3. 在 el-calendar 组件添加一个 class 字段,用来控制该日期的样式。在上述的模板,可以添加一个判断,如果该日期的 holiday 字段为 true,则添加一个 holiday 的 class。 ```html <el-calendar :value="date" :range="range" @change="change"> <template slot-scope="{ date, lunar, solarTerm, festival }"> <div :class="{ 'date': true, 'holiday': holiday }"> <div class="day">{{ date.getDate() }}</div> <div class="lunar">{{ lunar.day }}</div> <div class="festival">{{ festival }}</div> </div> </template> </el-calendar> ``` 4. 最后,在样式文件添加 holiday 类的样式,控制该日期的特定样式。例如,可以添加一个红色的标志,表示该日期是节假日。 ```css .holiday { position: relative; } .holiday::after { content: ''; position: absolute; top: 0; right: 0; width: 6px; height: 6px; border-radius: 50%; background-color: red; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值