• web前端之uniApp实现选择时间功能



    1、孙子组件

    1.1、html部分

    <template>
    	<view>
    		<checkbox-group @change="checkboxChange">
    			<view class="check_number_box">
    				<view class="check_number_item" v-for="(item, i) in checkNumberData" :key="i">
    					<view>
    						
    						<checkbox :value="item.toString()" :checked="item == defaultValue[0]" />
    					view>
    					<text>{{ item }}text>
    				view>
    			view>
    		checkbox-group>
    	view>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    1.2、JavaScript部分

    export default {
    	props: {
    		checkNumberData: {
    			type: Number,
    			default: () => {
    				return 7
    			},
    		}
    	},
    
    	data() {
    		return {
    			defaultValue: [1]
    		}
    	},
    
    	methods: {
    		checkboxChange(event) {
    			this.defaultValue = event.detail.value;
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    1.3、css部分

    * {
    	margin: 0;
    	padding: 0;
    }
    
    .check_number_box {
    	box-sizing: border-box;
    	padding: 10rpx 50rpx;
    	display: grid;
    	grid-template-columns: repeat(5, 1fr);
    	grid-gap: 10rpx 10rpx;
    }
    
    .check_number_item {
    	display: flex;
    	justify-content: flex-start;
    	align-items: center;
    	font-size: 30rpx;
    }
    
    .check_number_item>text {
    	margin-left: 6rpx;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2、子组件

    2.1、html部分

    <template>
    	<view>
    		<view class="set_cycle_box">
    			<radio-group @change="radioChange">
    				<view class="cycle_box">
    					
    					<view class="cycle_item">
    						<view>每日view>
    						<view>
    							<radio value="1" checked="true" />
    						view>
    					view>
    
    					
    					<view class="cycle_item">
    						<view>每周view>
    						<view>
    							<radio value="2" />
    						view>
    					view>
    					<checkNumber :checkNumberData="weekCycle" v-show="current == 2" ref="weekData">checkNumber>
    
    					
    					<view class="cycle_item">
    						<view>每月view>
    						<view>
    							<radio value="3" />
    						view>
    					view>
    					<checkNumber :checkNumberData="dayCycle" v-show="current == 3" ref="dayData">checkNumber>
    
    					
    					<view class="cycle_item">
    						<view>自定义view>
    						<view>
    							<radio value="4" />
    						view>
    					view>
    					<view class="set_cycle_title" v-show="current == 4">
    						请选择月份
    					view>
    					<checkNumber :checkNumberData="monthCycle" v-show="current == 4" ref="monthCustomaData">checkNumber>
    					<view class="set_cycle_title" v-show="current == 4">
    						请选择日期
    					view>
    					<checkNumber :checkNumberData="dayCycle" v-show="current == 4" ref="dayCustomaData">checkNumber>
    				view>
    			radio-group>
    
    			<view class="submit" @click="submitBtn">
    				确认
    			view>
    		view>
    	view>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    2.2、JavaScript部分

    import checkNumber from '@/components/checkNumber/checkNumber.vue'
    
    export default {
    	components: {
    		checkNumber
    	},
    	
    	data() {
    		return {
    			current: 1,
    			// 给孙子组件传递参数(start)
    			weekCycle: 7,
    			dayCycle: 31,
    			monthCycle: 12,
    			// 给孙子组件传递参数(end)
    		}
    	},
    
    	methods: {
    		// 单选状态
    		radioChange(event) {
    			let i = event.detail.value;
    			this.current = i;
    		},
    
    		// 确认
    		submitBtn() {
    			let i = this.current;
    			i = Number(i);
    
    			let submitData = {};
    
    			switch (i) {
    				case 2:
    					submitData.type = i;
    					submitData.submitWeek = this.$refs.weekData.defaultValue;
    					break;
    				case 3:
    					submitData.type = i;
    					submitData.submitDay = this.$refs.dayData.defaultValue;
    					break;
    				case 4:
    					submitData.type = i;
    					submitData.submitCustomaMonth = this.$refs.monthCustomaData.defaultValue;
    					submitData.submitCustomaDay = this.$refs.dayCustomaData.defaultValue;
    					break;
    				default:
    					submitData.type = i;
    					submitData.submitDay = [1];
    			}
    
    			this.$emit('clickSetCycle', submitData)
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    2.3、css部分

    .set_cycle_box {
    	margin-top: 60rpx;
    }
    
    .cycle_box {
    	padding: 0 50rpx;
    	font-size: 30rpx;
    	font-weight: 600;
    }
    
    .cycle_item {
    	display: flex;
    	justify-content: space-between;
    	align-items: center;
    	margin: 16rpx 0;
    }
    
    .submit {
    	background-color: #007AFF;
    	color: #FFFFFF;
    	font-size: 32rpx;
    	font-weight: 600;
    	width: 30%;
    	line-height: 50rpx;
    	text-align: center;
    	border-radius: 10rpx;
    	position: relative;
    	left: 50%;
    	transform: translate(-50%);
    	margin: 60rpx 0;
    }
    
    .set_cycle_title {
    	margin: 16rpx 0;
    	font-size: 26rpx;
    	color: #888888;
    	padding-left: 50rpx;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    3、父组件

    3.1、html部分

    <template>
    	<view>
    		<setCycle @clickSetCycle="cycleControl">setCycle>
    	view>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.2、JavaScript部分

    import setCycle from '../../components/setCycle/setCycle.vue'
    
    export default {
    	comments: {
    		setCycle
    	},
    
    	data() {
    		return {
    			
    		}
    	},
    
    	methods: {
    		cycleControl(data) {
    			console.log(data);
    			// 
    		},
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4、效果图

    效果图

  • 相关阅读:
    springboot整合ShardingSphere5.2.1(最新版)
    30.在springboot中使用thymeleaf的模板(include,insert)
    前端vue实现页面加水印文字 单个页面所有页面加水印(更新版)
    七、《图解HTTP》加密的协议HTTPS
    2024.4.2每日一题
    将小程序容器技术应用到物联网IoT生态建设中
    5.使用日志+自定义全局异常过滤器
    【Python】从同步到异步多核:测试桩性能优化,加速应用的开发和验证
    【Android】VirtualDisplay创建流程及原理
    Docker+K3S搭建集群
  • 原文地址:https://blog.csdn.net/weixin_51157081/article/details/131692783