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

南宁网站建设gxjzdrj长春昆仑建设股份有限公司网站

南宁网站建设gxjzdrj,长春昆仑建设股份有限公司网站,车贴广告设计图片,wordpress模板放在哪里element ui中日期和时间选择器#xff08;DateTimePicker#xff09;是一个常见且重要的组件。它允许用户轻松地选择日期和时间#xff0c;极大地提升了用户体验。然而#xff0c;在某些场景下#xff0c;用户可能需要更细粒度的控制#xff0c;例如单独清除已选择的时间…element ui中日期和时间选择器DateTimePicker是一个常见且重要的组件。它允许用户轻松地选择日期和时间极大地提升了用户体验。然而在某些场景下用户可能需要更细粒度的控制例如单独清除已选择的时间而保留日期。效果如下图本文将带领你一步步封装一个DateTimePicker组件实现时间的独立清除功能。 实现步骤 1、在components中新建组件dataTimePicker templateel-date-picker:popper-classuniqueIdstylemargin-left: 5pxtypedatetimev-modeldataTimeValue:placeholderplaceholderchangechangeDateTimefocusonFocus/el-date-picker /template 1、1给每个组件自定义一个唯一的下拉框class名  data() {return {dataTimeValue: , // 初始化值uniqueId: date-picker-${Math.random().toString(36).substring(2, 15)}, // 生成一个唯一的IDisAddCloseIcon: false,};}, 1.2 该组件接收两个props props: {timeValue: {type: Date, // 初始展示时间required: false,},placeholder: {type: String,require: false,default: 起始时间,},}, 1.3 在 onFocus事件中添加清除图标因为只有当onFocus触发时下拉框才显示才能获取到下拉框dom onFocus() {if (this.isAddCloseIcon) return; // 避免重复添加this.$nextTick(() {this.timeDiv document.querySelectorAll(.${this.uniqueId} .el-date-picker__editor-wrap .el-input,this.$el)[1];this.span document.createElement(span);this.span.innerHTML i classel-icon-circle-close/i;this.span.style.position absolute;this.span.style.right 10px;this.span.style.cursor pointer;this.span.style.display none; // 初始时隐藏spanthis.timeDiv.appendChild(this.span);// 鼠标移入事件this.timeDiv.addEventListener(mouseenter,this.handleMouseenter);// 鼠标移出事件this.timeDiv.addEventListener(mouseleave, this.handleMouseLeave);// 点击事件清除时间置为00:00:00this.span.addEventListener(click, this.clearDateTimeValue);this.isAddCloseIcon true;});}, 1.4  清除时间事件以及鼠标移入显示图标移除隐藏图标 clearDateTimeValue() {this.dataTimeValue this.$moment(this.dataTimeValue).startOf(day).toISOString();},handleMouseLeave() {this.span.style.display none;},handleMouseenter() {this.span.style.display inline;} 1.5 时间变化时通知父组件 changeDateTime(value) {this.dataTimeValue value;this.$emit(change, value);}, 1.6 组件销毁时移除监听事件 beforeDestroy() {// 组件销毁前清理事件监听器this.span.removeEventListener(click,this.clearDateTimeValue)this.timeDiv.removeEventListener(mouseenter,this.handleMouseenter)this.timeDiv.removeEventListener(mouseleave,this.handleMouseLeave)this.span nullthis.timeDiv null}, 完整代码 dataTimePicker.vue: templateel-date-picker:popper-classuniqueIdstylemargin-left: 5pxtypedatetimev-modeldataTimeValue:placeholderplaceholderchangechangeDateTimefocusonFocus/el-date-picker /templatescript export default {props: {timeValue: {type: Date,required: false,},placeholder: {type: String,require: false,default: 起始时间,},},data() {return {dataTimeValue: , // 初始化值uniqueId: date-picker-${Math.random().toString(36).substring(2, 15)}, // 生成一个唯一的IDisAddCloseIcon: false,};},mounted() {this.dataTimeValue this.timeValue;},methods: {changeDateTime(value) {this.dataTimeValue value;this.$emit(change, value);},onFocus() {if (this.isAddCloseIcon) return; // 避免重复添加this.$nextTick(() {this.timeDiv document.querySelectorAll(.${this.uniqueId} .el-date-picker__editor-wrap .el-input,this.$el)[1];this.span document.createElement(span);this.span.innerHTML i classel-icon-circle-close/i;this.span.style.position absolute;this.span.style.right 10px;this.span.style.cursor pointer;this.span.style.display none; // 初始时隐藏spanthis.timeDiv.appendChild(this.span);// 鼠标移入事件this.timeDiv.addEventListener(mouseenter,this.handleMouseenter);// 鼠标移出事件this.timeDiv.addEventListener(mouseleave, this.handleMouseLeave);this.span.addEventListener(click, this.clearDateTimeValue);this.isAddCloseIcon true;});},clearDateTimeValue() {this.dataTimeValue this.$moment(this.dataTimeValue).startOf(day).toISOString();},handleMouseLeave() {this.span.style.display none;},handleMouseenter() {this.span.style.display inline;}},beforeDestroy() {// 组件销毁前清理事件监听器this.span.removeEventListener(click,this.clearDateTimeValue)this.timeDiv.removeEventListener(mouseenter,this.handleMouseenter)this.timeDiv.removeEventListener(mouseleave,this.handleMouseLeave)this.span nullthis.timeDiv null}, }; /script 2、在父组件引入 template dateTimePicker :timeValuestartTime changechangeDateTime/dateTimePicker /template script typetext/ecmascript-6 import dateTimePicker from /components/dataTimePicker.vue export default { ...,methods: {changeDateTime(value) {console.log(变化,value) } } /script3、总结 通过以上方法我们可以实现在不改变日期的情况下单独清除时间功能实际上就是在element ui原本的el-date-picker组件上进行改装增加一个清除按钮并绑定清除事件来实现。实际应用中你可以根据具体需求进一步扩展和定制这个组件以满足不同场景下的使用。希望这篇文章能对你有所帮助
http://www.zqtcl.cn/news/17937/

