elementUI 时间段快捷选择及禁用(包含d2-crud-plus中使用)

需求

vue项目elementUI,需求为时间范围搜索,带快捷键并且只能选择今天之前,90天内的时间。搜索今天为实时数据,不能使用时间段,只能单独搜索。

※注

需求是今天不可选,只有时间为空时才查询今天,所有下面代码中时间段快捷选择时,结束时间都是昨天。具体可根据自己需求修改。

效果


昨天

近一周

近一月

代码

html 复制代码
<el-date-picker v-model="time" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
        size="small" value-format="yyyyMMdd" :picker-options="disableTime" />
       data() {
    return {
      disableTime: {
        disabledDate: (time) => {
          return time.getTime() > Date.now() - 1000 * 60 * 60 * 24 || time.getTime() < Date.now() - 1000 * 60 * 60 * 24 * 90
        }
      },
    }
  }, 
js 复制代码
<script>
export default {
	data() {
		return {
			// 设置禁用范围
			disableTime: {
        		disabledDate: (time) => {
          			return time.getTime() > Date.now() - 1000 * 60 * 60 * 24 || time.getTime() < Date.now() - 1000 * 60 * 60 * 24 * 90
        		},
		        shortcuts: [
		          {
		            text: '今天',
		            onClick(picker) {
		              picker.$emit('pick', null)
		            }
		          },
		          {
		            text: '昨天',
		            onClick(picker) {
		              const start = new Date()
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
		              picker.$emit('pick', [start, start])
		            }
		          },
		          {
		            text: '最近一周',
		            onClick(picker) {
		              const end = new Date()
		              const start = new Date()
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
		              end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
		              picker.$emit('pick', [start, end])
		            }
		          },
		          {
		            text: '最近一个月',
		            onClick(picker) {
		              const end = new Date()
		              const start = new Date()
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
		              end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
		              picker.$emit('pick', [start, end])
		            }
		          }
		        ]
      		},
		}
	}
}
</script>

d2-crud-plus中代码

js 复制代码
export const crudOptions = (vm) => {
	return {
		columns: [
			{
	        	title: '时间',
	        	key: 'time',
	        	type: 'datetime',
	        	search: { // 搜索
	          		disabled: false, // 是否禁用该字段的查询
	          		width: '250px',
	          		component: { // 查询框组件配置,默认根据form配置生成
	            		name: 'el-date-picker',
	            		props: {
	              			'value-format': 'yyyy-MM-dd',
	              			type: 'daterange',
	              			'range-separator': '-',
	              			'start-placeholder': '开始时间',
	              			'end-placeholder': '结束时间',
	              			'picker-options': {
	                			disabledDate(time) {
	                  				return time.getTime() > Date.now() - 1000 * 60 * 60 * 24 || time.getTime() < Date.now() - 1000 * 60 * 60 * 24 * 90
	                			},
	                			shortcuts: shortcuts
	              			}
	            		}
	          		},
	          		order: 2 // 查询字段排序,数字越小越靠前
	        	}
	     	},
		]
	}
}

const shortcuts = [
  {
    text: '今天',
    onClick(picker) {
      picker.$emit('pick', null)
    }
  }, {
    text: '昨天',
    onClick(picker) {
      const start = toNooning()
      const end = toNooning()
      addDays(start, -1)
      addDays(end, -1)
      picker.$emit('pick', [start, end])
    }
  }, {
    text: '近一周',
    onClick(picker) {
      const start = toNooning()
      const end = toNooning()
      addDays(start, -7)
      addDays(end, -1)
      picker.$emit('pick', [start, end])
    }
  }, {
    text: '近一月',
    onClick(picker) {
      const start = toNooning()
      const end = toNooning()
      addDays(start, -30)
      addDays(end, -1)
      picker.$emit('pick', [start, end])
    }
  }
]
function toNooning(date) {
  if (date == null) {
    date = new Date()
  }
  date.setHours(12)
  date.setMinutes(0)
  date.setSeconds(0)
  return date
}
function addDays(date, days) {
  date.setTime(date.getTime() + 3600 * 1000 * 24 * days)
}
相关推荐
陈随易7 分钟前
编程语言级别的Skill市场,AI Agent 的未来形态
前端·后端·程序员
SoaringHeart1 小时前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
IT_陈寒3 小时前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰3 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
竹林8184 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花4 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu12275 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪5 小时前
Vue3-生命周期
前端
莪_幻尘6 小时前
你的 AI Skill 越多越蠢?Token 上下文爆炸的求生指南
前端·ai编程
lichenyang4536 小时前
从 has.echo 到异步 API 注册表:一次 ASCF API 回调不触发的排查复盘
前端