用于选择日期和日期范围的Vue组件。
文件列表(压缩包大小 851.23K)
免费
概述
用于选择日期和日期范围的Vue组件。使用Moment.js进行日期操作。支持中国农历。
npm install vue-date-range
<calendar class="calendar"
:show-lunar="true"
:first-day-of-week="1"
:disable-days-before-today="disableDaysBeforeToday"
:sync-date.sync="date"
:lang="lang" @change="onChange"></calendar>
...
import {Calendar} from 'vue-date-range';
export default {
components: {
Calendar
},
data() {
return {
disableDaysBeforeToday: true,
lang: 'zh',
date: moment()
};
},
methods: {
onChange(date) {
this.date = date;
}
}
}
<daterange class="calendar" :sync-range.sync="range" :lang="lang" @change="onChange"></daterange>
...
import {DateRange} from 'vue-date-range';
export default {
components: {
DateRange
},
data() {
return {
lang: 'en',
range: {
startDate: moment(),
endDate: moment().add(7, 'days')
}
};
},
methods: {
onChange(range) {
this.range = range;
},
setRange (p) {
if (typeof p === 'number') {
console.log(p)
this.range = {
startDate: moment().add(p, 'days'),
endDate: moment()
}
}
},
}
}
从dist /下载vue-date-range.min.js并导入网页。例子:
...
<div id="calendarLunar" class="calendar-wrapper">
<span>{{date.format('YYYY-MM-DD')}}</span>
<calendar class="calendar"
:show-lunar="true"
:first-day-of-week="1"
:disable-days-before-today="disableDaysBeforeToday"
:sync-date="date"
:lang="lang" @change="onChange"></calendar>
</div>
...
<div id="range" class="calendar-wrapper">
<span>{{range.startDate.format('YYYY-MM-DD')}}</span>~<span>{{range.endDate.format('YYYY-MM-DD')}}</span>
<daterange class="calendar" :sync-range="range" :lang="lang" @change="onChange"></daterange>
<button @click.stop.prevent="setRange(-7)">Last 7 days</button>
<button @click.stop.prevent="setRange(-30)">Last 1 month</button>
</div>
...
<script src="//cdn.bootcss.com/moment.js/2.17.1/moment.min.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="../dist/vue-date-range.min.js"></script>
<script>
new Vue({
el: '#calendarLunar',
components: {
'calendar':daterange.Calendar
},
data() {
return {
disableDaysBeforeToday: true,
lang: 'zh',
date: moment()
};
},
methods: {
onChange(date) {
this.date = date;
}
}
});
new Vue({
el: '#range',
components: {
'daterange':daterange.DateRange
},
data() {
return {
lang: 'en',
range: {
startDate: moment(),
endDate: moment().add(7, 'days')
}
};
},
methods: {
onChange(range) {
this.range = range;
},
setRange (p) {
if (typeof p === 'number') {
console.log(p)
this.range = {
startDate: moment().add(p, 'days'),
endDate: moment()
}
}
},
}
});
</script>
对于Vue2.3.0 +,我们可以使用.sync修饰符:
<calendar :sync-date.sync="date"></calendar>
<date-range :sync-range.sync="range"></date-range>
我们还可以使用v-model修饰符(可以在2.2.0+中进行配置):
< Calendar v-model = “ date” > < /日历>
< date - range v - model = “ range” > < / date-range >
return true
}
return false
}
...// add 'today, important' class to today
dayClassFunc: function (dayMoment) {
if (dayMoment.format('YYYY-MM-DD') === moment().format('YYYY-MM-DD')) {
return ['today', 'important']
}
}
< calendar :sync-date.sync =“ date ” > </ calendar >
range: {startDate: moment(), endDate: moment().add(7, 'days')}
这个组件是建立在之上的Calendar,因此它Calendar除了具有所有其他道具外,sync-date 还具有其特定的道具:
< date-range :sync-range.sync =“ range ” > </ date-range >
< span title =“重阳” class =“ v-date-cell v-date-selected ” >
< div class =“ v-date-cell-text ” >
< p class =“ v-date-solar ” > 3 < / p >
< p class =“ v-date-lunar ” >十八</ p >
</ div >
</ span >
跨度标签将具有不同的类(v-date-selected,v-date-passive,v-date-in-range,v-date-start-day,v-date-end-day)根据所选择的日期。
可以使用这些类设置自定义样式。例如:.v-date-day-cell.v-date-start-day {
border-bottom-left-radius:50%;
border-top-left-radius:50%;
背景色:透明;
}
.v-date-day-cell.v-date-end-day {
border-bottom-right-radius:50%;
右上边界半径:50%;
背景色:透明;
}
.v-date-day-cell.v-date-in-range {
background-color:orange;
}
如果遇到文件不能下载或其他产品问题,请添加管理员微信:ligongku001,并备注:产品反馈
评论(0)