当前位置: 首页 > news >正文

Vant 日期时间组件拓展

基于 

"vant": "^4.8.3",

效果图

<template><!-- 弹出层 --><van-popupv-model:show="isPicker"position="bottom"><van-pickerref="picker":title="title"v-model="selectedValues":columns="columns"@change="onChange"@cancel="cancelOn"@confirm="onConfirm"/></van-popup>
</template>

js

<script setup lang="ts">
import { ref ,watch } from 'vue';
import {formatTimeArray} from "@/utils/formate/date_formate";const props = defineProps({showPicker: {type: Boolean,default: false,},title: {type: String,default: "请选择时间",},timeFormat: {type: String,default: "yyyy-MM-dd HH:mm:ss",},
})const  isPicker = ref<any>(false) //是否显示弹出层
const  columns = ref<any>([])
const picker = ref()
const getCountDays = (year: any, month: any) => {//获取某年某月多少天const day = new Date(year, month, 0);return day.getDate();
}const selectedValues = ref<any>(formatTimeArray(new Date()));const getColumns = () => {const Y: any = selectedValues.value[0];const M: any = selectedValues.value[1];let year: any = []; // 默认范围 前后十年for (let i = Y - 10; i <= Y + 10; i++) {year.push({text: i,value: i});}let month: any ;//月month = getColumn(13,true)let days: any = getCountDays(Y, M); //天,和当年月有关let day: any;day = getColumn(days + 1,true)let hour: any ; //小时hour = getColumn(24)let minute: any ; //分钟minute = getColumn(60)let seconds: any ; //秒seconds = getColumn(60)if (props.timeFormat?.includes("yyyy")) {columns.value.push(year);}if (props.timeFormat?.includes("MM")) {columns.value.push(month); //获取当月的天数}if (props.timeFormat?.includes("dd")) {columns.value.push(day);}if (props.timeFormat?.includes("HH")) {columns.value.push(hour);}if (props.timeFormat?.includes("mm")) {columns.value.push(minute);}if (props.timeFormat?.includes("ss")) {columns.value.push(seconds);}
}const getColumn = (length: any,isSplice: Boolean = false) => {if ( isSplice ) {const data = Array.from({ length }, (_, index) => ({ text: index, value: index }));return data.splice(1)} else {return Array.from({ length }, (_, index) => ({ text: index, value: index }));}
}const onChange = (values: any) => {let days: any = getCountDays(values.selectedValues[0], values.selectedValues[1]);let newDayColumn: any ;newDayColumn = getColumn(days + 1,true);columns.value[2] = newDayColumn
}const emits = defineEmits(["changeValue",'confirm']);const cancelOn = () => {emits("changeValue");
};const onConfirm = (val: any) => {let endVal: any = "";for (let i = 0; i < columns.value.length; i++) {endVal += val[i];if ( i === 2 ) {endVal += " ";} else if (i >= 3 && i <= 5) {endVal += ":";} else if (i < columns.value.length - 1) {endVal += "-";}}// 判断最后一个字符是否是分隔符if (endVal.endsWith("-") || endVal.endsWith(":")) {endVal = endVal.slice(0, -1); // 删除最后一个字符}emits("changeValue", endVal);emits("confirm", val.selectedValues);}// 监听 isPicker 的变化
watch(isPicker, (val: any) => {if (!val) {emits("changeValue");}columns.value = [];getColumns();
});// 监听 showPicker 的变化
watch(() => props.showPicker, (val) => {isPicker.value = val;
});</script>

引用组件

 <DateTimePicker@changeValue="showEndPicker = false"ref="popup":showPicker="showEndPicker"@confirm="onEndConfirm"/>

结合 输入框一起使用

      <van-fieldclass="form-wrapper"v-model="formData.pzzy_enddate"is-linkrequiredlabel-width="130"name="datePicker"label="批准作业结束时间"placeholder="点击选择时间"@click="showEndPicker = true"/><DateTimePicker@changeValue="showEndPicker = false"ref="popup":showPicker="showEndPicker"@confirm="onEndConfirm"/>


http://www.mrgr.cn/news/50522.html

相关文章:

  • 网络IP地址冲突:含义、影响及应对策略‌
  • yarn error nopt@7.2.1: The engine “node“ is....Expected version “^14.17.0....
  • 【MySQL】VARCHAR和CHAR的区别?
  • PythonAI 学习大纲
  • 云原生介绍
  • 本地部署ComfyUI并添加强大的Flux.1开源文生图模型远程制作AI图片
  • 滑动窗口经典例题
  • <<迷雾>> 第11章 全自动加法计算机(8)--一只开关取数并相加 示例电路
  • 知道ip地址怎么看网络地址
  • Vue3+TypeScript+AntVX6实现Web组态(从技术层面与实现层面进行分析)内含实际案例教学
  • 加盟模式如何运营?有哪些好的技巧和方法!
  • 开放式耳机哪个品牌好?盘点开放式蓝牙耳机排行榜前五名
  • 基于YOLOv9的空中飞鸟识别检测系统(附项目源码和数据集下载)
  • 2024双11买什么东西比较好?双11必买好物清单推荐
  • 创建Python GUI的方法
  • pytorch resnet源码分析
  • JavaScript全面指南(五)
  • 现在的 AI 产品,有多难做?
  • 前端vue-获取验证码和重新获取验证码倒计时
  • C#学习笔记(二)