封装vant 日期组件实现可以选择年份

<template>
<div class="yearMonMain">
<div class="label">{{ label }}</div>
<div class="content" @click="onShowDatePicker">
{{ date }}
<van-icon name="arrow-up" v-if="showDatePicker == true" />
<van-icon name="arrow-down" v-else />
</div>
<van-popup v-model="showDatePicker" position="bottom" ref="pop">
<van-picker
v-if="type == 'year'"
ref="yearPicker"
title="请选择年"
show-toolbar
:columns="yearList"
@confirm="onConfirmYear"
@cancel="onCancel"
/>
<van-datetime-picker
v-else
v-model="currentDate"
:type="type"
:title="title"
:max-date="maxDate"
:min-date="minDate"
@confirm="onConfirm"
@cancel="onCancel"
:formatter="formatter"
/>
</van-popup>
</div>
</template> <script>
export default {
name: "YearMonthSel",
data() {
let yearList = []
let year = new Date().getFullYear()
let month = new Date().getMonth() + 1
let currentDate = new Date()
// console.log("YearMonthSel defaultValue===", this.defaultValue)
if (this.defaultValue) {
if (this.type == "year") {
year = this.defaultValue
for (let i = 1900; i <= year; i++) {
yearList.push({ code: i, text: i })
}
} else {
year = this.defaultValue[0]
month = this.defaultValue[1]
currentDate = new Date(year, month)
}
} return {
currentDate,
year,
month,
showDatePicker: false,
maxDate: new Date(),
minDate: new Date("1900-01-01"),
yearList,
}
},
props: {
label: {
type: String,
default: "选择年月",
},
title: {
type: String,
default: "选择年月",
},
type: {
type: String,
default: "year-month",
},
defaultValue: {
type: String | Number,
default: "",
},
},
methods: {
formatter(type, val) {
if (type === "year") {
return `${val}年`
} else if (type === "month") {
return `${val}月`
}
return val
},
onShowDatePicker() {
this.showDatePicker = true
setTimeout(() => {
if (this.type == "year") {
const picker = this.$refs.yearPicker //获取组件实例
if (picker) {
picker.setValues([this.year]) //setIndexes()中的参数是一个数组
}
}
})
},
onConfirm(time) {
let date = new Date(time)
let year = date.getFullYear()
let month = date.getMonth() + 1
if (this.year != year || this.month != month) {
this.year = year
this.month = month
this.$emit("onChange", [year, month])
}
this.$emit("onConfirm", [year, month]) this.showDatePicker = false
},
onConfirmYear(v) {
if (this.year != v.code) {
this.year = v.code
this.$emit("onChange", v.code)
}
this.showDatePicker = false
this.$emit("onConfirm", v.code)
},
onCancel() {
this.showDatePicker = false
},
},
computed: {
date: function() {
switch (this.type) {
case "year":
return this.year + "年"
default:
return this.year + "年" + this.month + "月"
}
},
},
}
</script> <style lang="less" scoped>
.yearMonMain {
width: 100%;
display: flex;
.label {
text-align: center;
max-width: 100px;
color: #646566;
}
}
.content {
flex-grow: 1;
text-align: center;
}
</style>

