element-ui中日期区间组件

elementui中日期组件使用,最长只能选择3个月,不限制禁用日期

描述

点击【确定】按钮进行验证,点击【清空】按钮,清空输入框中的数据,
时间范围不能超过3个月,并添加快捷选择今天、最近一周、最近一月、最近3个月,以下为代码。

tips:1、点击确定按钮之前,点击非组件table,可以收起日期选择框。
2、点击确定按钮之后,只要不满足条件,点击任何区域都不会收起日期选择组件,除非关闭浏览器。
3、如果要完善第一条,点击确定按钮之前,只要不满足条件,点击任何区域都不会收起日期选择组件,除非关闭浏览器这一操作方法,日期组件上添加change方法(没有尝试过,用change方法和用blur方法的区别是,blur方法满足第二条,用change方法的话,点击确定按钮会校验一次,再次点击确定按钮会校验通过,对于我的需求有bug,所以用了blur方法,有兴趣的朋友可以尝试一下两个方法,本人比较懒不想尝试)

时间组件代码

 <el-date-picker
              ref="datePickRef"
              v-model="searchForm.queryTime"
              clearable
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm"
              format="yyyy-MM-dd HH:mm"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :validate-event="true"
              @blur="handleTimeRange"
            />

pickerOptions放在data中

            pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const startTime = _this.$moment().format('YYYY-MM-DD 00:00');
            const endTime = _this.$moment().format('YYYY-MM-DD HH:mm');
            _this.searchForm.queryTime = [startTime, endTime];
            _this.$refs.datePickRef.handleClose();
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = _this.$moment()._d;
            const start = new Date();
            const time1 = _this.$moment(end).subtract(7, 'days').unix() * 1000;
            start.setTime(time1);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = _this.$moment()._d;
            const start = new Date();
            const time1 = _this.$moment(start).subtract(1, 'months').unix() * 1000;
            start.setTime(time1);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = _this.$moment()._d;
            const start = new Date();
            const time1 = _this.$moment(start).subtract(3, 'months').unix() * 1000;
            start.setTime(time1);
            picker.$emit('pick', [start, end]);
          }
        }]
    }

handleTimeRange方法放在methods中

// 判断开始时间和结束时间的区间长度
    handleTimeRange() {
      if (!this.searchForm.queryTime) {
        this.searchForm.queryTime = [];
      } else {
        const startTime = this.$moment(this.searchForm.queryTime[0]);
        const endTime = this.$moment(this.searchForm.queryTime[1]);
        const rangeMonth = endTime.diff(startTime, 'month', true);
        if (rangeMonth > 3) {
          this.$message.error('时间范围,时长必须小于等于3个月!');
          this.$refs.datePickRef.focus();
        } else {
          this.$refs.datePickRef.handleClose();
        }
      }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*分页组件*/ <template> <div class="pagination"> <ul> <li v-if="showPrevMore" @click="handleLessPage(1)">...</li> <li v-for="num in pageList" :class="{ active: num === currentPage }" @click="handleChangePage(num)">{{ num }}</li> <li v-if="showNextMore" @click="handleMorePage(1)">...</li> </ul> <div v-if="showPageSizeSelector" class="page-size-selector"> <span>每页显示</span> <select v-model="pageSize" @change="handleChangePageSize"> <option v-for="size in pageSizeOpts" :key="size" :value="size">{{ size }} 条</option> </select> </div> </div> </template> <script> export default { name: 'Pagination', props: { // 分页的总条数 total: { type: Number, default: 0 }, // 每页显示的条数 pageSize: { type: Number, default: 10 }, // 分页连续页码数 pagerCount: { type: Number, validator(value) { return value % 2 === 1 && value >= 5 && value <= 21 }, default: 7 }, // 当前页码,支持 .sync 修饰符 currentPage: { type: Number, default: 1 }, // layout,支持结构自定义 layout: { type: String, default: 'total, sizes, prev, pager, next, jumper' }, // 可选每页显示条数 pageSizes: { type: Array, default: () => [10, 20, 30, 40, 50, 100] }, // 是否可以通过点击页码改变当前页码 pageChange: { type: Boolean, default: true }, // 最多显示的页码数,当总页数超过该值时会折叠 maxPagers: { type: Number, default: 8 } }, computed: { // 缓存所有的页码 pagers() { let currentPage = Number(this.currentPage) const pageCount = this.pageCount const pagerCount = this.pagerCount const maxPagers = this.maxPagers // 如果总页数小于最多显示页码数,则全部显示 if (pageCount <= maxPagers) { const arr = range(1, pageCount) return arr } let left = Math.floor(pagerCount / 2) let right = pagerCount - left - 1 if (currentPage <= left) { right = pagerCount - currentPage - 1 return range(1, pagerCount) } if (currentPage > pageCount - right) { left = pagerCount - (pageCount - currentPage) - 1 return range(pageCount - pagerCount + 1, pageCount) } return range(currentPage - left, currentPage + right) }, // 是否显示省略号(后省略) showPrevMore() { return this.pagers[0] > 1 }, // 是否显示省略号(前省略) showNextMore() { const pageCount = this.pageCount const lastPager = this.pagers[this.pagers.length - 1] return pageCount > this.maxPagers && lastPager < pageCount }, // 显示哪些元素 showItems() { const items = this.layout.split(',') let result = [] items.forEach((item) => { item = item.trim() switch (item) { case 'total': result.push({ type: 'total', text: `共${this.total}条` }) break case 'prev': result.push({ type: 'prev', text: '上一页' }) break case 'next': result.push({ type: 'next', text: '下一页' }) break case 'jumper': result.push({ type: 'jumper' }) break case 'sizes': result.push({ type: 'sizes', pageSizes: this.pageSizes }) break case 'pager': result.push({ type: 'pagers', pagers: this.pagers }) break } }) return result }, // 总页数 pageCount() { return Math.ceil(this.total / this.pageSize) }, // 可选的每页显示条数 pageSizeOpts() { return this.pageSizes.map((item) => { return Number(item) }) } }, methods: { handleChangePage(p) { if (p === this.currentPage || !this.pageChange) { return } this.currentPage = p this.$emit('update:currentPage', p) this.$emit('change', p) }, handleLessPage(lessPageCount) { this.handleChangePage(this.currentPage - lessPageCount) }, handleMorePage(morePageCount) { this.handleChangePage(this.currentPage + morePageCount) }, handleChangePageSize(event) { const pageSize = Number(event.target.value) this.pageSize = pageSize this.$emit('update:pageSize', pageSize) this.$emit('size-change', pageSize) this.handleChangePage(1) } } } // 获取一定区间内的数组 function range(left, right) { const arr = [] for (let i = left; i <= right; i++) { arr.push(i) } return arr } </script> <style scoped> .pagination { display: flex; justify-content: space-between; align-items: center; margin-top: 20px; margin-bottom: 20px; font-size: 14px; } ul { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; list-style-type: none; } li { margin-right: 8px; padding: 0 8px; border-radius: 2px; height: 28px; line-height: 28px; color: #666; cursor: pointer; &:hover { color: #409EFF; background-color: #ecf5ff; } } li.active { color: #409EFF !important; background-color: #ecf5ff; } /* 显示每页显示条数的选择器 */ .page-size-selector { display: flex; align-items: center; } </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值