uniapp日期选择器

<template>
	<view>
		<view class="uni-padding-wrap">
			<view class="uni-title" @tap="date">
				日期:{{nian}}{{yue}}{{ri}}</view>
		</view>

		<uni-popup ref="wenzi" type="bottom" :maskClick=false>
			
			<view class="date-select">
				<view class="btn u-f u-f-jsb">
					<view class="btn-left" @tap="concel">
						取消
					</view>
					<view class="btn-right" @tap="confirm">
						确定
					</view>
				</view>
				<picker-view class="picker-view" v-if="visible" :indicator-style="indicatorStyle" :value="value" @change="bindChange">
					<picker-view-column>
						<view class="item" v-for="(item,index) in years" :key="index">{{item}}</view>
					</picker-view-column>
					<picker-view-column>
						<view class="item" v-for="(item,index) in months" :key="index">{{item}}</view>
					</picker-view-column>
					<picker-view-column>
						<view class="item" v-for="(item,index) in days" :key="index">{{item}}</view>
					</picker-view-column>
				</picker-view>
			</view>

		</uni-popup>


	</view>
</template>

<script>
	import uniPopup from '../../components/uni-popup/uni-popup.vue'
	export default {
		data() {
			const date = new Date()
			const years = []
			const year = date.getFullYear()
			const months = []
			const month = date.getMonth() + 1
			const days = []
			const day = date.getDate()

			for (let i = 2020; i <= date.getFullYear(); i++) {
				years.push(i)
			}

			for (let i = 1; i <= 12; i++) {
				months.push(i)
			}

			for (let i = 1; i <= 31; i++) {
				days.push(i)
			}
			return {
				title: 'picker-view',
				years,
				year,
				months,
				month,
				days,
				day,
				value: [0, month - 1, day - 1],
				/**
				 * 解决动态设置indicator-style不生效的问题
				 */
				visible: true,
				indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`,
				nian:'',
				yue:'',
				ri:''
			}
		},
		methods: {
			bindChange(e) {
				let a;
				let y;
				console.log(e);
				let val = e.detail.value;
				y = this.years[val[0]];
				a = this.months[val[1]];
				this.day = this.days[val[2]];
				this.month = a;
				this.year = y;
				this.days = [];
				if (a||y) {
					if (a == 1 || a == 3 || a == 5 || a == 7 || a == 8 || a == 10 || a == 12) {
						for (let i = 1; i <= 31; i++) {
							this.days.push(i)
						}
					} else if (a == 4 || a == 6 || a == 11 || a == 9) {
						for (let i = 1; i <= 30; i++) {
							this.days.push(i)
						}
						
					} else if (a == 2) {
						if (this.year % 4 == 0 && (this.year % 100 != 0 || this.year % 400 == 0)) {
							for (let i = 1; i <= 29; i++) {
								this.days.push(i)
							}
						} else {
							for (let i = 1; i <= 28; i++) {
								this.days.push(i)
							}
						}
					}
				
				}
			},
			date(e) {
				console.log(e);
				this.$refs.wenzi.open();
				console.log();
			},
			concel(){
				this.$refs.wenzi.close();
			},
			confirm(){
				this.nian =this.year;
				this.yue = this.month;
				this.ri=this.day;
				this.$refs.wenzi.close();
			}
		}
	}
</script>

<style scoped lang="scss">
	// picker-view {
	// 	position: absolute;
	// 	width: 100%;
	// 	height: 500upx;
	// 	bottom: 20upx;
	// }

	

	.date-select {
		width: 100%;
		height: 700upx;
		border-top-left-radius: 40upx;
		border-top-right-radius: 40upx;
		background-color: #FFFFFF;
		position: relative;
		.picker-view {
			background-color: #EEEEEE;
			position: absolute;
			width: 100%;
			height: 500upx;
			bottom: 20upx;
			.item {
				line-height: 100rpx;
				font-size: 34upx;
				text-align: center;
			}
		}
		.btn{
				width: 100%;
				height: 100upx;
				// margin-top: 20upx;?
				box-sizing: border-box;
				padding: 40upx 30upx;
				.btn-left,.btn-right{
					color: #FFFFFF;
					width: 150upx;
					height: 90upx;
					line-height: 90upx;
					text-align: center;
					// border: 1upx solid #BEBEBE;
					border-radius: 15upx;
				}
				.btn-left{
					background-color: #BEBEBE;
				}
				.btn-right{
					background-color: #1cb419;
				}
			}
	}
	
</style>

在这里插入图片描述

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值