dateDays.vue 3.4 KB
<template>
<div class="datepicker-days vue-show-imo">
<table class=" table-condensed">
	<thead>
	<tr>
	<th class="prev" @click="prev">
		<i class="fa fa-chevron-left fi-arrow-left"></i>
	</th>
	<th colspan="5" class="date-switch" @click="tomonth">{{month}} {{year}}</th>
	<th class="next" @click="next">
		<i class="fa fa-chevron-right fi-arrow-right"></i>
	</th>
	</tr>
	<tr>
		<th class="dow" v-for="week in weeks">{{week}}</th>
	</tr>
	</thead>
	<tbody>
		<tr v-for="week in calendar">
			<td :class="{today: day.date.toDateString() === new Date().toDateString(), selected: day.day === this.currentDay, old: isnot(day,$index)}" v-for="day in week" track-by="$index" v-text="day.day === 32 ? '' : day.day" @click="selectDate(day,$index)"></td>
		</tr>
	</tbody>
</table>
</div>
</template>
<script>
	export default {
		props:["dateTime","mo","state","end"],
		data(){
			return {
				// date: new Date(this.dateText),
				weeks: ['日', '一', '二', '三', '四', '五', '六'],
				months:['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
				currentDay: ""
			}
		},
		computed: {

			month() {
				return this.months[this.dateTime.month]
			},
			year() {
				return this.dateTime.year
			},
			calendar() {
				let result = []
				let calendar = []
				let date = this.dateTime
				let year = this.year
				let month = this.month
				let first = new Date(date.year, date.month, 1).getDay()
				let days = new Date(date.year, date.month + 1, 0).getDate()
				for (let i = 0; i < days; i++, first++) {
					calendar[first] = i + 1
				}
				calendar = calendar.filter((v) => v)
				// 切割
				first -= days
				result[0] = []
				let currentDay = first
				for (let i = 0; i < 7 - currentDay; i++, first++) {
					result[0][first] = {
						day: i + 1,
						date: new Date(`${year}/${month}/${i + 1}`)
					}
				}
				while(currentDay > -1) {
					result[0][currentDay - 1] = {
						day: 32,
						date: new Date(`${year}/${month}/32`)
					}
					currentDay--
				}
				let count = Math.ceil((days - (7 - first)) / 7)
				let lastDay = result[0].slice(-1)[0].day
				for (let i = 1; i <= count; i++) {
					result[i] = []
					for (let ii = 0; ii < 7; ii++) {
						result[i][ii] = {
							day: calendar[lastDay + ii],
							date: new Date(`${year}/${month}/${calendar[lastDay + ii]}`)
						}
					}
					lastDay += 7
				}
				//console.dir(result)
				return result
			}
		},
		methods: {
			prev() {
				this.currentDay = ""
				this.dateTime.month-=1
			},
			next() {
				this.currentDay = ""
				this.dateTime.month+=1
			},
			selectDate(value,index) {
				if(this.isnot(value,index)){
					return
				}
				this.dateTime.date=value.day
				this.dateTime.__now__=Date.now()
				this.mo=''
			},
			tomonth(){
				this.mo="month"
			},
			isnot(day,index){
				if(day.day&&day.day!==32){
					if(this.state=="week"){
						if(day.day==1){
							return false;
						}
						var a=new Date(day.date.getFullYear(),day.date.getMonth(),day.date.getDate());
						
						a.setDate(a.getDate()+(6-a.getDay()));
						if(a>new Date()){
							return true;
						}
						if(index!==0){
							return true;
						}
						// return ! day.day || day.day === 32||index!==0;//||day.day!==1;
					}
					if(this.end){
						var _a=this.end.split('-');
						if(day.date>new Date(_a[0],_a[1],_a[2])){
							return true;
						}
					}
					return false;
				}
				return true;
			}
		},
		ready() {
			this.currentDay = this.dateTime.date
		}
	}
</script>