update DatePicker

update DatePicker
This commit is contained in:
梁灏 2016-12-19 21:59:36 +08:00
parent 0a5c5f4132
commit 3732707ede

View file

@ -43,8 +43,8 @@
<year-table <year-table
v-ref:left-year-table v-ref:left-year-table
v-show="leftCurrentView === 'year'" v-show="leftCurrentView === 'year'"
:year="leftYear" :year="leftTableYear"
:date="date" :date="leftTableDate"
selection-mode="range" selection-mode="range"
:disabled-date="disabledDate" :disabled-date="disabledDate"
@on-pick="handleLeftYearPick"></year-table> @on-pick="handleLeftYearPick"></year-table>
@ -52,7 +52,7 @@
v-ref:left-month-table v-ref:left-month-table
v-show="leftCurrentView === 'month'" v-show="leftCurrentView === 'month'"
:month="leftMonth" :month="leftMonth"
:date="date" :date="leftTableDate"
selection-mode="range" selection-mode="range"
:disabled-date="disabledDate" :disabled-date="disabledDate"
@on-pick="handleLeftMonthPick"></month-table> @on-pick="handleLeftMonthPick"></month-table>
@ -93,8 +93,8 @@
<year-table <year-table
v-ref:right-year-table v-ref:right-year-table
v-show="rightCurrentView === 'year'" v-show="rightCurrentView === 'year'"
:year="rightYear" :year="rightTableYear"
:date="rightDate" :date="rightTableDate"
selection-mode="range" selection-mode="range"
:disabled-date="disabledDate" :disabled-date="disabledDate"
@on-pick="handleRightYearPick"></year-table> @on-pick="handleRightYearPick"></year-table>
@ -102,7 +102,7 @@
v-ref:right-month-table v-ref:right-month-table
v-show="rightCurrentView === 'month'" v-show="rightCurrentView === 'month'"
:month="rightMonth" :month="rightMonth"
:date="rightDate" :date="rightTableDate"
selection-mode="range" selection-mode="range"
:disabled-date="disabledDate" :disabled-date="disabledDate"
@on-pick="handleRightMonthPick"></month-table> @on-pick="handleRightMonthPick"></month-table>
@ -142,7 +142,9 @@
disabledDate: '', disabledDate: '',
leftCurrentView: 'date', leftCurrentView: 'date',
rightCurrentView: 'date', rightCurrentView: 'date',
selectionMode: 'range' selectionMode: 'range',
leftTableYear: null,
rightTableYear: null
} }
}, },
computed: { computed: {
@ -155,37 +157,57 @@
} }
] ]
}, },
leftYear() { leftYear () {
return this.date.getFullYear(); return this.date.getFullYear();
}, },
leftTableDate () {
if (this.leftCurrentView === 'year' || this.leftCurrentView === 'month') {
return new Date(this.leftTableYear);
} else {
return this.date;
}
},
leftYearLabel () { leftYearLabel () {
const year = this.leftYear;
if (!year) return '';
if (this.leftCurrentView === 'year') { if (this.leftCurrentView === 'year') {
const year = this.leftTableYear;
if (!year) return '';
const startYear = Math.floor(year / 10) * 10; const startYear = Math.floor(year / 10) * 10;
return `${startYear}年 - ${startYear + 9}`; return `${startYear}年 - ${startYear + 9}`;
} else {
const year = this.leftCurrentView === 'month' ? this.leftTableYear : this.leftYear;
if (!year) return '';
return `${year}`;
} }
return `${year}`;
}, },
leftMonth() { leftMonth () {
return this.date.getMonth(); return this.date.getMonth();
}, },
rightYear() { rightYear () {
return this.rightDate.getFullYear(); return this.rightDate.getFullYear();
}, },
rightTableDate () {
if (this.rightCurrentView === 'year' || this.rightCurrentView === 'month') {
return new Date(this.rightTableYear);
} else {
return this.date;
}
},
rightYearLabel () { rightYearLabel () {
const year = this.rightYear;
if (!year) return '';
if (this.rightCurrentView === 'year') { if (this.rightCurrentView === 'year') {
const year = this.rightTableYear;
if (!year) return '';
const startYear = Math.floor(year / 10) * 10; const startYear = Math.floor(year / 10) * 10;
return `${startYear}年 - ${startYear + 9}`; return `${startYear}年 - ${startYear + 9}`;
} else {
const year = this.rightCurrentView === 'month' ? this.rightTableYear : this.rightYear;
if (!year) return '';
return `${year}`;
} }
return `${year}`;
}, },
rightMonth() { rightMonth () {
return this.rightDate.getMonth(); return this.rightDate.getMonth();
}, },
rightDate() { rightDate () {
const newDate = new Date(this.date); const newDate = new Date(this.date);
const month = newDate.getMonth(); const month = newDate.getMonth();
newDate.setDate(1); newDate.setDate(1);
@ -248,8 +270,7 @@
this.handleYearPick(year, close, 'right'); this.handleYearPick(year, close, 'right');
}, },
handleYearPick (year, close, direction) { handleYearPick (year, close, direction) {
this.date.setFullYear(year); this[`${direction}TableYear`] = year;
this.resetDate();
if (!close) return; if (!close) return;
this[`${direction}CurrentView`] = 'month'; this[`${direction}CurrentView`] = 'month';
@ -261,12 +282,24 @@
this.handleMonthPick(month, 'right'); this.handleMonthPick(month, 'right');
}, },
handleMonthPick (month, direction) { handleMonthPick (month, direction) {
let year = this[`${direction}TableYear`];
if (direction === 'right') {
if (month === 0) {
month = 11;
year--;
} else {
month--;
}
}
this.date.setYear(year);
this.date.setMonth(month); this.date.setMonth(month);
this[`${direction}CurrentView`] = 'date'; this[`${direction}CurrentView`] = 'date';
this.resetDate(); this.resetDate();
}, },
showYearPicker (direction) { showYearPicker (direction) {
this[`${direction}CurrentView`] = 'year'; this[`${direction}CurrentView`] = 'year';
this[`${direction}TableYear`] = this[`${direction}Year`];
}, },
showMonthPicker (direction) { showMonthPicker (direction) {
this[`${direction}CurrentView`] = 'month'; this[`${direction}CurrentView`] = 'month';