element 季度选择器组件

效果图:

在这里插入图片描述

回传给父组件的值:

在这里插入图片描述

季度选择器组件代码:

<template>
  <el-form>
    <el-form-item>
      <mark
        class="mark"
        v-show="showSeason"
        @click.stop="showSeason = false"
      ></mark>
      <el-input
        placeholder="请选择季度"
        readonly
        v-model="showValue"
        size="small"
        @focus="showSeason = true"
      >
        <i slot="prefix" class="el-input__icon el-icon-date"></i>
      </el-input>
      <el-card class="box-card" v-show="showSeason">
        <div slot="header" class="year-btn">
          <button
            type="button"
            aria-label="前一年"
            class="el-picker-panel__icon-btn el-date-picker__prev-btn el-icon-d-arrow-left"
            @click="prev"
          ></button>
          <span role="button">{
  { year }}年</span>
          <button
            type="button"
            aria-label="后一年"
            @click="next"
            class="el-picker-panel__icon-btn el-date-picker__next-btn el-icon-d-arrow-right"
          ></button>
        </div>
        <div class="season-list">
          <div
            v-for="btn in buttonList"
            :key="btn"
            :class="{ 'season-item': true, 'is-active': showValue === `${year}年第${['', '一', '二', '三', '四'][btn]}季度` }"
            @click="selectSeason(1)"
          >
          {
  { `第${['', '一', '二', '三', '四'][btn]}季度` }}
          </div>
        </div>
      </el-card>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: 'CustomQuarterPicker',
  props: {
    defaultValue: {
      default: '', //  '2024年第二季度'
      type: String,
    },
  },

  data() {
    this.buttonList = [1, 2, 3, 4]
    return {
      showSeason: false,
      season: '',
      year: new Date().getFullYear(),
      showValue: '',
    }
  },

  created() {},
  watch: {
    defaultValue: {
      handler(value) {
        if (value) {
          this.year = value.substr(0, 4)
          this.showValue = value
        }
      },
      immediate: true,
    },
  },
  methods: {
    prev() {
      this.year = +this.year - 1
    },
    next() {
      this.year = +this.year + 1
    },
    selectSeason(num) {
      this.season = num
      this.showSeason = false
      this.showValue = `${this.year}年第${['', '一', '二', '三', '四'][num]}季度`
      this.sendMsg()
    },

    sendMsg() {
      const dateArr = [
        ['01-01', '03-31'],
        ['04-01', '06-30'],
        ['07-01', '09-30'],
        ['10-01', '12-31'],
      ]
      const result = {
        value: this.showValue,
        year: this.year,
        season: this.season,
        dateRange: [
          `${this.year}-${dateArr[this.season - 1][0]}`,
          `${this.year}-${dateArr[this.season - 1][1]}`,
        ],
        dateTimeRange: [
          `${this.year}-${dateArr[this.season - 1][0]} 00:00:00`,
          `${this.year}-${dateArr[this.season - 1][1]} 23:59:59`,
        ],
      }
      this.$emit('func', result)
    },
  },
}
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 0;
  height: 32px;
}
::v-deep .el-form-item__content {
  height: 32px;
  line-height: 32px;
}
.box-card {
  width: 322px;
  padding: 0;
  margin-top: 10px;
  position: absolute;
  z-index: 9999;
}
::v-deep .el-card__header {
  height: 30px;
  padding: 0 0 12px 0;
  box-sizing: content-box;
  margin: 12px 12px 0 12px;
}
::v-deep .el-card__body {
  padding: 10px 0;
}
.el-input {
  width: 220px !important;
  height: 32px;
}
.mark {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0);
  z-index: 999;
}

.year-btn {
  text-align: center;
  padding: 0;
}

.season-list {
  .season-item {
    display: inline-block;
    width: 50%;
    color: #606266;
    text-align: center;
    cursor: pointer;
    &:hover {
      color: #409eff;
    }
    &.is-active {
      color: #409eff;
      border-color: rgba(255, 255, 255, 0); // 隐藏边框
    }
  }
}
</style>

父组件使用案例

            <customQuarterPicker v-show="dateType === 'quarter'" @func="getMsgQuarter" :defaultValue="dateValueQuarter" />
            
		    getMsgQuarter(val) {
		      console.log('val----', val)
		      this.dateValueQuarter = val?.value || ''
		    },

相关推荐

  1. vue element-ui 菜单管理使用图标选择组件

    2024-02-23 03:34:03       45 阅读
  2. vue element plus Select 选择

    2024-02-23 03:34:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-23 03:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-23 03:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-23 03:34:03       82 阅读
  4. Python语言-面向对象

    2024-02-23 03:34:03       91 阅读

热门阅读

  1. golang GOPATH 和GOROOT

    2024-02-23 03:34:03       53 阅读
  2. 【动态规划题目讲解】洛谷P8392 Uplifting Excursion

    2024-02-23 03:34:03       53 阅读
  3. 函数——递归4(c++)

    2024-02-23 03:34:03       50 阅读
  4. Factory Method

    2024-02-23 03:34:03       51 阅读
  5. TCP协议

    TCP协议

    2024-02-23 03:34:03      39 阅读
  6. springboot使用PageHelper

    2024-02-23 03:34:03       58 阅读
  7. 分享一个治疗神经痛的药方

    2024-02-23 03:34:03       47 阅读
  8. python子域名收集工具

    2024-02-23 03:34:03       47 阅读
  9. QT TCP通讯客户端与服务端

    2024-02-23 03:34:03       51 阅读
  10. PTA笔记

    2024-02-23 03:34:03       54 阅读
  11. Python系列(19)—— 条件语句

    2024-02-23 03:34:03       44 阅读