Programmatic Value Control
Values can change in a couple of different ways: through defaults, interacting with the UI or programmatically. The date and time picker defaults to now, which can be easily overridden with the defaultSelection option. The values are set by interacting with the component and making a selection or it can be done programmatically by updating the bound value. Use the buttons option for showing/hiding set, cancel or add custom buttons.
Demo
The calendar defaults to today if not overridden. You can however manually set a default value through the
defaultSelection option.
html<mbsc-datepicker
[controls]="['calendar']">
</mbsc-datepicker>
html<mbsc-datepicker
[controls]="['calendar']"
[defaultSelection]="defaultSelection">
</mbsc-datepicker>
tsdefaultSelection = new Date(2020, 11, 24);
While the selected value can be changed by interacting with the calendar, you can set the model value to change it programmatically.
htmlhtml<mbsc-button (click)="setValue()">02-01-2020</mbsc-button>
<mbsc-button (click)="setToday()">Today</mbsc-button>
<mbsc-datepicker
[controls]="['calendar']"
[(ngModel)]="customVal">
</mbsc-datepicker>
tssetValue() {
this.customVal = new Date(2020, 0, 2);
}
setToday() {
this.customVal = new Date();
}
The button API provides shortcuts to value selection and confirmation. Add 'Now' or custom button to the component or entirely remove the 'Set' button to activate auto-set, which automatically closes the dialog if a date is selected.
import { CommonModule } from '@angular/common';
import { Component, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MbscDatepicker, MbscModule, setOptions } from '@mobiscroll/angular';
setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
});
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
standalone: true,
imports: [CommonModule, FormsModule, MbscModule],
})
export class AppComponent {
@ViewChild('nowInst')
nowInst!: MbscDatepicker;
@ViewChild('customInst')
customInst!: MbscDatepicker;
customVal: Date | undefined;
now: Date | undefined;
custom: Date | undefined;
myDefaultSelection = new Date(2020, 11, 24);
nowButtons: any = [
{
text: 'Now',
handler: () => {
this.now = new Date();
this.nowInst.close();
},
},
];
customButtons: any = [
{
text: '05 Jan 2020',
handler: () => {
this.custom = new Date(2020, 0, 5);
this.customInst.close();
},
},
'set',
'cancel',
];
autoButtons: any = ['close'];
setValue(): void {
this.customVal = new Date(2020, 0, 2);
}
setToday(): void {
this.customVal = new Date();
}
}
<mbsc-page>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Controlling the default value</div>
<mbsc-datepicker label="Default" [controls]="['calendar']" placeholder="Please select..."></mbsc-datepicker>
<mbsc-datepicker
label="Custom default"
[controls]="['calendar']"
[defaultSelection]="myDefaultSelection"
placeholder="Please select..."
></mbsc-datepicker>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Setting a custom value</div>
<div class="mbsc-button-group-block">
<mbsc-button (click)="setValue()">02-01-2020</mbsc-button>
<mbsc-button (click)="setToday()">Today</mbsc-button>
</div>
<mbsc-datepicker [controls]="['calendar']" [(ngModel)]="customVal" display="inline"></mbsc-datepicker>
</div>
<div class="mbsc-form-group">
<div class="mbsc-form-group-title">Buttons API</div>
<mbsc-datepicker
#nowInst
label="Now"
[controls]="['calendar']"
[(ngModel)]="now"
[buttons]="nowButtons"
placeholder="Please select..."
></mbsc-datepicker>
<mbsc-datepicker
#customInst
label="Custom"
[controls]="['calendar']"
[(ngModel)]="custom"
[buttons]="customButtons"
placeholder="Please select..."
>Custom</mbsc-datepicker
>
<mbsc-datepicker label="Auto set" [controls]="['calendar']" [buttons]="autoButtons" placeholder="Please select..."
>Auto set</mbsc-datepicker
>
</div>
</mbsc-page>