限制选择时间控件

前言

最近刚接触的NG-ZORRO,今天小编给大家分享一下NG-ZORRO中,时间控件限制时间选择的知识。

html
 <nz-range-picker
      (click)="picker()"
      [(ngModel)]="dateValue"
      name="dateValue"
      [nzDisabledDate]="disabledDate"
      [nzDisabledTime]="disabledRangeTime"
      [nzShowTime]="{ nzHideDisabledOptions: true, nzDefaultOpenValue: timeDefaultValue }"
      nzFormat="yyyy-MM-dd HH:mm:ss"
  ></nz-range-picker>
ts
range(start: number, end: number): number[] {
        const result = [];
        for (let i = start; i < end; i++) {
            result.push(i);
        }
        return result;
    }
    disabledDate = (current: Date): boolean => {
        return differenceInCalendarDays(current, this.today) < 0;
    }

    disabledRangeTime = (value: Date[], type: 'start' | 'end'): object => {
        for (let i = 0; i < this.dateValue.length; i++) {
            // 选择日
            this.data1 = this.dateValue[0].getFullYear() + '-' + (this.dateValue[0].getMonth() + 1) + '-' + this.dateValue[0].getDate() + ' '
                + this.dateValue[0].getHours() + ':' + this.dateValue[0].getMinutes() + ':' + this.dateValue[0].getSeconds();
        }
        if (type === 'start') {
            // 选择日日期跟服务器日期相同,限制时间控件不可以选当前时间以及之前的回见
            if (this.data1.substr(0, 10) === this.time.substr(0, 10)) {
                this.hours = this.time.substr(11, 2);
                this.minutes =  this.time.substr(14, 2);
                this.seconds =  this.time.substr(17, 2);
                console.log('this.hours');
                console.log('this.minutes');
                console.log('this.seconds');
                return {
                    nzDisabledHours: () => this.range(0, 60).splice(0, this.hours),
                    nzDisabledMinutes: () => this.range(0, this.minutes),
                    nzDisabledSeconds: () => [55, this.seconds]
                };
            } else {
                return {
                    nzDisabledHours: () => this.range(0, 60).splice(0, this.hours),
                    nzDisabledMinutes: () => this.range(0, this.minutes),
                    nzDisabledSeconds: () => [55, 56]
                };
            }
        }
        return {
            nzDisabledHours: () => this.range(0, 60).splice(0, 0),
            nzDisabledMinutes: () => this.range(0, 0),
            nzDisabledSeconds: () => [55, 56]
        };
    }
/**
    * 点击日期控件获取服务器时间
    * @author 
    * @since 
 */
 
 picker() {
        const url = 'exam-web/examination/selTime';
            this.http.get(url).subscribe(
                res => {
                    if (res.json().code === ResponseCode.SUCCESSCODE) {
                        this.time = res.json().data;
                        console.log(this.time);
                        console.log(res.json());

                    } else if (res.json().code === ResponseCode.FAILCODE) {
                        this.tipMsgService.error('警告', '考试添加失败!');
                    }
              }
      );
 }

选择开始日期(2018/12/16)和结束日期(2018/12/17)
在这里插入图片描述
选择开开始时间和结束时间
在这里插入图片描述
服务器时间
在这里插入图片描述

大家看以上三张图,有没有发现时间控件有些时间是不可以选择的,是因为在ts中限制了开始时间不能大于当前时间,这样限制住控件不在选择范围内的时间不可编辑,很方便用户使用。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值