在Ionic 4中在警报上渲染默认日期

时间:2019-02-20 22:47:07

标签: ionic-framework ionic4

我正在使用Ionic 4 AlertController创建警报,以使用户可以添加任务。问题是,在用户单击输入的日期之前,没有日期显示。我也尝试同时添加minmax

代码如下

async addCustomTask() {
const alert = await this.alertController.create({
  header: 'Add custom task',
  inputs: [
    {
      name: 'task',
      type: 'text',
      placeholder: 'I would like to..'
    },
    {
      name: 'dueDate',
      type: 'date'
    }
  ],
  buttons: [
    {
      text: 'Cancel',
      role: 'cancel',
      cssClass: 'secondary',
      handler: () => {
        return false;
      }
    }, {
      text: 'Ok',
      handler: (data) => {
        this.addTask(data.task, moment.tz(data.dueDate, this.account.timezone));
      }
    }
  ]
});
await alert.present();
};

加载后看起来像这样

first image

单击它的那一刻,所有内容都会根据需要开始工作。它也以正确的格式作为日期提交。

second image

Ionic:

   ionic (Ionic CLI)             : 4.10.3 (/Users/sam/.nvm/versions/node/v11.0.0/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.0.0-rc.0
   @angular-devkit/build-angular : 0.8.9
   @angular-devkit/schematics    : 0.8.9
   @angular/cli                  : 6.2.9
   @ionic/angular-toolkit        : 1.2.2

Cordova:

   cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms     : ios 4.5.5
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.4.0, (and 11 other plugins)

System:

   NodeJS : v11.0.0 (/Users/sam/.nvm/versions/node/v11.0.0/bin/node)
   npm    : 6.4.1
   OS     : macOS Mojave
   Xcode  : Xcode 10.1 Build version 10B61

1 个答案:

答案 0 :(得分:0)

我当前的Ionic版本是4.12.0,需要一个警报弹出窗口才能从用户StartDate和EndDate获取来自过滤器的输入。我了解到的是,它允许您添加“日期”类型,但仅支持“ STRING”作为值和最大值。另外,请记住不要混用输入类型。以下是我的代码

private format_date(dt: any) {
    var today = new Date(dt);
    let dd: any = today.getDate();
    let mm: any = today.getMonth() + 1;
    const yyyy = today.getFullYear();
    if (dd < 10) {
        dd = `0${dd}`;
    }
    if (mm < 10) {
        mm = `0${mm}`;
    }
    return `${yyyy}-${mm}-${dd}`;
}
async presentPopover() {
    const dt = new Date();
    this.firstDate =   dt.getFullYear() + '-' + ((dt.getMonth() + 1) < 10 ? '0' + (dt.getMonth() + 1) : (dt.getMonth() + 1) ) + '-01';
    this.maxDate = this.format_date(dt);
    this.lastDay = new Date(dt.getFullYear(), dt.getMonth() + 2, 0);
    this.lastDay = this.format_date(this.lastDay);

    if(new Date(this.lastDay) > dt) {
      this.lastDay = this.format_date(dt);
    }
    const alerts = await this.alertController.create({
      header: 'Filter',
      inputs: [
        {
          name: 'fromDt',
          type: 'date',
          value: this.firstDate,
          max: this.maxDate
        },
        {
          name: 'toDt',
          type: 'date',
          value: this.lastDay,
          max: this.maxDate
        }
      ],
      buttons: 
      [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('Confirm Cancel');
          }
        }, 
        {
          text: 'Ok',
          handler: () => {
              console.log('Confirm Ok');
              this.fetchData();
            }
          }
      ]
    });
    await alerts.present();    
}