当前位置: 首页 > article >正文

vant_UI的选择时间小组件封装

<template>
  <div>
    <div class="time-container">
      <span>开始时间 :</span>
      <van-field v-model="startTimeInner" placeholder="请输入开始时间"  @focus='chooseTime(true)'  @blur="onInputBlur($event, true)" style=" padding-left:5px;width:185px;padding: 0px 5px;" />
      <!-- <van-button @click="chooseTime(true)" size="mini" type="primary" style="padding: 0px 5px;min-width: 40px;">选择</van-button> -->
    </div>
    <div class="time-container">
      <span>结束时间 :</span>
      <van-field v-model="endTimeInner" placeholder="请输入结束时间" @focus='chooseTime(true)'   @blur="onInputBlur($event, false)" style="padding-left:5px; width:185px;padding: 0px 5px;" />
      <!-- <van-button @click="chooseTime(false)" size="mini" type="primary" style="padding: 0px 5px;min-width: 40px;">选择</van-button> -->
    </div>
    <van-popup v-model="showPopup" round position="top" :style="{ height: '40%' }">
      <van-datetime-picker v-model="currentDate" @confirm="onConfirm" @cancel="onCancel" type="datetime" :title="isStartTime ? '选择开始时间' : '选择结束时间'" :formatter="timeFormatter" />
    </van-popup>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Vue, Emit, Watch } from 'vue-property-decorator'
import PageBase from '@src/views/PageBase'
import moment from 'moment'

@Component({
  components: {}
})
export default class timePickr extends PageBase {
  mounted() {}
  // 开始时间
  @Prop({
    default: () => {
      return new Date()
    }
  })
  private startTime
  private startTimeInner: any = new Date()
  // @Emit('update:startTime')
  // private updateStartTime_inner(val: any) {}
  @Emit('updateStartTimeFn')
  private updateStartTimeFn(val: any) {
    return val
  }
  @Watch('startTime', { deep: true, immediate: true })
  private watchStartTimeChange(val: any) {
    this.startTimeInner = val
  }
  // ===========
  // 结束时间
  @Prop({
    default: () => {
      return new Date()
    }
  })
  private endTime
  private endTimeInner: any = new Date()
  // @Emit('update:endTime')
  // private updateEndtime_inner(val: any) {}
  @Emit('updateEndTimeFn')
  private updateEndTimeFn(val: any) {
    return val
  }
  @Watch('endTime', { deep: true, immediate: true })
  private watchEndTimeChange(val: any) {
    this.endTimeInner = val
  }
  // ===========
  private showPopup: boolean = false
  private currentDate: any = ''
  private isStartTime: boolean = false
  private chooseTime(isStartTime) {
    this.showPopup = true
    if (isStartTime) {
      this.currentDate = moment(this.startTimeInner).toDate()
    } else {
      this.currentDate = moment(this.endTimeInner).toDate()
    }
    this.isStartTime = isStartTime
  }
  private onCancel() {
    this.showPopup = false
  }
  // 日期选择器确定时触发
  private onConfirm(chooseTime) {
    if (this.isStartTime) {
      if (moment(this.endTimeInner).diff(moment(chooseTime)) <= 0) {
        this.$notify({ type: 'warning', title: 'mes', message: '开始时间不能大于结束时间!' })
        this.startTimeInner = this.startTime
        return
      }
      this.startTimeInner = moment(chooseTime).format('YYYY-MM-DD HH:mm:ss')
      this.updateStartTimeFn(this.startTimeInner)
    } else {
      if (moment(this.startTimeInner).diff(moment(chooseTime)) >= 0) {
        this.$notify({ type: 'warning', title: 'mes', message: '结束时间不能小于开始时间!' })
        this.endTimeInner = this.endTime
        return
      }
      this.endTimeInner = moment(chooseTime).format('YYYY-MM-DD HH:mm:ss')
      this.updateEndTimeFn(this.endTimeInner)
    }
    this.showPopup = false
  }
  // 日期输入框失去焦点时触发
  private onInputBlur(event, isStartTime) {
    this.isStartTime = isStartTime
    this.onConfirm(event.target.value)
  }
  private timeFormatter(type, val) {
    if (type === 'year') {
      return val + '年'
    }
    if (type === 'month') {
      return val + '月'
    }
    if (type === 'day') {
      return val + '日'
    }
    if (type === 'hour') {
      return val + '时'
    }
    if (type === 'minute') {
      return val + '分'
    }
    return val
  }
}
</script>

<style scoped lang="less">
.time-container {
  margin-top: 10px;
  display: flex;
  align-items: center;
  span {
    padding: 0px 5px 0px 0px;
    white-space: nowrap;
    min-width: 64px;
  }
}
/deep/ .van-cell.van-field {
  padding: unset;
}
/deep/ .van-field__left-icon {
  margin-right: 10px;
  .van-icon {
    font-size: 1.4rem;
  }
}
/deep/ .van-field__value {
  border: 1px solid #969799;
}
/deep/ .van-field__control {
    display: block;
    box-sizing: border-box;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0;
    color: #323233;
    line-height: inherit;
    text-align: left;
    background-color: transparent;
    border: 0;
    resize: none;
    padding-left: 10px;
}
</style>

使用:

 <timePickr :startTime="startTime" :endTime="endTime" @updateEndTimeFn="updateEndTime" @updateStartTimeFn="updateStartTime"></timePickr>

 private updateStartTime(val) {
    this.startTime = val
    this.refreshData()
  }


http://www.kler.cn/a/319589.html

相关文章:

  • 【计算机网络】【传输层】【习题】
  • 群控系统服务端开发模式-应用开发-前端个人信息功能
  • 三维测量与建模笔记 - 特征提取与匹配 - 4.2 梯度算子、Canny边缘检测、霍夫变换直线检测
  • docker配置代理解决不能拉镜像问题
  • 深度学习之卷积问题
  • [CKS] K8S NetworkPolicy Set Up
  • excel VBA进行间比法设计
  • 运行python程序
  • 初识前端监控
  • C++如何进阶? -- 整理一些学习资料
  • 基于stm32物联网身体健康检测系统
  • LeetCode 909. 蛇梯棋
  • nlohmann json:读写json文件
  • c++优先级队列自定义排序实现方式
  • SDK3(note上)
  • NLP 文本分类任务核心梳理
  • Selenium点击元素的方法
  • 【深入学习Redis丨第六篇】Redis哨兵模式与操作详解
  • 电脑自带dll修复在哪里,dll丢失的6种解决方法总结
  • 免费与付费代理IP工具的优缺点分析
  • 遗忘的数学(拉格朗日乘子法、牛顿法)
  • (16)docker自动镜像打包脚本
  • 【Power Query】追加查询(动态列,动态路径)
  • 微软宣布弃用WSUS,企业用户尽早准备替换方案
  • [RabbitMQ] RabbitMQ介绍,安装与快速上手
  • 虚拟机开启网络代理设置,利用主机代理访问国外资源