【问题标题】:DatePicker in Angular NG-BoostrapAngular NG-Bootstrap 中的 DatePicker
【发布时间】:2019-07-08 03:38:49
【问题描述】:

我从 ng-bootstrap 范围选择日期选择器中选择日期时遇到问题。我只能看到一个日期的值,但看不到第二个日期的值。使用反应式表单时,我将如何选择第二个日期。谢谢。请参阅此 stackblitz 链接CLICK HERE

<ngb-datepicker #dp (select)="onDateSelection($event)" [displayMonths]="2" [dayTemplate]="t" outsideDays="hidden" formControlName="date">
    </ngb-datepicker>

    <ng-template #t let-date let-focused="focused">
        <span class="custom-day"
        [class.focused]="focused"
        [class.range]="isRange(date)"
        [class.faded]="isHovered(date) || isInside(date)"
        (mouseenter)="hoveredDate = date"
        (mouseleave)="hoveredDate = null">
    {{ date.day }}
  </span>
</ng-template>

【问题讨论】:

    标签: angular angular6 angular-reactive-forms ng-bootstrap angular-forms


    【解决方案1】:

    您没有在反应式表单中插入第二个日期,这就是您看不到它的原因。我的方法是使用setValue 在响应式表单中插入起始/终止日期

    相关HTML

    <form [formGroup]="myDateForm" (ngSubmit)="onSubmit(myDateForm)">
    
      <ngb-datepicker #dp (select)="onDateSelection($event)" [displayMonths]="2" [dayTemplate]="t" outsideDays="hidden" >
        </ngb-datepicker>
    
        <ng-template #t let-date let-focused="focused">
            <span class="custom-day"
            [class.focused]="focused"
            [class.range]="isRange(date)"
            [class.faded]="isHovered(date) || isInside(date)"
            (mouseenter)="hoveredDate = date"
            (mouseleave)="hoveredDate = null">
        {{ date.day }}
      </span>
    </ng-template>
    
    <!--
      <pre>From: {{ fromDate | json }} </pre>
    <pre>To: {{ toDate | json }} </pre>
    -->
    <br/>
    <button type="submit" class="btn btn-sm btn-primary">Submit</button>
    </form>
    
    <b>Form Status:</b>{{myDateForm.value | json}}
    

    相关TS

    import { Component } from '@angular/core';
    import { NgbDate, NgbCalendar } from '@ng-bootstrap/ng-bootstrap';
    import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
    
    @Component({
      selector: 'ngbd-datepicker-range',
      templateUrl: './datepicker-range.html',
      styles: [`
        .custom-day {
          text-align: center;
          padding: 0.185rem 0.25rem;
          display: inline-block;
          height: 2rem;
          width: 2rem;
        }
        .custom-day.focused {
          background-color: #e6e6e6;
        }
        .custom-day.range, .custom-day:hover {
          background-color: rgb(2, 117, 216);
          color: white;
        }
        .custom-day.faded {
          background-color: rgba(2, 117, 216, 0.5);
        }
      `]
    })
    export class NgbdDatepickerRange {
    
      hoveredDate: NgbDate;
    
      fromDate: NgbDate;
      toDate: NgbDate;
    
      dateForm: FormGroup;
    
    
      myDateForm: FormGroup;
      myFromDate: FormControl;
      myToDate: FormControl;
    
      constructor(calendar: NgbCalendar, private formBuilder: FormBuilder) {
        this.fromDate = calendar.getToday();
        this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
        this.dateForm = this.formBuilder.group({
          date: [null, Validators.required],
          endDate: [null, Validators.required],
        });
    
        this.myDateForm = new FormGroup({
          myFromDate: new FormControl(''),
          myToDate: new FormControl(''),
        });
    
      }
    
      onDateSelection(date: NgbDate) {
        if (!this.fromDate && !this.toDate) {
          this.fromDate = date;
        } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
          this.toDate = date;
          let dateObj = new Date(this.toDate.year, this.toDate.month, this.toDate.day);
          this.myDateForm.controls.myFromDate.setValue(dateObj);
        } else {
          this.toDate = null;
          this.fromDate = date;
          let dateObj = new Date(this.fromDate.year, this.fromDate.month, this.fromDate.day);
          this.myDateForm.controls.myToDate.setValue(dateObj);
        }
      }
    
      isHovered(date: NgbDate) {
        return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
      }
    
      isInside(date: NgbDate) {
        return date.after(this.fromDate) && date.before(this.toDate);
      }
    
      isRange(date: NgbDate) {
        return date.equals(this.fromDate) || date.equals(this.toDate) || this.isInside(date) || this.isHovered(date);
      }
    
      onSubmit(form: FormGroup) {
        console.log(form.value)
      }
    
    }
    

    完成working stackblitz here

    【讨论】:

      【解决方案2】:

      你可以这样做

      例如this

      import {Component} from '@angular/core';
      import {NgbDate, NgbCalendar} from '@ng-bootstrap/ng-bootstrap';
      import { FormGroup, FormBuilder, Validators } from '@angular/forms';
      import { NgbDateParserFormatter } from './NgbDateParserFormatter';
      
      
      @Component({
        selector: 'ngbd-datepicker-range',
        templateUrl: './datepicker-range.html',
        styles: [`
          .custom-day {
            text-align: center;
            padding: 0.185rem 0.25rem;
            display: inline-block;
            height: 2rem;
            width: 2rem;
          }
          .custom-day.focused {
            background-color: #e6e6e6;
          }
          .custom-day.range, .custom-day:hover {
            background-color: rgb(2, 117, 216);
            color: white;
          }
          .custom-day.faded {
            background-color: rgba(2, 117, 216, 0.5);
          }
        `]
      })
      
      
      export class NgbdDatepickerRange {
      
        hoveredDate: NgbDate;
        alldate:String;
        fromDate: any;
        toDate: any;
      
         list: NgbDate[] = [];
         dateForm: FormGroup;
      
        constructor(calendar: NgbCalendar, private formBuilder: FormBuilder) {
          this.fromDate = calendar.getToday();
          this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
          this.dateForm = this.formBuilder.group({
            date: [null, Validators.required],
          });
        }
      
        onDateSelection(date: NgbDate) {
          if (!this.fromDate && !this.toDate) {
            this.fromDate = date;
            console.log("if " +JSON.stringify(date));
      
          } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
            this.toDate = date;
            this.list.push(date);
            console.log("else if " +JSON.stringify(date));
          } else {
            this.toDate = null;
            this.list.push(date);
            this.fromDate = date;
            console.log("else " +JSON.stringify(date));
      
          }
        }
      
        isHovered(date: NgbDate) {
          return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
        }
      
        isInside(date: NgbDate) {
          return date.after(this.fromDate) && date.before(this.toDate);
        }
      
        isRange(date: NgbDate) {
          return date.equals(this.fromDate) || date.equals(this.toDate) || this.isInside(date) || this.isHovered(date);
        }
      
        onSubmit(form: FormGroup) {
       console.log(this.list);
        console.log(form.value)
        }
      
      }
      

      【讨论】:

        【解决方案3】:

        另一个。你的表格是这样的

        this.dateForm = new FormGroup({
             from:new FormControl(),
             to:new FormControl()
           })
        

        不要将formControlName放在.html中,并将onDateSeleccion函数更改为

        onDateSelection(date: NgbDate) {
            if (!this.fromDate && !this.toDate) {
              this.fromDate = date;
              this.dateForm.get('from').setValue(date);
            } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
              this.toDate = date;
              this.dateForm.get('to').setValue(date);
            } else {
              this.toDate = null;
              this.fromDate = date;
              this.dateForm.get('from').setValue(date);
              this.dateForm.get('to').setValue(null);
            }
          }
        

        stackblitz

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-15
          • 1970-01-01
          • 2019-06-06
          • 2021-08-04
          • 1970-01-01
          • 2017-09-25
          • 1970-01-01
          相关资源
          最近更新 更多