只有月和日的日期选择器(隐藏年份)
Datepicker with only Month and Day (Hide year)
我找不到任何在 ngx-bootstrap 日期选择器上隐藏年份的记录方法。我尝试了 dateInputFormat: 'MMDD'
,但年份仍然显示在日期选择器中 header。
文档 link:
https://valor-software.com/ngx-bootstrap/#/datepicker
我想展示类似的东西:
好吧,事情是使用 encapsulation:ViewEncapsulation.None 和一个 class like
.bs-datepicker-head button:nth-child(3){
display:none!important;
}
ViewEncapsultaion.None 的问题是您所有的 .css 组件都会影响所有应用程序。所以,最好写一个 .css like
.custom .bs-datepicker-head button:nth-child(3){
display:none!important;
}
因此,如果我们使用 ViewEncapsultaion.None,只会影响具有 class "custom" 的日期选择器。要创建具有 class 自定义的 datePicker,您需要使用 [bsConfig].
我写代码
我们的.html
<div class="content">
<h4>datepicker should display current date:</h4>
<div class="row">
<div class="col-xs-12 col-12 col-md-4 form-group">
<input type="text"
class="form-control"
[minDate]="minDate"
[maxDate]="maxDate"
#dp="bsDatepicker" [bsConfig]="bsConfig"
bsDatepicker [(bsValue)]="myDateValue">
</div>
</div>
</div>
还有我们的组件
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None
})
export class AppComponent implements OnInit {
myDateValue: Date;
bsConfig: Partial<BsDatepickerConfig>;
ngOnInit() {
this.myDateValue = new Date();
//see that our class was "theme-green custom"
this.bsConfig = Object.assign({}, { containerClass: 'theme-green custom' });
}
}
您可以在stackblitz
中看到
您可以使用自定义 NgbDatepickerI18n 并将方法 getYearNumerals
覆盖为 return 空字符串。事实上,正是 NgbDatePicker 使用这种方法在导航中呈现年份 header.
这是一个简单的例子:
@Injectable({
providedIn: 'root'
})
class CustomDateI18n extends NgbDatepickerI18nDefault {
getYearNumerals(year: number): string {
return '';
}
}
@Component({
selector: 'app-component',
templateUrl: './my-component.component.html',
providers: [
{
provide: NgbDatepickerI18n,
useClass: CustomDateI18n
}
]
})
我找不到任何在 ngx-bootstrap 日期选择器上隐藏年份的记录方法。我尝试了 dateInputFormat: 'MMDD'
,但年份仍然显示在日期选择器中 header。
文档 link:
https://valor-software.com/ngx-bootstrap/#/datepicker
我想展示类似的东西:
好吧,事情是使用 encapsulation:ViewEncapsulation.None 和一个 class like
.bs-datepicker-head button:nth-child(3){
display:none!important;
}
ViewEncapsultaion.None 的问题是您所有的 .css 组件都会影响所有应用程序。所以,最好写一个 .css like
.custom .bs-datepicker-head button:nth-child(3){
display:none!important;
}
因此,如果我们使用 ViewEncapsultaion.None,只会影响具有 class "custom" 的日期选择器。要创建具有 class 自定义的 datePicker,您需要使用 [bsConfig].
我写代码
我们的.html
<div class="content">
<h4>datepicker should display current date:</h4>
<div class="row">
<div class="col-xs-12 col-12 col-md-4 form-group">
<input type="text"
class="form-control"
[minDate]="minDate"
[maxDate]="maxDate"
#dp="bsDatepicker" [bsConfig]="bsConfig"
bsDatepicker [(bsValue)]="myDateValue">
</div>
</div>
</div>
还有我们的组件
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
encapsulation: ViewEncapsulation.None
})
export class AppComponent implements OnInit {
myDateValue: Date;
bsConfig: Partial<BsDatepickerConfig>;
ngOnInit() {
this.myDateValue = new Date();
//see that our class was "theme-green custom"
this.bsConfig = Object.assign({}, { containerClass: 'theme-green custom' });
}
}
您可以在stackblitz
中看到您可以使用自定义 NgbDatepickerI18n 并将方法 getYearNumerals
覆盖为 return 空字符串。事实上,正是 NgbDatePicker 使用这种方法在导航中呈现年份 header.
这是一个简单的例子:
@Injectable({
providedIn: 'root'
})
class CustomDateI18n extends NgbDatepickerI18nDefault {
getYearNumerals(year: number): string {
return '';
}
}
@Component({
selector: 'app-component',
templateUrl: './my-component.component.html',
providers: [
{
provide: NgbDatepickerI18n,
useClass: CustomDateI18n
}
]
})