相关文章:

  • 响应式个人网站模板网络整合营销的目的是什么
  • google建网站镇江网站推广优化
  • 优惠券精选网站怎么做网站空间租用费用
  • 新一站保险网智慧团建网站首页
  • 龙江网站设计制作泰安市齐鲁人才网
  • 延安网站建设公司外国人搞笑做视频网站
  • 建立网站有什么好处中国国防新闻
  • 西安公司的网站建设长沙房地产交易网
  • 模板网站 动易mysql做网站
  • 一流的嘉兴网站建设买了域名怎么用
  • 帮朋友做网站 知乎网站备案为什么要关闭
  • 网站建设需要用到的技术有没有哪些可以看片的
  • 织梦cms网站seo推广有用吗
  • 厦门网站怎么做青海西宁网站建设
  • 网站建设团购佳木斯市郊区建设局网站
  • 南宁建企业网站公司黄石网站建设哪家好
  • 怎么仿做网站单位网站设计制作
  • 北京公司注册流程及需要的材料seo是什么意思啊电商
  • seo站点是什么意思青岛seo关键词优化排名
  • 建设银行网站点不了建网站需要什么步骤
  • 齐齐哈尔网站seo个人怎么做网页
  • 泌阳县住房建设局网站网站建设步骤及推广方法
  • wordpress搜索筛选商城网站优化方案
  • 更换网站空间网络公司经营范围开发属于制造吗
  • 有没有关于网站开发的名人访谈深圳网站建设价格
  • 公司网站被抄袭中国著名设计建筑
  • 重庆装修工人哪里找嘉定网站设计制作优化排名
  • 给一个公司做网站需要多久上海单位名称大全
  • 威海网站建设网站开发项目报价
  • 厦门网站制最新的新闻 今天