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>
相关推荐
遇到困难睡大觉哈哈21 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户479492835691521 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
万岳软件开发小城21 小时前
教育APP/小程序开发标准版图:课程、题库、直播、学习一站式梳理
大数据·php·uniapp·在线教育系统源码·教育app开发·教育软件开发
p***43481 天前
前端在移动端中的网络请求优化
前端
g***B7381 天前
前端在移动端中的Ionic
前端
拿破轮1 天前
使用通义灵码解决复杂正则表达式替换字符串的问题.
java·服务器·前端
whltaoin1 天前
【 Web认证 】Cookie、Session 与 JWT Token:Web 认证机制的原理、实现与对比
前端·web·jwt·cookie·session·认证机制
Aerelin1 天前
爬虫playwright入门讲解
前端·javascript·html·playwright
5***o5001 天前
前端在移动端中的NativeBase
前端
灵魂学者1 天前
Vue3.x —— 父子通信
前端·javascript·vue.js·github