首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >直接使用mat-datepicker而无需输入

直接使用mat-datepicker而无需输入
EN

Stack Overflow用户
提问于 2018-02-02 20:58:28
回答 3查看 20.7K关注 0票数 30

我想把datepicker放在一个永久的侧边栏中,总是可见的,不依赖于输入,这是可能的吗?我认为只要将组件放入并添加opened = true,就可以使框中的datepicker始终可见。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-20 07:08:58

事实证明,这非常简单,像往常一样导入MatDatePickerModule,然后只需使用mat-calendar选择器

代码语言:javascript
复制
<mat-calendar></mat-calendar>

为了通过typescript连接到选择

代码语言:javascript
复制
  @ViewChild(MatCalendar) _datePicker: MatCalendar<Date>

ngOnInit() {
    this._datePicker.selectedChange.subscribe(x => {
      console.log(x);
    });
  }
票数 38
EN

Stack Overflow用户

发布于 2018-03-19 16:23:12

您也可以尝试使用css ex隐藏:

代码语言:javascript
复制
<mat-form-field style="width:1px;visibility:hidden;">
  <input matInput [matDatepicker]="picker" >
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>
<button mat-button (click)="picker.open()" ></button>

使用css隐藏的优点是日期选择器是相对于隐藏的表单字段定位的

票数 18
EN

Stack Overflow用户

发布于 2020-01-23 00:16:45

我找到的唯一有效的方法是大而有效:

(partial credits)

Html

代码语言:javascript
复制
  <mat-form-field id="dashboardComponent">
    <input matInput
      [matDatepicker]="matDatepicker"
      [formControl]="dateFormControl"
      (dateChange)="onDateChanged('change', $event)"
      [max]="datePickerMaxDate"
      >
    <mat-datepicker-toggle matSuffix [for]="matDatepicker"></mat-datepicker-toggle>
    <mat-datepicker #matDatepicker></mat-datepicker>
  </mat-form-field>

component.ts

代码语言:javascript
复制
@Component({
    ....
    encapsulation: ViewEncapsulation.None
})

export class MyComponent {

    // Date Picker
    public currentDate: Date = new Date() // set default here
    public dateFormControl: FormControl = new FormControl(this.currentDate)

css

代码语言:javascript
复制
/* DATE PICKER */
mat-form-field {
    margin: 0 0.5em; //adapt me to your needs
    width: 0.8em; //adapt me to your needs
    font-weight: normal;
}


#dashboardComponent {
    .mat-form-field-underline {
        background-color: transparent;
        height: 0;
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48583243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档