草庐IT

小程序坑(二) Vant时间选择器

againstAllOdds_ 2023-09-01 原文

Vant2.0 中DatetimePicker 时间选择

deteTimePicker默认是显示十年之内时间

(一)、 min-date max-date

1.如果出现空白,那就是传参没传对

2.如果报了 “RangeError: Invalid array length”,那就是最大时间和最小时间参数不对,(建议选第二种 .getTime() )

min-date max-date参数不对,都是因为没有加   getTime()

maxDate: new Date(2040, 11, 31)   如果这样显示不出来

就加上

maxDate: new Date(2040, 11, 31).getTime();变成时间戳

 (二)设置默认时间

        官方文档写的v-model,我用没效果,换成 :value 就可以了

 (三)@change  和@input 事件改变方法

1.@change事件拿到的值有1-2s延迟问题(开发者工具没问题,真机调试有延迟)

解决办法:我用的延时器 setTimeout,加了个load的按钮,体验不是很好,但是这是我唯一想到的方法了

附一(代码) 里有写

2.@input会有抖动问题

解决办法:换成@change,真机调试时候是有点卡

3.@change@input 改变获取时间

配套最下面的代码,这里只写逻辑

@input="onInput"

onInput(e){
    this.timeValue = e.detail
},
confirmFn(){  //确认按钮
    let moment = require("moment")
    this.timeValue = moment(this.timeValue).format("YYYY-MM-DD")
    this.confirBtn = false
	let timer = setTimeout(() => {
		learTimeout(timer)
		if(this.pickerInputNum == 0) this.couponData.start_time = this.timeValue
		if(this.pickerInputNum == 1) this.couponData.end_time = this.timeValue
		this.pickerView_isShow = false;
		this.confirBtn = true
	},1010)
}   
//用@input 就不需要用“附代码”下方的里面的onLoad(不是这段代码)


=========================================================================================
我是一个分界线
=========================================================================================


@change="onChange"

onChange(e){
    let that = this
	let timeVal = e.detail.getValues().join().replace(/,/g,"-")
	that.timeValue = timeVal
},
confirmFn() { // 确定按钮
	this.confirBtn = false
	let timer = setTimeout(() => {
	    clearTimeout(timer)
	    if(this.pickerInputNum == 0) this.couponData.start_time = this.timeValue
	    if(this.pickerInputNum == 1) this.couponData.end_time = this.timeValue
	    this.pickerView_isShow = false;
	    this.confirBtn = true	
    },1010)
},
onLoad(){
    let moment = require("moment")
	this.timeValue = moment(this.timeValue).format("YYYY-MM-DD")
}

        

    (四)确认和取消按钮

改变确认和取消文字

confirm-button-text=' 是' //确定按钮文字
cancel-button-text=' 否'  //取消按钮文字

隐藏顶部确认取消按钮

:show-toolbar="false"

或者

.van-picker__toolbar{
      display: none !important;
  }

 

 附一(代码)



<view class="choose-time-cell" @click="pickerInput(0)">
	<input type="text"  
	    :value="couponData.start_time" 
		placeholder="请选择开始时间"
		placeholder-class="placeHolder_class"
		class="input"
	/>
</view>
<view class="choose-time-cell" @click="pickerInput(1)">
	<input type="text"  
	    :value="couponData.start_time" 
		placeholder="请选择结束时间"
		placeholder-class="placeHolder_class"
		class="input"
	/>
</view>



<van-popup position="bottom" round 
:show="pickerView_isShow" @click-overlay='pickerView_isShow = false'>
	<view class="popPicker">
		<van-datetime-picker
			type="date"  //设置类型
			confirm-button-text=' ' //确定按钮文字(不改则去掉)
			cancel-button-text=' '  //取消按钮文字(不改则去掉)
            :min-date="minDate"    //设置最小的日期
			:max-date="maxDate"    //设置最大的日期
			@change="onChange()"     //改变时间调用
			:value="currentDate"   // 设置默认展示时间,v-model我用会报错
            swipe-duration='10'
		 />

	//因需求,需要自己写确认和取消按钮,如不需要可忽略
	<view class='btns'>
		<view class="closePicker" @click="pickerView_isShow = false">取消</view>
			<van-button type="primary" @click="confirmFn" v-if="confirBtn">确定</van-button>
			<van-button loading  type="primary" loading-type="spinner" v-else></van-button>
		</view>
	</view>
</van-popup>



data() {
	       return {
				pickerView_isShow:false ,// 弹出层是否显示
				pickerInputNum:0,  //选中的index
				minDate: new Date(2010, 11, 31).getTime(),
                maxDate: new Date(2040, 11, 31).getTime(),
				currentDate: new Date().getTime(),  //初始化时间
                confirBtn:true,  //解决延迟问题
				timeValue: new Date().getTime(),  //没调用change时候默认显示
			}
		},


methods:{
    onChange(e){
		let that = this
		let timeVal = e.detail.getValues().join().replace(/,/g,"-")
		that.timeValue = timeVal
	},
	pickerInput(num){
		// console.log('点击index' , num)
		this.pickerView_isShow = true  //点击显示
		this.pickerInputNum = num
	},	

    confirmFn(val) { // 确定按钮
		this.confirBtn = false  //显示加载中按钮 ,解决延迟问题
		let timer = setTimeout(() => {
		    clearTimeout(timer)
			if(this.pickerInputNum == 0) this.couponData.start_time = this.timeValue
			if(this.pickerInputNum == 1) this.couponData.end_time = this.timeValue
			this.pickerView_isShow = false;
			this.confirBtn = true  //去掉加载中按钮
					
		},1100)  //一秒后执行上面代码
	},
},
onLoad(e) {
    let moment = require("moment")
	this.timeValue = moment(this.timeValue).format("YYYY-MM-DD")
}

用less写的css 

// @all_color 可以换成 你想要的颜色
.popPicker{
		.van-picker__columns{
			margin-bottom: 142rpx;
		}
		.van-picker__toolbar{
			display: none !important;
		}
		.van-picker-column>view{
			line-height: 88rpx !important;
		}
		.pickerCol {
		  width: 80%;
		  height: 96rpx;
		  border-top: 1px solid transparent;
		  border-bottom: 1px solid transparent;
		  border-image: linear-gradient(to right,#FBFBFB,#e4e4e4,#FBFBFB) 1 1;
		  position: relative;
		  margin-left: 10%;
		}
		.btns{
			width: 100%;	
			height: 72rpx;
			font-size: 30rpx;
			display: flex;
			justify-content: center;
			position: absolute;
			left: 0;
			z-index: 999;
			bottom: 34rpx;
			
			view{
				width: 220rpx !important;
				height: 100%;
				line-height: 72rpx;
				border-radius: 15rpx;
				text-align: center;
			}
			.closePicker{
				color: @all_color;
				background-color: #e4e4e4;
				margin-right: 40rpx;
			}
			.getValue{
				background-color: @all_color;
				color: #FFFFFF;
			}
			.van-button--primary{
				background-color: @all_color;
				width: 220rpx;
				height: 100%;
				line-height: 72rpx;
				border-radius: 15rpx;
				border: none;
			}
		}
		
	}

有关小程序坑(二) Vant时间选择器的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  3. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  4. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  7. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  8. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  9. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  10. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

随机推荐