vue表单搜索条件时间默认值

84 阅读1分钟
  // 格式化时间
 
 export function getNowFormatDate(date) {
      var myyear = date.getFullYear();
      var mymonth = date.getMonth() + 1;
      var myweekday = date.getDate();
      if (mymonth < 10) {
        mymonth = "0" + mymonth;
      }
      if (myweekday < 10) {
        myweekday = "0" + myweekday;
      }
      return myyear + "-" + mymonth + "-" + myweekday;
    }
    /** 

页面引入

import { getNowFormatDate } from '@/utils/index'


 created() {
 this.getdatatime()
 this.getList();
 },


此方法在你获取接口方法之前使用,如果时created你放在接口名之前,如上,或是你在mounted中调用getdatatime方法都是可以的。
开始时间时开始时间7天前,结束时间时当前时间
 getdatatime() {//默认显示今天
 const that = this;
 const end = new Date();
 const start = new Date();
 start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)//默认开始时间7天前
 this.queryParams.endTime = getNowFormatDate(end);
 this.queryParams.startTime = getNowFormatDate(start);
},
 根据业务,页面清空的时候无论搜索时间有没有改变,都显示初始值,所以在重置的时候,在重新调用这个日期时间方法
  /** 重置按钮操作 */
         resetQuery() {
             this.resetForm("queryForm");
             this.queryParams.eventType = '',
                 this.queryParams.userName = '',
                 this.getdatatime()
             this.handleQuery();
         },