vant-ui的DatetimePicker 组件只能选择年月日时分,可能是组件维护者认为秒的选择用途不多,但是今天的需求中就是需要选择年月日时分秒所以就对DatetimePicker的组件封装成了可以选择年月日时分秒,直接上代码:
封装成组件:
<template>
<div>
<!-- 弹出层 -->
<van-popup
get-container="body"
v-model="isPicker"
position="bottom"
@close="confirmOn"
>
<!-- 时间选择 -->
<div v-if="isPicker">
<van-picker
ref="picker"
show-toolbar
title="请选择时间"
:columns="columns"
@change="onChange"
@cancel="cancelOn"
@confirm="onConfirm"
/>
</div>
</van-popup>
</div>
</template>
<script>
export default {
name: "popup",
props: ["showPicker", "values"],
data() {
return {
isPicker: false, //是否显示弹出层
columns: [],
//所有时间
Mdays: "", //弹窗关闭时月份所在值
Dindex: null,
};
},
watch: {
isPicker(val) {
!val && this.$emit("changeValue");
this.columns = [];
this.getcolumns();
},
showPicker(val) {
this.isPicker = val;
},
values(val) {
if (val == "") {
this.Mdays = "";
this.Dindex = null;
} else {
console.log(val);
}
},
},
methods: {
getCountDays(year, month) {
//获取某年某月多少天
var day = new Date(year, month, 0);
return day.getDate();
},
getcolumns() {
var strtime = this.values;
var date = new Date(strtime);
var vmoduletime = date.getTime();
if (this.values != "") {
var vmoduledate = new Date(vmoduletime);
} else {
var vmoduledate = new Date(); //没有传入时间则默认当前时刻
}
var Y = vmoduledate.getFullYear();
var M = vmoduledate.getMonth();
var D = vmoduledate.getDate();
var h = vmoduledate.getHours();
var m = vmoduledate.getMinutes();
var s = vmoduledate.getSeconds();
var year = {}; //获取前后十年数组
year.values = [];
var Currentday = new Date().getFullYear();
for (var i = Currentday - 10; i < Currentday + 10; i++) {
year.values.push(i);
}
year.defaultIndex = year.values.indexOf(Y);
//设置默认选项当前年
this.columns.push(year);
var month = {};
//获取12月数组
month.defaultIndex = M;
month.values = Object.keys(Array.apply(null, { length: 13 })).map(
function (item) {
if (+item + 1 <= 10) {
return "0" + item;
} else if (+item + 1 == 11) {
return +item;
} else {
return (+item + 0).toString();
}
}
);
month.values.splice(0, 1);
this.columns.push(month); //获取当月的天数
var days = this.getCountDays(Y, this.Mdays == "" ? M + 1 : this.Mdays);
var day = {}; //创建当月天数数组
day.defaultIndex = this.Dindex == null ? D - 1 : this.Dindex;
day.values = Object.keys(Array.apply(null, { length: days + 1 })).map(
function (item) {
if (+item + 1 <= 10) {
return "0" + item;
} else if (+item + 1 == 11) {
return +item;
} else {
return (+item + 0).toString();
}
}
);
day.values.splice(0, 1);
this.columns.push(day);
var hour = {}; //创建小时数组
hour.defaultIndex = h;
hour.values = Object.keys(Array.apply(null, { length: 24 })).map(
function (item) {
if (+item + 1 <= 10) {
return "0" + item;
} else if (+item + 1 == 11) {
return +item;
} else {
return (+item + 0).toString();
}
}
);
this.columns.push(hour);
var mi = {}; //创建分钟数组
mi.defaultIndex = m;
mi.values = Object.keys(Array.apply(null, { length: 60 })).map(function (
item
) {
if (+item + 1 <= 10) {
return "0" + item;
} else if (+item + 1 == 11) {
return +item;
} else {
return (+item + 0).toString();
}
});
this.columns.push(mi);
var ss = {}; //创建秒数数组
ss.defaultIndex = s;
ss.values = Object.keys(Array.apply(null, { length: 60 })).map(function (
item
) {
if (+item + 1 <= 10) {
return "0" + item;
} else if (+item + 1 == 11) {
return +item;
} else {
return (+item + 0).toString();
}
});
this.columns.push(ss);
},
onChange(values, a) {
console.log(a);
//a为所有列备选项值的数组
var days = this.getCountDays(a[0], a[1]);
var newdays = {};
newdays.values = Object.keys(Array.apply(null, { length: days + 1 })).map(
function (item) {
if (+item + 1 <= 10) {
return "0" + item;
} else if (+item + 1 == 11) {
return +item;
} else {
return (+item + 0).toString();
}
}
);
newdays.values.splice(0, 1);
this.$refs.picker.setColumnValues(2, newdays.values); //设置第三列的值
this.$refs.picker.setColumnIndex(2, a[2] - 1); //设置第三列索引
}, // 关闭弹框
confirmOn() {
var currentM = this.$refs.picker.getColumnValue(1);
this.Mdays = currentM.substr(0, 2);
this.Dindex = this.$refs.picker.getColumnIndex(2);
},
//时间选择器关闭
cancelOn() {
this.$emit("changeValue");
}, // 时间选择器确定
onConfirm(val, index) {
var endval =
val[0] +
"-" +
val[1] +
"-" +
val[2] +
" " +
val[3] +
":" +
val[4] +
":" +
val[5];
this.$emit("changeValue", endval);
this.$emit("confirm", endval);
},
},
};
</script>
引入:
import DatetimePicker from "@/components/common/datetimePicker";
components: {
DatetimePicker,
},
调用:
<DatetimePicker
:values="timePick"
@changeValue="timeShow = false"
ref="popup"
:showPicker="timeShow"
@confirm="selectTime"
/>
data() {
timePick: "",
timeShow:false
},
methods: {
selectTime(val) {
console.log(val)
},
}