vue+elementUI 时间范围选择器

Posted miny_simp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue+elementUI 时间范围选择器相关的知识,希望对你有一定的参考价值。

1、引入组件

<div>
  <el-date-picker type="date" placeholder="选择开始日期" :picker-options="pickerOptionsStart" v-model="startTime" @change="startTimeChang"></el-date-picker>
  <el-date-picker type="date" placeholder="选择结束日期" :picker-options="pickerOptionsOver" v-model="endTime" @change="endTimeChang"></el-date-picker>
  <el-button style="margin-left: 10px;" plain size="mini" @click="theWeek">最近一周</el-button>
  <el-button style="margin-left: 10px;" type="primary" @click="searchTime" icon="el-icon-search">查询</el-button
</div>

2、js 定义

data() {
    return {
      startTime: ‘’,
      endTime: ‘’,
      pickerOptionsStart: {
        disabledDate (time) {
          return time.getTime() < 1488297600000 || time.getTime() >= Date.now();
        }
      },
      pickerOptionsOver: {
        disabledDate (time) {
          return time.getTime() < 1488297600000 || time.getTime() >= Date.now();
        }
      }
    };
  }

初始化定义:(最近一周)

 startTime: this.dateFilter(new Date() - 3600 * 1000 * 24 * 7),
 endTime: this.dateFilter(new Date()),

 

3、时间格式化方法,@change的方式取值

    dateFilter:function (input) {
          var d = new Date(input);
          var year = d.getFullYear();
          var month = d.getMonth() <9 ? ‘0‘ + (d.getMonth() + 1) : ‘‘ + (d.getMonth() + 1);
          var day = d.getDate() <10 ? ‘0‘ + d.getDate() : ‘‘ + d.getDate();
          // var hour = d.getHours();
          // var minutes = d.getMinutes();
          // var seconds = d.getSeconds();
          return  year+ ‘-‘ + month + ‘-‘ + day;
    },
    startTimeChang (val) {
      let startTime = this.dateFilter(val);
      this.startTime = startTime;
    },
    endTimeChang (val) {
      let endTime = this.dateFilter(val);
      this.endTime = endTime;
    },

4、查询

  async searchTime() {
      let param = {
        userId: this.$store.state.userInfo.userId,
        startTime: this.startTime,
        endTime: this.endTime
      };
      try {
        const reportData = await getTeamReportList(param);
        if (reportData.result) {
          console.log(reportData);

          this.reportList = reportData.data;
        } else {
          throw new Error(reportData.msg);
        }
      } catch (err) {
        this.$message.error(err.message);
        console.log(‘获取数据失败‘, err);
      }
    }
  }

5、最近一周查询

    async theWeek() {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);

      this.startTime = this.dateFilter(start);
      this.endTime = this.dateFilter(end);
      this.searchTime();
    },
this.dateFilter(start):时间格式化


以上是关于vue+elementUI 时间范围选择器的主要内容,如果未能解决你的问题,请参考以下文章

ElementUI日期选择器时间选择范围限制

vue+ElementUI 日期选择器 获取时间戳

elementui日期选择能支持按旬选择吗?

在vue中从elementui复制日期选择器怎么不显示

vue和elementui怎么做通过时间范围筛选数据??最好有案例

vue中使用elementUI 的级联选择器el-cascader实现动态加载