日期切换

javascript 复制代码
组件:
javascript 复制代码
<template>
  <div class="time-picker">
    <el-radio-group size="small" v-model="timeType" @change="changePickerType">
      <el-radio-button label="hour" v-if="isShow">时</el-radio-button>
      <el-radio-button label="day">日</el-radio-button>
      <el-radio-button label="month">月</el-radio-button>
      <el-radio-button label="quarter">季</el-radio-button>
      <el-radio-button label="year">年</el-radio-button>
    </el-radio-group>
    <el-date-picker v-if="timeType != 'year' && timeType != 'quarter'" v-model="timeList" :type="pickerType[timeType].type" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" size="small" style="width: 300px" value-format="timestamp" :clearable="false" :format="pickerType[timeType].format" @change="changeDatePicker" class="select-time" popper-class="time-popper" :default-time="['00:00:00', '23:00:00']">
    </el-date-picker>
    <YearYear1 v-if="timeType == 'year'" style="width:300px" :initYear="dateValue2" @updateTimeRange="updateStatisticYear" />
    <QuarterTime v-if="timeType == 'quarter'" @getQuarter="getQuarter"></QuarterTime>
  </div>
</template>

<script>
import YearYear1 from "@/components/YearYear1";
import QuarterTime from "@/components/QuarterTime";
export default {
  model: {
    prop: "times",
    event: "updatetimes",
  },
  props: {
    times: {
      type: Array,
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  components: {
    YearYear1,
    QuarterTime,
  },
  data() {
    return {
      timeType: "hour",
      timeList: [],
      yearTime: [],
      dateValue2: [],
      pickerType: {
        hour: {
          type: "datetimerange",
          format: "yyyy-MM-dd HH时",
        },
        day: {
          type: "daterange",
        },
        month: {
          type: "monthrange",
        },
        quarter: {
          type: "quarter",
        },
        year: {
          type: "year",
        },
      },
    };
  },
  methods: {
    changePickerType(type) {
      let dayType = {
        month: {
          type: "years",
          count: 1,
        },
        day: {
          type: "days",
          count: 3,
        },
        hour: {
          type: "days",
          count: 3,
        },
        quarter: {
          type: "quarter",
          count: 1,
        },
        year: {
          type: "years",
          count: 2,
        },
      };
      //   var moment = require("moment");
      //   const startDay = moment()
      //     .subtract(dayType[type].count, dayType[type].type)
      //     .format("YYYY-MM-DD HH:00:00");
      //   const endDay = moment().format("YYYY-MM-DD HH:00:00");
      //   this.timeList = [
      //     new Date(startDay).getTime(),
      //     new Date(endDay).getTime(),
      //   ];
      this.timeList = [];
      this.yearTime = this.timeList;
      //   this.$emit("updatetimes", this.timeList);
    },
    isNull(value) {
      if (value) {
        return false;
      }
      return true;
    },
    //获取季度子组件传回的数据
    getQuarter(val) {
      console.log("季节:", val);
    },
    // 选择年
    updateStatisticYear(val) {
      console.log("年", val);
    },
    changeDatePicker(e) {
      console.log("选择:", e);
      this.$emit("updatetimes", e);
    },
  },
};
</script>

<style lang="scss" scoped>
.time-picker {
  display: flex;
}

.time-popper {
  .el-time-spinner {
    display: flex;
    justify-content: center;

    & > :nth-child(2) {
      display: none;
    }
  }

  .el-picker-panel__footer {
    & > :first-child {
      display: none;
    }
  }
}
.el-radio-group {
  margin-right: 10px;
}
.year-picker {
  line-height: 28px;
}
::v-deep .el-radio-button--small .el-radio-button__inner {
  height: 36px;
  line-height: 18px;
}
::v-deep .el-range-editor--small.el-input__inner {
  height: 36px;
  line-height: 18px;
}
::v-deep .el-range-editor--small .el-range__icon {
  line-height: 30px;
}
</style>
javascript 复制代码
里面的年(YearYear1)和季度(QuarterTime)组件是前面分享的文章:
标题:年至年的选择仿elementui的样式、仿真elementUI的时间选择的季度选择
引入即可
javascript 复制代码
使用:
javascript 复制代码
<SelectTimePicker :isShow="false" @updatetimes="updatetimes"></SelectTimePicker>

import SelectTimePicker from "@/components/SelectTimePicker/index.vue";
components: { SelectTimePicker },

updatetimes(val) {
 console.log("点击:", val);
},
相关推荐
懒大王爱吃狼1 小时前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
待磨的钝刨2 小时前
【格式化查看JSON文件】coco的json文件内容都在一行如何按照json格式查看
开发语言·javascript·json
逐·風5 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
Devil枫5 小时前
Vue 3 单元测试与E2E测试
前端·vue.js·单元测试
尚梦6 小时前
uni-app 封装刘海状态栏(适用小程序, h5, 头条小程序)
前端·小程序·uni-app
GIS程序媛—椰子6 小时前
【Vue 全家桶】6、vue-router 路由(更新中)
前端·vue.js
前端青山7 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
毕业设计制作和分享7 小时前
ssm《数据库系统原理》课程平台的设计与实现+vue
前端·数据库·vue.js·oracle·mybatis
程序媛小果7 小时前
基于java+SpringBoot+Vue的旅游管理系统设计与实现
java·vue.js·spring boot
从兄8 小时前
vue 使用docx-preview 预览替换文档内的特定变量
javascript·vue.js·ecmascript