项目搭建用的vant组件库,但是vant官网上DatetimePicker
时间组件van-datetime-picker
没有单独显示年份的组件,下面给大家介绍一下改造后的代码!
template代码
<van-cell-group>
<van-field style="text-align:center" readonly clickable placeholder="请选择年份" :value="dateValue" @click="showPicker = true" />
<van-popup v-model="showPicker" round position="bottom">
<van-picker title="请选择年份" show-toolbar :columns="columns" :default-index="select" @confirm="yearConfirm" @cancel="cancel" />
</van-popup>
</van-cell-group>
script代码
data() {
return {
dateValue: '',
showPicker: false,
select: null,
columns: []
}
},
created() {
this.getCurrentYear()
this.getYearData()
},
methods: {
formatDates(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
},
//获取默认最新时间
getCurrentYear() {
var nowTime = new Date()
let year = nowTime.getFullYear()
let month = nowTime.getMonth()
let day = nowTime.getDate()
var dateValue = ''
dateValue = this.formatDates(new Date(year, month, day))
this.dateValue = dateValue.slice(0, 4)
this.signs(this.dateValue)
},
//年选择器
yearConfirm(value) {
this.dateValue = value.toString()
this.signs(this.dateValue)
this.showPicker = false
},
//年数据
getYearData() {
var nowTime = new Date(this.dateValue)
let year = nowTime.getFullYear()
let month = nowTime.getMonth()
let day = nowTime.getDate()
for (let i = 1901; i < 2099; i++) {
this.columns.push(i)
}
var years = this.formatDates(new Date(year, month, day))
var Year = years.slice(0, 4)
this.select = this.columns.indexOf(Number(Year))
},
//点击取消按钮时触发的事件
cancel() {
this.showPicker = false
}
}
以上是编程学习网小编为您介绍的“van-datetime-picker只显示年份(改造后)”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:van-datetime-picker只显示年份(改造后)
基础教程推荐
猜你喜欢
- css下div下同行多元素右对齐 2024-01-19
- js实现简单实用的AJAX完整实例 2022-12-15
- Ajax实现上传图像功能的示例详解 2023-02-24
- 1.1 HTML & CSS汇总 2023-10-27
- 从零学CSS系列之文本属性 2024-03-10
- javascript检查浏览器是否已经启用XX功能 2024-01-05
- VUE——组件(四)组件的高级用法 2023-10-08
- 利用js-cookie实现前端设置缓存数据定时失效 2024-02-06
- Vue入门笔记Day4 2023-10-08
- nginx搭建vue项目,刷新页面404解决方法? 2024-12-09