angular 使用 mob-date-selector 教程

angular 使用mob-date-selector教程

1. 安装

npm i mob-date-selector --save-dev

2. 引入css

angular.json
"styles": [
    "node_modules/ng-zorro-antd/src/ng-zorro-antd.css",
    "node_modules/mob-date-selector/index.css",
    "src/styles.scss"
],

3. html

test.component.html
<form nz-form [formGroup]="timeForm" nzLayout="horizontal">
    <nz-form-item>
        <nz-form-control>
            <input id="stime-picker" nz-input placeholder="开始时间" formControlName="stime" />
        </nz-form-control>
    </nz-form-item>
    <nz-form-item>
        <nz-form-control>
            <input id="etime-picker" nz-input placeholder="结束时间" formControlName="etime" />
        </nz-form-control>
    </nz-form-item>
</form>
<!-- 多个input需要对应多个Container -->
<div id="sdate-picker"></div>
<div id="edate-picker"></div>

4. ts

test.component.ts
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import DateSelector from 'mob-date-selector';
import * as dateFns from 'date-fns';
@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit, AfterViewInit {

  timeForm: FormGroup; // 定义表单

  ngOnInit() {
    // 定义formControl
    /*
       使用插件时,input绑定了touchstart事件,
       然而点击时有可能会使input聚焦,因此禁用input
    */
    this.timeForm = this.fb.group({
      stime: [{ value: '', disabled: true }],
      etime: [{ value: '', disabled: true }]
    });
  }
  ngAfterViewInit() {
    this.createPicker();
  }

  createPicker() {
    const stimePicker = new DateSelector({
      input: 'stime-picker',
      container: 'sdate-picker',
      type: 1,
      param: [1, 1, 1, 1, 1],
      beginTime: [],
      endTime: [],
      recentTime: [],
      success: (arr1) => {
        const res = this.getTimeString(arr1);
        this.timeForm.get('stime').setValue(res);
      }
    });
    
    const etimePicker = new DateSelector({
      input: 'etime-picker',
      container: 'edate-picker',
      type: 1,
      param: [1, 1, 1, 1, 1],
      beginTime: [],
      endTime: [],
      recentTime: [],
      success: (arr1) => {
        const res = this.getTimeString(arr1);
        this.timeForm.get('etime').setValue(res);
      }
    });
  }

  getTimeString(arr): string {
    const timeString = dateFns.format(
      new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4]),
      'YYYY-MM-DD HH:mm:ss'
    );
    return timeString;
  }
}
基本完成

mdp.jpg

颜色不太好看?改一下咯

而且input设置了disabled后样式与正常的不一样,会让人感觉点不了的,改回来

input.jpg

5. css(scss)

// 使用 ::ng-deep 即可选中组件内元素
::ng-deep .ant-input-disabled { // ng-zorro ui框架自带的样式,改一下
    background-color: #fff;
    color:rgba(0, 0, 0, 0.65);
    cursor: pointer;
}

::ng-deep .date-selector-btn-box {
    .date-selector-tab {
        color: #1890ff;
    }
    .date-selector-tab-active {
        background-color: #4FC3F7;
        color: #fff;
    }
    background-color: #1890ff;
}

demo

https://github.com/ZhongMingK...

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值