VUE 日期组件(包括年选择)
封装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 日期组件(包括年选择)的更多相关文章
- 使用WdatePicker日期组件时,选择日期后,执行某个方法
WdatePicker({onpicked:function(){alert(123);},dateFmt:'yyyy年MM月dd日',maxDate:'%y-%M-%d'}) 1.onpicked: ...
- vue自定义日期组件
vue-datepicker 基于 vuejs 2.x 可自定义主题的日期组件 github Usage 只需要在项目中加入 calendar.vue,就可以使用了. 向组件传入 prop 即可改变 ...
- vue 移动端轻量日期组件不依赖第三方库
Vue版移动端日期选择组件 1.优点:不需要依赖其他第三方库,灵活可配置: 不需要依赖第三方组件的vue日期移动端组件 小轮子 轻量可复用: https://github.com/BeckReed ...
- iview的table组件中加入超链接组件,可编辑组件,选择组件,日期组件
这篇文章主要介绍了iview的table组件中使用选择框,日期,可编辑表格,超链接等组件. 1.select选择组件 // tableColumn数组响应对象需要传入一个固定的option数组,如果该 ...
- vue自定义可输入的选择框组件
vue自定义可输入的选择框组件 props: 属性 说明 类型 默认值 selectDataList 下拉框中的内容 Array 空数组([]) value 输入框中的内容 String 空字符串(& ...
- 自定义Vue&Element组件,实现用户选择和显示
在我们很多前端业务开发中,往往为了方便,都需要自定义一些用户组件,一个是减少单一页面的代码,提高维护效率:二个也是方便重用.本篇随笔介绍在任务管理操作中,使用自定义Vue&Element组件, ...
- Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例
Vue UI组件 开发框架 服务端 辅助工具 应用实例 Demo示例 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 ...
- vue统计组件库和ui框架
UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和WeUI的组件库 iview ★6634 - 基于 Vuejs 的开源 UI ...
- 测试开发【提测平台】分享12-掌握日期组件&列表状态格式化最终实现提测管理多条件搜索展示功能
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 本章内容思维导图如下,由于需要各种状态下的菜单操作,所以需要先实现提测信息的列表基础页面,然后再推进其他需求开发 基本知识点学习 Date ...
随机推荐
- 通过Kuberneters Goat学习K8S安全(上)
实验环境:https://katacoda.com/madhuakula/scenarios/kubernetes-goat 0x1.敏感信息泄露利用 第一关是代码泄露利用,打开网站后显示: 告诉我们 ...
- java实现稀疏矩阵的压缩与解压
任务要求 把棋盘当作一个稀疏矩阵,0表示没棋,1表示黑棋,2表示蓝棋. 把该稀疏矩阵压缩以三元组形式表示并以文件形式保存,再写另一个程序读取文件中的信息把压缩后的三元组还原成原来的稀疏矩阵. 其中三元 ...
- Oracle 关于v$之类的视图使用说明
官方文档:https://docs.oracle.com/en/database/oracle/oracle-database/19/cncpt/data-dictionary-and-dynamic ...
- Dubbo 必须依赖的包有哪些?
Dubbo 必须依赖 JDK,其他为可选.
- 阐述 final、finally、finalize 的区别?
final:修饰符(关键字)有三种用法:如果一个类被声明为 final,意味 着它不能再派生出新的子类,即不能被继承,因此它和 abstract 是反义词.将 变量声明为 final,可以保证它们在使 ...
- MariaDB 存储引擎一览(官方文档翻译)
inline-translate.translate { } inline-translate.translate::before, inline-translate.translate::after ...
- 序列化多表操作、请求与响应、视图组件(子类与拓展类)、继承GenericAPIView类重写接口
今日内容概要 序列化多表操作 请求与相应 视图组件 内容详细 1.序列化多表操作 模型类 models.py中 # 新建django项目 # 创建表 模型类models.py中: from djang ...
- C++中初始化列表的使用(总结)
原文链接 https://www.cnblogs.com/dishengAndziyu/p/10906081.html 参考链接:https://www.cnblogs.com/laiqun/p/57 ...
- 分压杯频LLC变换器
- javascript 理解和使用回调函数
在javascript中,function是内置的类对象,也就是说它是一种类型的对象,可以和其他String.Array.Number.Objec类的对象一样用于内置对象的管理.因为function实 ...