@@ -13,7 +13,6 @@ import {DateAdapter} from '@angular/material/core';
13
13
import { MatCalendar } from '@angular/material' ;
14
14
import { ThemePalette } from '@angular/material/core' ;
15
15
16
-
17
16
@Component ( {
18
17
moduleId : module . id ,
19
18
selector : 'datepicker-demo' ,
@@ -50,10 +49,41 @@ export class DatepickerDemo {
50
49
// Custom header component for datepicker
51
50
@Component ( {
52
51
selector : 'custom-header' ,
53
- template : 'custom header'
52
+ template : `
53
+ <div>
54
+ <button (click)="previousClicked('year')"><<</button>
55
+ <button (click)="previousClicked('month')"><</button>
56
+ {{periodLabel}}
57
+ <button (click)="nextClicked('month')">></button>
58
+ <button (click)="nextClicked('year')">>></button>
59
+ </div>
60
+ `
54
61
} )
55
62
export class CustomHeader {
56
63
constructor ( @Host ( ) public calendar : MatCalendar < any > ,
57
- public adapter : DateAdapter < any > ) {
64
+ private _dateAdapter : DateAdapter < any > ) { }
65
+
66
+ get periodLabel ( ) {
67
+ let year = this . _dateAdapter . getYearName ( this . calendar . activeDate ) ;
68
+ let month = ( this . _dateAdapter . getMonth ( this . calendar . activeDate ) + 1 ) ;
69
+ return `${ month } /${ year } ` ;
70
+ }
71
+
72
+ previousClicked ( mode : 'month' | 'year' ) {
73
+ this . calendar . activeDate = mode == 'month' ?
74
+ this . _dateAdapter . addCalendarMonths ( this . calendar . activeDate , - 1 ) :
75
+ this . _dateAdapter . addCalendarYears (
76
+ this . calendar . activeDate ,
77
+ - 1
78
+ ) ;
79
+ }
80
+
81
+ nextClicked ( mode : 'month' | 'year' ) {
82
+ this . calendar . activeDate = mode == 'month' ?
83
+ this . _dateAdapter . addCalendarMonths ( this . calendar . activeDate , 1 ) :
84
+ this . _dateAdapter . addCalendarYears (
85
+ this . calendar . activeDate ,
86
+ 1
87
+ ) ;
58
88
}
59
89
}
0 commit comments