web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 好玩
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

van-datetime-picker只显示年份(改造后)

1831 ℃

项目搭建用的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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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
  }
}
  • 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

el-table合并行的通用方法(附js代码)

vue前端分页功能实现代码(附js代码)

el-table利用:row-style="rowClass"设置指定行变色

一款免费开源效果酷炫​的 Vue / React 大屏数据展示组件库——DataV 

vue项目动态设置background背景色,解决颜色被替换问题

标签: DatetimePicker, van-datetime-picker, vant时间组件

上面是“van-datetime-picker只显示年份(改造后)”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3977.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > vuejs
本文共计1362个字,预计阅读时长10分钟
生活小工具,收录了80多款小工具
上一篇: 介绍几款带有中国风古韵的免费可商用中文字体
下一篇: 推荐9款支持 Vue3 免费开源的前端 UI 组件库
回到顶部
x 打工人ai神器