element-ui 通過(guò)按鈕式觸發(fā)日期選擇器
element-ui 通過(guò)按鈕式觸發(fā)日期選擇器
寫(xiě)在前面
需求:elementui中日期時(shí)間選擇器,目前只能通過(guò)點(diǎn)擊input輸入框觸發(fā)日期選擇器,我希望能通過(guò)其他方式觸發(fā)日期選擇器同時(shí)把input輸入框去掉,如點(diǎn)擊按鈕
1. 自定義的日期時(shí)間組件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: { // 父組件傳過(guò)來(lái)的值 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; } // 驗(yàn)證格式 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); // 檢查年份是否在合理范圍內(nèi) 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到當(dāng)月的最大天數(shù)之間 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; } // 檢查小時(shí)是否在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; } // 創(chuàng)建日期對(duì)象 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() { //觸發(fā)日期框展開(kāi) // 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; /* 為絕對(duì)定位的日期選擇器提供相對(duì)定位 */ } .date-input-field { width: 18%; /* flex-grow: 1; /* 讓輸入框占滿(mǎn)剩余空間 */ /* 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; /* 調(diào)整圖標(biāo)大小 */ font-weight: bold; /* 粗體字 */ margin: 0; /* 刪除圖標(biāo)的外邊距 */ } /* .timePickCSS { position: absolute; top: 100%; left: 0; z-index: 1000; } */ .line { display: inline-block; width: 10px; height: 2px; background-color: #005987; } </style>
2. 頁(yè)面效果
總結(jié)
寫(xiě)這篇博客主要的目的是讓自己更深刻,有回憶點(diǎn),然后就是分享自己的做法;有需要的根據(jù)自己的需求進(jìn)行修改
補(bǔ)充:
element UI時(shí)間組件兩種使用方式
加油,新時(shí)代打工!
組件官網(wǎng):https://element.eleme.cn/
先上效果圖,如下:
第一種實(shí)現(xiàn)方式
<div class="app-container"> <el-form ref="submitForm" :model="submitForm" class="form-item table" label-width="80px" label-position="left" :inline="true" > <el-form-item label="開(kāi)始日期" prop="startDate"> <el-date-picker v-model="submitForm.startDate" type="date" placeholder="請(qǐng)輸入開(kāi)始日期" value-format="yyyy-MM-dd" :picker-options="pickerOptionsStart" ></el-date-picker> </el-form-item> <el-form-item label="結(jié)束日期" prop="endDate"> <el-date-picker v-model="submitForm.endDate" type="date" placeholder="請(qǐng)輸入開(kāi)始日期" value-format="yyyy-MM-dd" :picker-options="pickerOptionsEnd" ></el-date-picker> </el-form-item> <el-form-item> <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button> <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> </el-form-item> </el-form> </div> <script> export default { name: 'statisticsCountyCrops', data() { return { loading: true, // 開(kāi)始結(jié)束日期限制 pickerOptionsStart: { disabledDate: time => { if (this.submitForm.endDate) { return ( time.getTime() >= new Date(this.submitForm.endDate).getTime() ); } } }, // 結(jié)束日期限制 pickerOptionsEnd: { disabledDate: time => { if (this.submitForm.startDate) { return ( time.getTime() <= new Date(this.submitForm.startDate).getTime() ); } } }, submitForm: { startDate: '', endtDate: '' }, }; }, methods: { /* get請(qǐng)求 對(duì)象參數(shù)*/ getList(){ statisticsCountyCrops(this.submitForm).then(res =>{ this.dataList = res.data; this.loading = false; }).catch(err =>{ console.error(err) this.loading = false; }) }, /** 搜索按鈕操作 */ handleQuery() { this.getList(); }, /** 重置按鈕操作 */ resetQuery() { this.daterangeCreateTime = []; this.resetForm("submitForm"); }, }, }; </scrpit>
第二種實(shí)現(xiàn)方式
<el-form-item label="查詢(xún)范圍"> <el-date-picker v-model="daterangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="開(kāi)始日期" end-placeholder="結(jié)束日期" ></el-date-picker> </el-form-item> <script> export default { data() { return { daterangeCreateTime: [], queryParams:{}, } }, this.queryParams = {}; if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) { this.queryParams["startDate"] = this.daterangeCreateTime[0]; this.queryParams["endDate"] = this.daterangeCreateTime[1]; } } <script>
到此這篇關(guān)于element-ui 通過(guò)按鈕式觸發(fā)日期選擇器的文章就介紹到這了,更多相關(guān)element-ui 觸發(fā)日期選擇器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
echarts修改橫坐標(biāo)顏色簡(jiǎn)單代碼示例
這篇文章主要給大家介紹了關(guān)于echarts修改橫坐標(biāo)顏色的相關(guān)資料,在項(xiàng)?中常常會(huì)?到echarts的實(shí)例,根據(jù)不同的需求字體顏?需要變化,需要的朋友可以參考下2023-07-07JS回調(diào)函數(shù) callback的理解與使用案例分析
這篇文章主要介紹了JS回調(diào)函數(shù) callback的理解與使用,結(jié)合具體案例形式分析了javascript回調(diào)函數(shù)的功能、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09JavaScript基礎(chǔ)知識(shí)及常用方法總結(jié)
JAVASCRIPT是AJAX技術(shù)中不可或缺的一部分,所以想學(xué)好AJAX以及現(xiàn)在流行的AJAX框架,學(xué)好JAVASCRIPT是最重要的,通過(guò)本篇文章給大家介紹javascript基礎(chǔ)知識(shí)及常用方法總結(jié),對(duì)js基礎(chǔ)知識(shí)及常用方法相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)菜單左右聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05IE與FireFox的JavaScript兼容問(wèn)題解決辦法
本篇文章主要是對(duì)IE與FireFox的JavaScript兼容問(wèn)題解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12Js鼠標(biāo)跟隨代碼小手點(diǎn)擊實(shí)例方法
一個(gè)跟隨鼠標(biāo)的小手效果,鼠標(biāo)移在哪里,小手就跟著移向哪里,會(huì)出現(xiàn)手的效果,放在鏈接上的時(shí)候,手會(huì)變化,兩只手很可愛(ài)哦,JS鼠標(biāo)跟隨代碼分享與大家。2013-05-05微信小程序?qū)崿F(xiàn)左側(cè)滑欄過(guò)程解析
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)左側(cè)滑欄過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08JavaScript實(shí)現(xiàn)滑塊驗(yàn)證案例
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)滑塊驗(yàn)證案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01