uview upicker时间选择器(附Demo)

目录

前言

uniapp时间选择器,是upicker,与微信小程序还是有些区别

补充官网的基本知识:uview官网

官网的展示例子如下:(但是没Demo)

正文

通过上面的展示图,复刻一个类似Demo图:

以下代码过滤掉一些无关内容

html 复制代码
<template>

	<view>
		<u-collapse :value="['0']" ref="collapse">
			<u-collapse-item title="搜索" name="0">
				<u--form :model="formModel" ref="form">
				
					
					<u-form-item @click="dateTimePicker = true" label="时间:" labelWidth="100rpx" prop="maintenanceTime" style="align-items: center;">
						<u--input
							v-model="formModel.maintenanceTime"
							prefixIcon="calendar" >
						</u--input>
					</u-form-item>
				</u--form>
			</u-collapse-item>
		</u-collapse>
		
		<u-datetime-picker
			@confirm="dateTimePickerConfirm" 
			@cancel="dateTimePicker = false"
			:show="dateTimePicker" 
			v-model="timeValue"
			ref="datetimePicker"
			mode="datetime"
			closeOnClickOverlay
			:defaultIndex="defaultTimeIndex">
		</u-datetime-picker>
		

	</view>
</template>

对应script页面如下:

js 复制代码
<script>
	
	export default {
		data(){
			return {
				dateTimePicker: false,
				}
		},
		onReady() {
			console.log('onReady')
			
			// 微信小程序需要用此写法
			this.$refs.datetimePicker.setFormatter(this.formatter)
		},
		methods:{
			
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				if (type === 'hour'){
					return `${value}时`
				}
				if (type === 'minute'){
					return `${value}分`
				}
				return value
			},
			dateTimePickerConfirm(e){
				this.dateTimePicker = false
				console.log(e.value,e.mode)
				this.formModel.maintenanceTime = this.$u.date(e.value,'yyyy-mm-dd hh:MM:ss')
				this.$refs.form.validateField('maintenanceTime')
				console.log(this.formModel.maintenanceTime)
			}
	}
</script>


<style lang="scss" scoped>
	.u-popup-slot {
		width: 200px;
		height: 80px;
		@include flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 10rpx;
	}
</style>
相关推荐
Sailing3 小时前
🔥 React 高频 useEffect 导致页面崩溃的真实案例:从根因排查到彻底优化
前端·react.js·面试
aduzhe3 小时前
关于在嵌入式中打印float类型遇到的bug
前端·javascript·bug
Sailing3 小时前
🔥 大模型时代最讽刺的职业出现了:“大模型善后工程师”
前端·openai·ai编程
o***Z4483 小时前
前端组件表单验证,React Hook Form与VeeValidate
前端·react.js·前端框架
xiaoxue..3 小时前
Vibe Coding之道:从Hulk扩展程序看Prompt工程的艺术
前端·人工智能·prompt·aigc
程序猿小蒜3 小时前
基于springboot的汽车资讯网站开发与实现
java·前端·spring boot·后端·spring
q***98523 小时前
前端的dist包放到后端springboot项目下一起打包
前端·spring boot·后端
鹏多多3 小时前
vue过滤器filter的详解及和computed的区别
前端·javascript·vue.js
Mintopia3 小时前
🚀 Trae 国际版 Max 模型升级:算力与智能的共舞
前端·人工智能·trae
Mintopia3 小时前
🌍 WebAIGC的高算力消耗:技术优化与绿色计算路径
前端·人工智能·trae