当前位置: 首页 > 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.pierceye.com/news/17937/

相关文章:

  • 番禺建设网站哪家好网站建设公司的pest分析
  • 济南建设集团网站wordpress 菜单没了
  • 找人做菠菜网站需要多少钱上海襄阳网站建设
  • 网络销售型网站有哪些内容宁波网站营销推广制作
  • 网站开发使用什么语言软件开发培训去哪报名
  • 西安在线网站制作wordpress 能承受多大并发访问
  • asp企业网站cms深圳网络营销推广外包
  • 专业摄影网站网站建设中字样图片
  • 创造网站的最简单 软件是哪个商丘网站建设哪家值得信任
  • 国内做医疗耗材的网站工商服务网
  • 高端大气的的网站上海原画培训机构排名
  • 怎么做网站互换链接个人网站建设知乎
  • sq网站推广海外营销策略
  • 如何制作一个属于自己的网站新乡 网站开发
  • 广州市住房和城乡建设局官方网站微信小程序商城开发教程
  • 国内外包网站wordpress连阿里云域名
  • 如何侵入网站服务器网站开发设计知乎
  • 一份电子商务网站建设规划书网站域名所有权
  • 网站源码做exe执行程序小程序哪家开发最好
  • 哪里学做网站网站备案主体是什么
  • 网站开发技术是什么专业会的江苏手机网站建设
  • 做网站用的字体是什么网站项目建设流程
  • 网站建设最低要求插件 wordpress开发
  • 佛山英文网站建设娄底网站制作
  • 贵阳市住房城乡建设局八大员网站宝塔面板配置wordpress
  • 织梦招商加盟网站源码怎么制作图片表格
  • 做网站维护有前途吗湖南网站推广多少钱
  • 专门做正品的网站有哪些长沙百度首页优化排名
  • 花生壳可以做网站吗百度小说
  • 龙岩市城乡规划建设局网站怎么查自己的邮箱号