VUE 日期组件(包括年选择)的更多相关文章

  1. 使用WdatePicker日期组件时,选择日期后,执行某个方法

    WdatePicker({onpicked:function(){alert(123);},dateFmt:'yyyy年MM月dd日',maxDate:'%y-%M-%d'}) 1.onpicked: ...

  2. vue自定义日期组件

    vue-datepicker 基于 vuejs 2.x 可自定义主题的日期组件 github Usage 只需要在项目中加入 calendar.vue,就可以使用了. 向组件传入 prop 即可改变 ...

  3. vue 移动端轻量日期组件不依赖第三方库

    Vue版移动端日期选择组件 1.优点:不需要依赖其他第三方库,灵活可配置: 不需要依赖第三方组件的vue日期移动端组件  小轮子 轻量可复用:  https://github.com/BeckReed ...

  4. iview的table组件中加入超链接组件,可编辑组件,选择组件,日期组件

    这篇文章主要介绍了iview的table组件中使用选择框,日期,可编辑表格,超链接等组件. 1.select选择组件 // tableColumn数组响应对象需要传入一个固定的option数组,如果该 ...

  5. vue自定义可输入的选择框组件

    vue自定义可输入的选择框组件 props: 属性 说明 类型 默认值 selectDataList 下拉框中的内容 Array 空数组([]) value 输入框中的内容 String 空字符串(& ...

  6. 自定义Vue&Element组件,实现用户选择和显示

    在我们很多前端业务开发中,往往为了方便,都需要自定义一些用户组件,一个是减少单一页面的代码,提高维护效率:二个也是方便重用.本篇随笔介绍在任务管理操作中,使用自定义Vue&Element组件, ...

  7. Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例

    Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 ...

  8. vue统计组件库和ui框架

    UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和WeUI的组件库 iview ★6634 - 基于 Vuejs 的开源 UI ...

  9. 测试开发【提测平台】分享12-掌握日期组件&列表状态格式化最终实现提测管理多条件搜索展示功能

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 本章内容思维导图如下,由于需要各种状态下的菜单操作,所以需要先实现提测信息的列表基础页面,然后再推进其他需求开发 基本知识点学习 Date ...

随机推荐

  1. spring cloud 的核心组件有哪些?

    Eureka:服务注册于发现. Feign:基于动态代理机制,根据注解和选择的机器,拼接请求 url 地址,发起请求. Ribbon:实现负载均衡,从一个服务的多台机器中选择一台. Hystrix:提 ...

  2. Redis 的回收策略(淘汰策略)?

    volatile-lru:从已设置过期时间的数据集(server.db[i].expires)中挑选最近最 少使用的数据淘汰 volatile-ttl:从已设置过期时间的数据集(server.db[i ...

  3. SQLyog创建用户并授权的过程

    点击你要授权的数据库然后点击用户管理器 然后输入用户名和密码主机选localhost 然后点击创建,然后选择你创建的数据库全选 最后保存就可以了

  4. cache + redis(一)

    一.缓存的概念 1.Buffer,Cache的区别: buffer: 一般用于写操作,写缓冲. cache: 一般用于读操作,读缓存. 它们都是解决速度不一致的问题,都设计到IO操作. 2.Cache ...

  5. log4J——在Spring中的使用

    log4J简介 1.通过 log4j 可以看到程序运行过程中更详细的信息 (1)经常使用 log4j 查看日志 2.使用 (1)导入 log4j 的jar包 (2)复制 log4j 的配置文件,复制到 ...

  6. js 遮罩效果

    -------------------------------tipswindown.js------------------------------ ///--------------------- ...

  7. 直接使用sublime编译stylus

    stylus介绍 Stylus 是一个CSS的预处理框架,2010年产生,来自Node.js社区,主要用来给Node项目进行CSS预处理支持,所以 Stylus 是一种新型语言,可以创建健壮的.动态的 ...

  8. 深入理解ES6之《扩展对象》

    属性初始值的简写 当对象字面量只有一个属性的名称时,JS引擎会在可访问作用域中查找其同名变量:如果找到则该变量的值被赋给对象字面量里的同名属性 function createPerson(name, ...

  9. web音频流转发之AudioNode

    前言 上一章地址: web音频流转发之音频源下一张地址:web音频流转发之音视频直播在这一章我说几个我们需要用到的音频处理模块也就3个吧,包括我们转发流是需要用到的核心模块.更多模块请看MDN,或者看 ...

  10. Element UI table参数中的selectable的使用

    Element UI table参数中的selectable的使用中遇到的坑:页面: <el-table-column :selectable='selectable' type="s ...