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

element-ui 通过按钮式触发日期选择器

element ui

  • 写在前面
  • 1. 自定义的日期时间组件CustomDatePicker.vue
  • 2. 页面效果
  • 总结
  • 写在最后

写在前面

需求:elementui中日期时间选择器,目前只能通过点击input输入框触发日期选择器,我希望能通过其他方式触发日期选择器同时把input输入框去掉,如点击按钮

1. 自定义的日期时间组件CustomDatePicker.vue

<template>
  <div class="date-input">
    <el-input
      v-model="startDateStr"
      :placeholder="$t('task.taskStartTime')"
      type="text"
      clearable
      class="date-input-field"
      @input="validateDate"
    />
    <span class="line"></span>
    <el-input
      v-model="endDateStr"
      :placeholder="$t('task.taskFinishTime')"
      type="text"
      clearable
      class="date-input-field"
      @blur="validateDate"
    />
    <div class="icon-container" @click="toggleDatePicker">
      <i class="el-icon-date" style="font-size: 24px;"></i>
    </div>
    <el-date-picker
      style="
        position: absolute;
        z-index: -100;
        top: 15px;
        left: -178px;
        transform: scale(0.1);
      "
      size="mini"
      v-model="selectedDateRange"
      :editable="false"
      type="datetimerange"
      @change="onDateChange"
      ref="timePick"
      value-format="yyyy-MM-dd HH:mm:ss"
    />
  </div>
</template>

<script>

export default {
  props: {
    // 父组件传过来的值  
    customTimePicker: {  
      type: Array,  
      default: () => {
              return [new Date(), new Date()]
            }  
    },  
  },
  data() {
    return {
      selectedDateRange: [],
      startDateStr: "",
      endDateStr: "",
      error: ''
    };
  },
  created(){
    console.log('====> customTimePicker', this.customTimePicker);
  },
  watch: {
    customTimePicker: {
      handler(newVal) {
        console.log('customTimePicker==>newVal', newVal);
        if (newVal && newVal.length === 2) {
          this.selectedDateRange = [...newVal];
          this.startDateStr = newVal[0];
          this.endDateStr = newVal[1];
        }
      },
      deep: true
    },
    selectedDateRange: {
      handler(newVal, oldVal) {
        if (newVal && newVal.length === 2) {
          if(oldVal && newVal.toString() === oldVal.toString()) {
            return;
          } else {
            this.startDateStr = newVal[0].toString().replace('T', ' ');
            this.endDateStr = newVal[1].toString().replace('T', ' ');
            this.$emit('input', newVal);
          }
        }
      },
      deep: true
    },
    startDateStr(newVal, oldVal) {
      if(oldVal && newVal.toString() === oldVal.toString()) {
        return;
      } else {
        this.selectedDateRange[0] = newVal.toString().replace('T', ' ');
        this.$emit('input', this.selectedDateRange);
      }
    },
    endDateStr(newVal, oldVal) {
      if(oldVal && newVal.toString() === oldVal.toString()) {
        return;
      } else {
        this.selectedDateRange[1] = newVal.toString().replace('T', ' ');
        this.$emit('input', this.selectedDateRange);
      }
    }
  },
  methods: {
    validateDate() {
      const value = this.startDateStr;
      if (value.trim() === '') {
        this.error = '';
        this.$emit('updateError', this.error);
        return;
      }

      // 验证格式
      const regex = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;
      const match = value.match(regex);

      if (!match) {
        this.$message.error('Invalid date format. Please use yyyy-MM-dd HH:mm:ss.');
        //this.error = 'Correct format is yyyy-MM-dd HH:mm:ss';
        // this.$emit('updateError', this.error);
        return;
      }

      // 解析日期
      const [year, month, day, hours, minutes, seconds] = match.slice(1).map(Number);

      // 检查年份是否在合理范围内
      if (year < 1900 || year > 2100) {
        this.$message.error('Invalid year. Please enter a year between 1900 and 2100.');
        // this.error = 'please input valid year';
        // this.$emit('updateError', this.error);
        return;
      }

      // 检查月份是否在1到12之间
      if (month < 1 || month > 12) {
        this.$message.error('Invalid month. Please enter a month between 1 and 12.');
        // this.error = 'please input valid month';
        // this.$emit('updateError', this.error);
        return;
      }

      // 检查日期是否在1到当月的最大天数之间
      const daysInMonth = new Date(year, month, 0).getDate();
      if (day < 1 || day > daysInMonth) {
        this.$message.error('Invalid day. Please enter a day between 1 and the maximum number of days in the selected month.');
        // this.error = 'please input valid day';
        // this.$emit('updateError', this.error);
        return;
      }

       // 检查小时是否在0到23之间
       if (hours < 0 || hours > 23) {
        this.$message.error('Invalid hour. Please enter an hour between 0 and 23.');
        // this.error = 'please input valid hour';
        // this.$emit('updateError', this.error);
        return;
      }

      // 检查分钟是否在0到59之间
      if (minutes < 0 || minutes > 59) {
        this.$message.error('Invalid minute. Please enter a minute between 0 and 59.');
        // this.error = 'please input valid minute';
        // this.$emit('updateError', this.error);
        return;
      }

      // 检查秒是否在0到59之间
      if (seconds < 0 || seconds > 59) {
        this.$message.error('Invalid second. Please enter a second between 0 and 59.');
        // this.error = 'please input valid second';
        // this.$emit('updateError', this.error);
        return;
      }

      // 创建日期对象
      const date = new Date(year, month - 1, day, hours, minutes, seconds);

      // 检查日期是否有效
      if (isNaN(date.getTime())) {
        this.$message.error('Invalid date. Please enter a valid date.');
        // this.error = 'please input valid date';
        // this.$emit('updateError', this.error);
        return;
      }

      this.error = '';
      this.$emit('updateError', this.error);
    },
    toggleDatePicker() {
      //触发日期框展开
      //  document
      //     	.querySelector(".time-date-picker")
      //     	.querySelector("input")
      //     	.focus();
      this.$refs.timePick.focus();
    },
    onDateChange(date) {
      this.startDateStr = date[0];
      this.endDateStr = date[1];
      this.$set(this, 'selectedDateRange', [this.startDateStr, this.endDateStr])
      this.$emit('input', this.selectedDateRange);
    },
    
  },
};
</script>

