This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
forked from ng-bootstrap/ng-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demo(datepicker): add custom date parser/formatter demo (ng-bootstrap…
…#3451) Closes ng-bootstrap#3417
- Loading branch information
1 parent
0e05dec
commit c8c6be9
Showing
4 changed files
with
94 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 75 additions & 5 deletions
80
demo/src/app/components/datepicker/demos/adapter/datepicker-adapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,90 @@ | ||
import {Component, Injectable} from '@angular/core'; | ||
import {NgbDateAdapter, NgbDateStruct, NgbDateNativeAdapter} from '@ng-bootstrap/ng-bootstrap'; | ||
import { | ||
NgbCalendar, | ||
NgbDateAdapter, | ||
NgbDateStruct, | ||
NgbDateParserFormatter | ||
} from '@ng-bootstrap/ng-bootstrap'; | ||
|
||
/** | ||
* This Service handles how the date is represented in scripts i.e. ngModel. | ||
*/ | ||
@Injectable() | ||
export class CustomAdapter extends NgbDateAdapter<string> { | ||
|
||
readonly DELIMITER = '-'; | ||
|
||
fromModel(value: string): NgbDateStruct { | ||
let result: NgbDateStruct = null; | ||
if (value) { | ||
let date = value.split(this.DELIMITER); | ||
result = { | ||
day : parseInt(date[0], 10), | ||
month : parseInt(date[1], 10), | ||
year : parseInt(date[2], 10) | ||
}; | ||
} | ||
return result; | ||
} | ||
|
||
toModel(date: NgbDateStruct): string { | ||
let result: string = null; | ||
if (date) { | ||
result = date.day + this.DELIMITER + date.month + this.DELIMITER + date.year; | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
/** | ||
* This Service handles how the date is rendered and parsed from keyboard i.e. in the bound input field. | ||
*/ | ||
@Injectable() | ||
export class CustomDateParserFormatter extends NgbDateParserFormatter { | ||
|
||
readonly DELIMITER = '/'; | ||
|
||
parse(value: string): NgbDateStruct { | ||
let result: NgbDateStruct = null; | ||
if (value) { | ||
let date = value.split(this.DELIMITER); | ||
result = { | ||
day : parseInt(date[0], 10), | ||
month : parseInt(date[1], 10), | ||
year : parseInt(date[2], 10) | ||
}; | ||
} | ||
return result; | ||
} | ||
|
||
format(date: NgbDateStruct): string { | ||
let result: string = null; | ||
if (date) { | ||
result = date.day + this.DELIMITER + date.month + this.DELIMITER + date.year; | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
@Component({ | ||
selector: 'ngbd-datepicker-adapter', | ||
templateUrl: './datepicker-adapter.html', | ||
|
||
// NOTE: For this example we are only providing current component, but probably | ||
// NOTE: you will want to provide your main App Module | ||
providers: [{provide: NgbDateAdapter, useClass: NgbDateNativeAdapter}] | ||
providers: [ | ||
{provide: NgbDateAdapter, useClass: CustomAdapter}, | ||
{provide: NgbDateParserFormatter, useClass: CustomDateParserFormatter} | ||
] | ||
}) | ||
export class NgbdDatepickerAdapter { | ||
|
||
model1: Date; | ||
model2: Date; | ||
model1: string; | ||
model2: string; | ||
|
||
constructor(private ngbCalendar: NgbCalendar, private dateAdapter: NgbDateAdapter<string>) {} | ||
|
||
get today() { | ||
return new Date(); | ||
return this.dateAdapter.toModel(this.ngbCalendar.getToday()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters