2
0
mirror of https://github.com/tenrok/vue2-datepicker.git synced 2026-06-23 07:00:36 +03:00

Add disabledDays prop to accept array of dates to disable

This commit is contained in:
Aaron Rubin
2017-06-10 22:32:47 -04:00
parent 5eb51fd12e
commit a13290a35f
3 changed files with 24 additions and 6 deletions
+2 -1
View File
@@ -43,8 +43,9 @@ export default {
|-----------------|---------------|-------------|---------------------------------------| |-----------------|---------------|-------------|---------------------------------------|
| range | Boolean | false | if true, the type is daterange | | range | Boolean | false | if true, the type is daterange |
| format | String | yyyy-MM-dd | Date formatting string | | format | String | yyyy-MM-dd | Date formatting string |
| lang | String | zh | Translation (en/zh/es) | | lang | String | zh | Translation (en/zh/es) |
| placeholder | String | | input placeholder text | | placeholder | String | | input placeholder text |
| width | String/Number | 210 | input size | | width | String/Number | 210 | input size |
| disabledDays | Array | [] | Days in YYYY-MM-DD format to disable |
+17 -1
View File
@@ -40,7 +40,8 @@ export default {
startAt: null, startAt: null,
endAt: null, endAt: null,
value: null, value: null,
show: Boolean show: Boolean,
disabledDays: Array
}, },
data () { data () {
const translation = this.$parent.translation const translation = this.$parent.translation
@@ -91,12 +92,14 @@ export default {
const date = new Date(time.getFullYear(), time.getMonth(), day) const date = new Date(time.getFullYear(), time.getMonth(), day)
return { return {
title: date.toLocaleDateString(), title: date.toLocaleDateString(),
iso: cal.isoDate(date),
date, date,
day, day,
classes classes
} }
}) })
} }
var cal = this;
const time = new Date(this.now) const time = new Date(this.now)
time.setDate(0) // 把时间切换到上个月最后一天 time.setDate(0) // 把时间切换到上个月最后一天
const lastMonthLength = time.getDay() + 1 // time.getDay() 0是星期天, 1是星期一 ... const lastMonthLength = time.getDay() + 1 // time.getDay() 0是星期天, 1是星期一 ...
@@ -121,6 +124,15 @@ export default {
} }
this.dates = result this.dates = result
}, },
isoDate(date) {
function doubleDigits(num) {
if ( parseInt(num) < 10 ) {
return '0'+num;
}
return num;
}
return date.getFullYear()+'-'+doubleDigits((date.getMonth()+1))+'-'+doubleDigits(date.getDate());
},
getClasses (cell) { getClasses (cell) {
const classes = [] const classes = []
const cellTime = cell.date.getTime() const cellTime = cell.date.getTime()
@@ -131,6 +143,10 @@ export default {
classes.push(cell.classes) classes.push(cell.classes)
if ( typeof this.disabledDays.find(function(disabledDate) { return disabledDate === cell.iso } ) !== 'undefined' ) {
classes.push('disabled');
}
if (cellTime === today) { if (cellTime === today) {
classes.push('today') classes.push('today')
} }
+5 -4
View File
@@ -20,14 +20,14 @@
ref="calendar" ref="calendar"
v-show="showPopup"> v-show="showPopup">
<template v-if="!range"> <template v-if="!range">
<calendar-panel @select="showPopup = false" v-model="currentValue" :show="showPopup"></calendar-panel> <calendar-panel @select="showPopup = false" v-model="currentValue" :show="showPopup" :disabledDays="disabledDays"></calendar-panel>
</template> </template>
<template v-else> <template v-else>
<div class="datepicker-top"> <div class="datepicker-top">
<span v-for="range in ranges" @click="selectRange(range)">{{range.text}}</span> <span v-for="range in ranges" @click="selectRange(range)">{{range.text}}</span>
</div> </div>
<calendar-panel style="width:50%;box-shadow:1px 0 rgba(0, 0, 0, .1)" v-model="currentValue[0]" :end-at="currentValue[1]" :show="showPopup"></calendar-panel> <calendar-panel style="width:50%;box-shadow:1px 0 rgba(0, 0, 0, .1)" v-model="currentValue[0]" :end-at="currentValue[1]" :show="showPopup" :disabledDays="disabledDays"></calendar-panel>
<calendar-panel style="width:50%;" v-model="currentValue[1]" :start-at="currentValue[0]" :show="showPopup"></calendar-panel> <calendar-panel style="width:50%;" v-model="currentValue[1]" :start-at="currentValue[0]" :show="showPopup" :disabledDays="disabledDays"></calendar-panel>
</template> </template>
</div> </div>
</div> </div>
@@ -57,7 +57,8 @@ export default {
type: String, type: String,
default: 'zh' default: 'zh'
}, },
value: null value: null,
disabledDays: Array
}, },
data () { data () {
return { return {