【问题标题】:Is there a way to customize angular date picker?有没有办法自定义角度日期选择器?
【发布时间】:2020-02-13 06:55:58
【问题描述】:

我需要帮助来自定义角度材料日期选择器,即https://material.angular.io/components/datepicker/overview。 一旦我们单击日期,日历不应关闭。我应该可以有 2 个按钮,一个是“取消”,另一个是“保存”。单击保存按钮后,应关闭日历。

【问题讨论】:

    标签: angular angular-material angular6


    【解决方案1】:

    我不知道这是否是一个更好的解决方案,但基于这个SO answer,我认为解决方案是使用带有日历的菜单。

    由于我们需要输入,因此我们至少需要两个函数,一个将输入的值解析为日期对象,另一个在我们有日期对象时格式化输入的值。

    因为我想要这个“特殊”的日期选择器可以用多种方式格式化日期,YYYY-MM-DD 和 DD/MM/YYYY 我需要树变量

      placeHolder:string="DD/MM/YYYY"
      separator:string;
      order:number[]=[];
    

    一个函数给了我们“分隔符和顺序”的值

      init(placeholder:string)
      {
         this.separator=placeholder.replace(/[YMD]/g,'').substr(0,1)
         const parts=placeholder.replace(/[.\/]/g,'-').split('-')
         this.order[0]=parts.indexOf('YYYY')
         this.order[1]=parts.indexOf('MM')
         this.order[2]=parts.indexOf('DD')
      }
    

    所以,我们的函数解析和格式变得像

    format(date:any)
      {
        if (!date)
          return null;
        const parts=[''+date.getFullYear(),
                     ("00" + (date.getMonth() + 1)).slice(-2),
                     ("00" + date.getDate()).slice(-2)]
        return parts[this.order[0]]+this.separator+
               parts[this.order[1]]+this.separator+
               parts[this.order[2]]
      }
      parse(value:string)
      {
       const parts=value?value.replace(/[.\/]/g,'-').split('-'):[]
       const date:any=(parts.length==3)?
            new Date(parts[this.order[0]]+'-'+parts[this.order[1]]+'-'+parts[this.order[2]]):
            null
       return date && date.getTime && date.getTime()?date:null
      }
    

    一个辅助函数让我们(模糊)重新格式化值

      tryFormat(value:string)
      {
        const date=this.parse(value)
        this.date=date?this.format(date):value
      }
    

    “菜单”打开时的一个功能,允许更改 ViewChild 中日历的活动日期

    @ViewChild('calendar',{static:false}) calendar:any
    onOpen()
      {
        if (this.date)
        {
          const date=this.parse(this.date);
          if (date)
          {
            this.calendar.activeDate=date;
          }
        }
      }
    

    最后,.html 是这样的

    <mat-form-field class="example-full-width">
        <mat-label>Date</mat-label>
        <input matInput placeholder="{{placeHolder}}" [(ngModel)]="date" (blur)="tryFormat(date)" >
        <button matSuffix mat-icon-button [matMenuTriggerFor]="appMenu" (menuOpened)="onOpen()">
      <mat-icon>calendar_today</mat-icon>
    </button>
      </mat-form-field>
    
    <mat-menu #appMenu="matMenu" class="drop-calendar" >
        <div (click)="$event.stopPropagation()">
            <mat-calendar #calendar 
              (selectedChange)="date=format($event)" 
               [selected]="parse(date)">
            </mat-calendar>
        </div>
    </mat-menu>
    

    完整代码在stackblitz

    【讨论】:

    • 非常感谢!这是我正在寻找的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2021-12-15
    • 2016-02-12
    • 2023-03-13
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多