<style scoped>
.date-input {
  display: flex;
  align-items: center;
  position: relative; /* 为绝对定位的日期选择器提供相对定位 */
}

.date-input-field {
  width: 18%;
  /* flex-grow: 1; /* 让输入框占满剩余空间 */
  /* margin: 0; /* 删除外边距 */
  z-index: 10;
}

.icon-container {
  display: flex;
  align-items: center;
  justify-content: center;
  /*width: 30px; /* 正方形框的宽度 */
  /*height: 30px; /* 正方形框的高度 */
  /*border: 1px solid #ccc; /* 正方形框的边框 */
  cursor: pointer;
  /*background-color: #f9f9f9; /* 可以选择性添加背景色 */

  background: transparent;
  color: #008ed0;
  /*border: 1px solid #008ed0;
}

.icon {
  font-size: 16px; /* 调整图标大小 */
  font-weight: bold; /* 粗体字 */
  margin: 0; /* 删除图标的外边距 */
}
/*
.timePickCSS {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 1000;
}
*/
.line {
  display: inline-block;
  width: 10px;
  height: 2px;
  background-color: #005987;
}
</style>

2. 页面效果

在这里插入图片描述

总结

写这篇博客主要的目的是让自己更深刻,有回忆点,然后就是分享自己的做法;有需要的根据自己的需求进行修改

写在最后

如果此文对您有所帮助,请帅戈靓女们务必不要吝啬你们的Zan,感谢!!不懂的可以在评论区评论,有空会及时回复。


http://www.kler.cn/news/329725.html

相关文章:

  • 【流计算】流计算概论
  • 如何安装和设置 Go 版本的 fabric AI 工作流框架?
  • MySQL基础篇 - 多表查询
  • Jmeter生成JWT token
  • 海外媒体投稿:如何运用3种国内外媒体套餐发稿突出重围?
  • 注册安全分析报告:人民卫生音像
  • profile-spec-ref元素
  • 【JavaEE】——线程池大总结
  • 代码随想录算法训练营DAY10之动态规划(二)背包问题
  • 什么是 Supply chain attack(供应链攻击)
  • 大数据毕业设计选题推荐-民族服饰数据分析系统-Python数据可视化-Hive-Hadoop-Spark
  • 针对考研的C语言学习(定制化快速掌握重点3)
  • 如何批量转换大模型训练数据集格式
  • 使用容器启动的zk无法暴露3888问题解决
  • 创建Vue项目的时出现:无法加载文件 E:\software\node\node_global\vue.ps1,因为在此系统上禁止运行脚本
  • Android SQLite的基本使用、生成Excel文件保存到本地
  • 6.MySQL基本查询
  • 50. GLTF格式简介 (Web3D领域JPG)
  • 0708-指针和字符数组(上)(下)
  • 【不看会后悔系列】排序之——文件归并【史上最全详解】~
  • 数据结构之栈和队列——LeetCode:155. 最小栈,20. 有效的括号,1249. 移除无效的括号
  • Ktor快速上手1 - 第一个服务端项目
  • el-table表格点击该行任意位置时也勾选上其前面的复选框
  • OpenCV第十二章——人脸识别
  • 介绍篇| 爬虫工具介绍
  • 算法-汉诺塔问题(Hanoi tower)
  • Rust(1)基础语法
  • 【Python】探索自然语言处理的利器:THULAC 中文词法分析库详解
  • 【Redis】Redis中的 AOF(Append Only File)持久化机制
  • 【C++】set容器和map容器的基本使用