【问题标题】:Changing the ngbDatepicker input template更改 ngbDatepicker 输入模板
【发布时间】:2019-04-07 05:23:27
【问题描述】:

我正在开发一个使用此示例的应用程序:

https://stackblitz.com/angular/rynxmynlykl

我想要的是以不同的格式显示 选定的日期。我想要mm/dd/yyyy 而不是yyyy-mm-dd。占位符很容易更改,但我无法在文档中找到我要查找的内容 (https://ng-bootstrap.github.io/#/components/datepicker/api)。

ngModel 接受一个包含年、月和日的对象。然后 Datepicker 将其格式化为上述格式。

我找到的最接近的答案在这里,但现在已经过时了 (How to change model structure of angular powered bootstrap ngbDatepicker)。

以前有人遇到过这种情况吗?

【问题讨论】:

    标签: angular ng-bootstrap


    【解决方案1】:

    the DatePicker documentation 中所述,您可以提供NgbDateParserFormatter 的自定义版本。有关演示,请参阅 this stackblitz

    解析器/格式化程序的以下代码改编自this GitHubGist by Niels Robin-Aubertin

    import { Injectable } from "@angular/core";
    import { NgbDateParserFormatter, NgbDateStruct } from "@ng-bootstrap/ng-bootstrap";
    
    @Injectable()
    export class CustomDateParserFormatter extends NgbDateParserFormatter {
    
      parse(value: string): NgbDateStruct {
        if (value) {
          const dateParts = value.trim().split('/');
          if (dateParts.length === 1 && this.isNumber(dateParts[0])) {
            return { year: this.toInteger(dateParts[0]), month: null, day: null };
          } else if (dateParts.length === 2 && this.isNumber(dateParts[0]) && this.isNumber(dateParts[1])) {
            return { year: this.toInteger(dateParts[1]), month: this.toInteger(dateParts[0]), day: null };
          } else if (dateParts.length === 3 && this.isNumber(dateParts[0]) && this.isNumber(dateParts[1]) && this.isNumber(dateParts[2])) {
            return { year: this.toInteger(dateParts[2]), month: this.toInteger(dateParts[0]), day: this.toInteger(dateParts[1]) };
          }
        }
        return null;
      }
    
      format(date: NgbDateStruct): string {
        let stringDate: string = "";
        if (date) {
          stringDate += this.isNumber(date.month) ? this.padNumber(date.month) + "/" : "";
          stringDate += this.isNumber(date.day) ? this.padNumber(date.day) + "/" : "";
          stringDate += date.year;
        }
        return stringDate;
      }
    
      private padNumber(value: number) {
        if (this.isNumber(value)) {
          return `0${value}`.slice(-2);
        } else {
          return "";
        }
      }
    
      private isNumber(value: any): boolean {
        return !isNaN(this.toInteger(value));
      }
    
      private toInteger(value: any): number {
        return parseInt(`${value}`, 10);
      }
    }
    

    将日期解析器/格式化程序添加到模块中的提供程序:

    import { NgbDateParserFormatter, ... } from '@ng-bootstrap/ng-bootstrap';
    import { CustomDateParserFormatter } from "./datepicker-formatter";
    
    @NgModule({
      ...
      providers: [{ provide: NgbDateParserFormatter, useClass: CustomDateParserFormatter }]
    })
    export class AppModule { }
    

    【讨论】:

    • 谢谢,这正是我想要的。
    猜你喜欢
    • 2019-08-20
    • 1970-01-01
    • 2018-01-03
    • 2019-09-04
    • 1970-01-01
    • 2018-06-03
    • 2017-09-23
    • 2018-07-01
    • 2020-09-05
    相关资源
    最近更新 更多