最近在写后台管理系统,需要实现一个功能就是农历日历组件,需要实现 选择日历/回显日历等功能
效果图如下:
vue-jlunar-datepicker
这个插件本身是基于vue2.0
和elementUi
框架来实现的。
所以后面会有关于部分样式的改动。
参考大神的vue农历日期组件的链接:https://www.cnblogs.com/zeussbook/p/16208969.html
gitee 农历日期组件的链接:https://toscode.gitee.com/tuhe32/vue-jLunar-datePicker
下面介绍该组件的使用步骤:
1.插件的安装——npm install vue-jlunar-datepicker --save
如果安装过程中,出现报错,一般在终端中会显示出来解决办法
我这边确实报错了,提示的信息是elementUi
版本不兼容的问题,解决方法就是:
npm install vue-jlunar-datepicker --save --force
2.插件在main.js
中引入并注册
import Vue from 'vue';//这行main.js中本来就有
import JDatePicker from 'vue-jlunar-datepicker';
Vue.component("j-date-picker",JDatePicker);
3.html
页面中的使用
<j-date-picker
v-model="form.birthdaydate"
style="width: 100%"
:placeholder="placeholder"
:picker-options="pickerOptions"
:rangeSeparator="rangeSeparator"
@change="onDateChange"
:disabled="disabled"
:showLunarClass="showLunarClass"
:showLunarControl="showLunarControl"
:type="type"
:showBackYears="showBackYears"
:showLunarIcon="showLunarIcon"
:format="format"
>
</j-date-picker>
data
中参数的定义:
data(){
return{
form: {
birthdaydate: 'L2023-02-21',
},
type: 'DATE',
showLunarClass: 'MIX',
showBackYears: 2,
showLunarIcon: true,
showLunarControl: true,
width1: '300',
format: 'YYYY/MM/DD',
placeholder: '请选择日期',
rangeSeparator: '-',
disabled: false,
editable: true,
clearable: true,
pickerOptions: {
disabledDate(time) {
// return time.getTime() < Date.now() - 8.64e7;//这行代码是限制当前日期之前不可选择,如果注释掉,则不做限制
}
},
}
}
methods
中方法的定义:
methods:{
onDateChange(val) {
this.form.birthdaydate = val;
this.$forceUpdate();
},
}
css
中样式代码如下:
::v-deep.icon-richeng:before {
content: '>';
}
/deep/.full-jcalendar__body {
height: 240px !important;
}
/deep/.full-jcalendar .input__inner:focus {
border-color: #f90;
}
.full-jcalendar .input__inner:hover {
border-color: #ffcb7c;
}
/deep/.full-jcalendar__body .data-list li:hover {
background-color: #f90;
}
/deep/.full-jcalendar-header {
height: 30px;
background: #ffeacb;
}
/deep/.full-jcalendar__body {
border: 1px solid #ebebeb;
}
/deep/.full-jcalendar-header label {
display: none !important;
}
/deep/.full-jcalendar .input__inner,
/deep/input::input-placeholder {
font-size: 13px !important;
color: #999 !important;
}
/deep/.full-jcalendar__body .day-cell.select,
/deep/.full-jcalendar__body .data-list li.select-year {
background-color: #f90;
}
/deep/.day-cell.today.select .number.is-today.is-empty {
color: #fff !important;
}
/deep/.day-cell.today .number.is-today.is-empty {
color: #f90 !important;
}
/deep/.full-jcalendar__body .day-cell.today:before,
/deep/.full-jcalendar__body .data-list li.curr-year::before {
border-top: 0.5em solid #f90 !important;
}
/deep/.full-jcalendar-header .title-year:hover,
/deep/.full-jcalendar-header .title-month:hover {
color: #f90 !important;
}
/deep/.full-jcalendar__main {
width: 100% !important;
}
/deep/.iconfont.icon-richeng::before {
content: '' !important;
}
/deep/.icon.iconfont.icon-xiangyoujiantou::before {
content: '>' !important;
}
/deep/.icon.iconfont.icon-xiangzuojiantou::before {
content: '<' !important;
}
/deep/.day-number > .lunar {
font-size: 11px !important;
}
/deep/.day-cell {
line-height: 18px !important;
}
4.数据的回显
如果要回显农历数据,则返回的格式需要是L2023-02-21
,如果返回的格式是阳历数据,则返回的格式是2023-02-21