基于element-ui实现日期组件的多选、全选、清空

一、效果图(全选效果)

二、代码


            <el-date-picker
              style="width: 80%"
              size="small"
              :picker-options="pickerOptions"
              v-model="formInline.statDate"
              type="dates"
              value-format="yyyy-MM-dd"
              placeholder="请选择日期"
              :clearable="false"
            >
            </el-date-picker>

  data() {
    let _this = this
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now() - 24*60*60*1000;
        },
        shortcuts: [{
            text: '清空',
            onClick() {
              _this.formInline.statDate = null
            }
          }, {
            text: '多选',
            onClick() {
              _this.formInline.statDate = null
            }
          }, {
            text: '全选',
            onClick(picker) {
              _this.formInline.statDate = null
              let year = new Date(picker.date).getFullYear()
              let month =(new Date(picker.date).getMonth() + 1) < 10 ? String('0' + (new Date(picker.date).getMonth() + 1)) : new Date(picker.date).getMonth() + 1
              let nowDate = getYearMonthDay(new Date(new Date().getTime() - 24*60*60*1000))
              let dateList = []
              if (getMonthList(year, month).indexOf(nowDate) !== -1) {
                dateList = getMonthList(year, month).splice(0, getMonthList(year, month).indexOf(nowDate) + 1)
              } else {
                dateList = getMonthList(year, month)
              }
              _this.formInline.statDate = dateList
            }
          }]
      },
      formInline: {
        statDate: [],
      },
    };
  },

三、用到的日期封装方法

/**
 * 输入年月返回一个月所有天数的数组
 */
export const getMonthList = (year, month) => {
  let d = new Date(year, month, 0).getDate(); // 获取某一个月的天数
  let list = [];
  for (let  i= 0; i < d; i++) {
    if( i + 1 < 10) {
      list.push(year + "-" + month + "-" + `0${(i + 1)}`);
    } else {
      list.push(year + "-" + month + "-" + (i + 1));
    }
  }
  return list;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值