angular 使用 mob-date-selector 教程

发布时间:2019-06-10 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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;
  }
}
基本完成

@H_736_406@

颜色不太好看?改一下咯

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

angular 使用 mob-date-selector 教程

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...

脚本宝典总结

以上是脚本宝典为你收集整理的angular 使用 mob-date-selector 教程全部内容,希望文章能够帮你解决angular 使用 mob-date-selector 教程所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。