element-plus时间选择器快速筛选(本周,上周,本月,上月,过去x天),选中后并返回text

在这里插入图片描述

<el-date-picker
          v-model="rangeValue"
          popper-class="picker-popovers"
          :shortcuts="rangeShortcuts"
          class="timefilter"
          @visible-change="handleVisibleChange"
          type="daterange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :prefix-icon="customPrefix"
          value-format="YYYY-MM-DD"
        >
        </el-date-picker>
 import dayjs from 'dayjs'
 const timeText = ref('')

 const customPrefix = shallowRef({
    render() {
      return h('div', timeText.value)
    },
  })
  

const handleVisibleChange = (type) => {
    const btns = document.getElementsByClassName('el-picker-panel__shortcut')
    Array.from(btns).forEach(el => {
      el.addEventListener('click', function(e){
        timeText.value = e.target?.innerText
      })
    })
  }
  

 const rangeShortcuts = ref([
    {
      text: '今天',
      value: () => [dayjs()],
    },
    {
      text: '昨天',
      value: () => [dayjs().subtract(1, 'day')],
    },
    {
      text: '本周',
      value: () => [
        dayjs().startOf('week').add(1, 'days'),
        dayjs().endOf('week').add(1, 'days'),
      ],
    },
    {
      text: '上周',
      value: () => [
        dayjs().subtract(1, 'weeks').startOf('week').add(1, 'days'),
        dayjs().subtract(1, 'weeks').endOf('week').add(1, 'days'),
      ],
    },
    {
      text: '本月',
      value: () => [dayjs().startOf('month'), dayjs().endOf('month')],
    },
    {
      text: '上月',
      value: () => [
        dayjs().subtract(1, 'months').startOf('month'),
        dayjs().subtract(1, 'months').endOf('month'),
      ],
    },
    {
      text: '过去5天',
      value: () => [dayjs().subtract(5, 'day'), dayjs()],
    },
    {
      text: '过去7天',
      value: () => [dayjs().subtract(7, 'day'), dayjs()],
    },
    {
      text: '过去14天',
      value: () => [dayjs().subtract(14, 'day'), dayjs()],
    },
    {
      text: '过去30天',
      value: () => [dayjs().subtract(30, 'day'), dayjs()],
    },
    {
      text: '过去60天',
      value: () => [dayjs().subtract(60, 'day'), dayjs()],
    },
    {
      text: '过去180天',
      value: () => [dayjs().subtract(180, 'day'), dayjs()],
    },
  ])

样式

.timefilter .el-icon {
    @apply ml-6 mr-4;
    white-space: nowrap;
  }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值