Layui时间范围选择器,添加【本周、本月、本季度、本年等常用时间快捷键】

文章介绍了如何使用layui.laydate插件在HTML中创建时间选择器,并提供了两种JS实现方式,包括设置快捷选项如本周、本月、本年等,以及处理选择后的事件,将选择的时间范围赋值给隐藏的输入字段。

1. 界面实现

 <input id="Date_select" type="text" class="form-control" placeholder="请选择时间范围" style="border-radius: 4px;" />
 <input id="StartDate" type="hidden" />
 <input id="EndDate" type="hidden" />

2. JS具体实现

2.1 第一种实现

//时间选择
layui.laydate.render({
   
   
	elem: '#Date_select',
    type: 'date',
	range: true,
	shortcuts: [
		{
   
   
			text: '本周',
			value: function () {
   
   
				let now = new Date(); //当前日期 
				let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天,周日为0,但实际上平时都是以周一为一周的开始,所以周3因该为2
				let nowDay = now.getDate(); //当前日 
				let nowMonth = now.getMonth(); //当前月 
				let nowYear = now.getFullYear(); //当前年 
				let startTime = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
				let endTime = new Date(nowYear, nowMonth, nowDay + 6 - nowDayOfWeek);
				var value = [];
				value.push(startTime);
				value.push(endTime);
				return value;
			}()
		},
		{
   
   
			text: "本月",
			value: function () {
   
   
				let now = new Date(); //当前日期 
				let nowMonth = now.getMonth(); //当前月 
				let nowYear = now.getFullYear(); //当前年 
				let startTime = new Date(nowYear, nowMonth, 1);
				let endTime = new Date(nowYear, nowMonth + 1, 0);
				var value = [];
				value.push(startTime);
				value.push(endTime);
				return value;
			}()
		}
明白了!你希望: - ✅ **保留场站选择框**(带“场站”标签) - ✅ **保留“本周本月本年”快捷按钮** - ❌ 只修改时间选择部分:从原来的单个月选择器 → 改为 **带图标的开始月和结束月范围选择器** - ✅ 时间选择前面要有“时间区间”文字 + 每个选择框前有图标(如 📅 和 🕒) --- ### ✅ 最终目标效果 ``` 场站 [▼ 风场A] 时间区间 [📅 2024-01 至 🕒 2025-01] [本周] [本月] [本年] ``` > ⚠️ 注意:虽然按钮叫“本周/本月/本年”,但我们的时间选择是 **按月的范围选择器**,所以点击时会自动计算对应的月份区间。 --- ### ✅ 修改后的代码(仅替换时间选择部分) ```vue <template> <div class="form-button-date-picker-container"> <!-- 场站选择 --> <BasicForm @register="registerStation" /> <!-- 时间区间 + 月范围选择器 --> <div class="time-range-group"> <span class="label">时间区间</span> <a-range-picker v-model:value="monthRange" mode="month" :placeholder="['开始月份', '结束月份']" format="YYYY-MM" value-format="YYYY-MM" separator="至" class="month-range-picker" :suffix-icon="renderSuffixIcon()" /> </div> <!-- 快捷按钮:本周本月本年 --> <div class="button-group"> <div v-for="(item, index) in buttons2" :key="index" :class="['btn', { active: type === index }]" @click="handleTimeTypeClick(index)" > {{ item }} </div> </div> </div> </template> <script lang="ts" setup> import { ref } from 'vue'; import { BasicForm, useForm } from '/@/components/Form'; import { createVNode } from 'vue'; import { CalendarOutlined, ClockCircleOutlined } from '@ant-design/icons-vue'; import dayjs from 'dayjs'; // ======= 场站选择(保持不变)======= const [registerStation] = useForm({ schemas: [ { label: '场站', field: 'fengchang', component: 'Select', componentProps: { options: [ { label: '风场A', value: 'A' }, { label: '风场B', value: 'B' }, { label: '风场C', value: 'C' }, ], placeholder: '请选择场站', }, labelWidth: 50, }, ], compact: true, size: 'small', baseColProps: { style: { width: '240px', flexShrink: 0 } }, showLabel: true, showResetButton: false, showSubmitButton: false, }); // ======= 月范围选择器 ======= const monthRange = ref<string[]>([]); const buttons2 = ['本周', '本月', '本年']; const type = ref<number>(1); // 自定义后缀图标(Ant Design 图标) const renderSuffixIcon = () => { // 这里可以返回一个图标,但 a-range-picker 不支持左右不同图标 // 所以我们统一用一个图标表示“月份选择” return createVNode(CalendarOutlined, { style: { color: '#1e7bda' } }); }; // 处理快捷按钮点击 const handleTimeTypeClick = (index: number) => { type.value = index; const today = dayjs(); let start: dayjs.Dayjs | null = null; let end: dayjs.Dayjs = today; if (index === 0) { // 本周 → 转为周所在月份?还是按逻辑处理成“最近7天”所在的起止月? // 更合理:取本周第一天和最后一天,再提取月份 start = today.startOf('week'); end = today.endOf('week'); } else if (index === 1) { // 本月 start = today.startOf('month'); end = today.endOf('month'); } else if (index === 2) { // 本年 start = today.startOf('year'); end = today.endOf('year'); } // 提取开始月和结束月 monthRange.value = [ start.format('YYYY-MM'), end.format('YYYY-MM') ]; }; </script> <style lang="less" scoped> .form-button-date-picker-container { display: flex; align-items: center; gap: 16px; height: 40px; min-width: 900px; } .time-range-group { display: flex; align-items: center; gap: 8px; font-size: 14px; color: white; white-space: nowrap; .label { min-width: 60px; } } :deep(.ant-picker.month-range-picker) { width: 260px !important; height: 32px; background: transparent; border: 1px solid rgba(255, 255, 255, 0.3); border-radius: 4px; color: white; } :deep(.ant-picker-input > input) { color: white !important; } :deep(.ant-picker-suffix svg) { color: #1e7bda !important; width: 1em; height: 1em; } :deep(.ant-picker-input > input::placeholder) { color: rgba(255, 255, 255, 0.5); } </style> ``` --- ### ✅ 解释说明 | 功能 | 实现方式 | |------|----------| | 带“时间区间”标签 | 手动添加 `<span class="label">时间区间</span>` | | 月范围选择 | 使用 `<a-range-picker mode="month" />` | | 显示图标 | 使用 `suffixIcon` 插入 `CalendarOutlined`(Ant Design 图标) | | 格式化输出 | `format="YYYY-MM"` 和 `value-format="YYYY-MM"` | | 快捷按钮行为 | 点击后根据当前周/月/年设置 `monthRange` 的起止月份 | > 💡 虽然无法给两个输入框分别加不同图标(Ant Design 不支持),但你可以通过 CSS 或自定义组件进一步增强,目前这个方案简洁且稳定。 --- ### ✅ 效果总结 - ✔️ 场站选择不变 - ✔️ 快捷按钮不变 - ✔️ 时间选择升级为带图标的 **月范围选择器** - ✔️ 样式统一、美观、可交互 --- ###
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值