Vue导出Excel表格并把指定内容除以60和除以3600,保留两位小数 设置默认日期(年月日时分秒)

Vue导出Excel表格并把指定内容除以60和除以3600,保留两位小数 设置默认日期(年月日时分秒),第1张

定义两个单选框


   

单位

创建导出按钮

 
   
      
            导出
      
   
 

创建表格


   
     
        
      

     
        
     

导出

JS

  export default { 
    data() {
      return {
        exportTitle:'',
        tableDataTreeTime: [],
        tableDataTreeTimeExport: [],
        pageCount: 1,
        pageSize: 10,
        selectPageDayList: [],
        selectPageList: [],
        Readytime: '', // 时间月
        value1: [], //时间日
        radio: '1', // 期间
        radiosss: '1', // 单位
        radios: '1',
        timeChange: '1', // 用来判断日期格式
        item: '',
        typeNext: true, // 是否包含当前时间
        exportList: [{
          column: 'loadTimeNew',
          title: '运转时长(时)',
          headerAlign: 'center',
          align: 'center',
          width: '200px'
        }, {
          column: 'shutdownTime',
          title: '异常停止(时)',
          headerAlign: 'center',
          align: 'center',
          width: '200px'
        }],
        searchData: {

          beginTime: "2022-04-07 08:00:00",
          endTime: "2022-04-09 02:00:00",
          id: 0,
          type: 2,
          next: true,
          sort: "ASC",
          sortName: "activation",
        },
        value: '',
        // 排序默认
        tableDataTreeTime: [{
          warehouseName: '1'
        }]
      }
    },
}
 methods: {
      // 设置默认日期
      defaultDateMonth() {
        //获取新的时间(2019.4.12)
        let date = new Date()
        //获取当前时间的年份转为字符串
        let year = date.getFullYear().toString() //'2019'
        //获取月份,由于月份从0开始,此处要加1,判断是否小于10,如果是在字符串前面拼接'0'
        let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : (date.getMonth() + 1)
          .toString() //'04'
        //获取天,判断是否小于10,如果是在字符串前面拼接'0'
        let da = date.getDate() < 10 ? '0' + date.getDate().toString() : date.getDate().toString() //'12'
        //获取时
        let hour = date.getHours() < 10 ? '0' + date.getHours().toString() : date.getHours().toString()
        //获取分
        let minute = date.getMinutes() < 10 ? '0' + data.getMinutes().toString() : date.getMinutes().toString()
        //获取秒
        let second = date.getSeconds() < 10 ? '0' + data.getSeconds().toString() : date.getSeconds().toString()
        //字符串拼接,开始时间,结束时间
        let end = year + '-' + month + '-' + da + ' ' + hour + ':' + minute
        let beg = year + '-' + month + '-01' + ' ' + '08' + ':' + '00'
        this.value1 = [beg, end] //将值设置给插件绑定的数据
        let end1 = year + '-' + month //当天'2019-04-12'
        let beg1 = year + '-' + month //当月第一天'2019-04-01'
        this.Readytime = [beg1, end1] //将值设置给插件绑定的数据
      },

      // 获取时间
      getTime() {
        file.selectTimee().then((response) => {
          const data = response.data;
          if (data.code == "1") {
            this.value1.push(data.data.beginTime, data.data.endTime)
          }
        }).catch((response) => {
          console.log(response)
        })
      },
      //获取时间的默认值
      getDate() {
        let start = this.Fungetdate(0) // 7天前
        let end = this.Fungetdate(0) // 当前时间
        this.dateValue.push(start, end)
        console.log('aaaa', start)
        this.Readytime = this.dateValue
        this.value1 = this.dateValue
        this.dyTime = this.dateValue //  当前时间
      },
      // 点击单选框 月  1
      selLables(val) {
        console.log('月', val)
        this.timeChange = '1'
      },
      // 点击单选框  日 2
      selLable(val) {
        console.log('日', val)
        this.timeChange = '2'
        // console.log(this.value1)
      },

      //点击单选框 时
      selTimeHours() {
        this.exportList[2].title = '运行时长(时)'
        this.exportList[3].title = '异常停止(时)'
        this.excle();
      },
      //点击单选框 分
      selTimeMinutes() {
        this.exportList[2].title = '运行时间(分)'
        this.exportList[3].title = '异常停止(分)'
        this.excle();
      },
      exportClick() {
        this.$refs.exportBox.setExportExcel();
      },

      changeTime(e) {
        this.$forceUpdate()
      },
      search() {
        if (this.radio == '1') {
          this.searchData.beginTime = this.Readytime[0]
          this.searchData.endTime = this.Readytime[1]
        } else {
          this.searchData.beginTime = this.value1[0] + ':00'
          this.searchData.endTime = this.value1[1] + ':00'
        }
        let param = {
          pageNum: 1,
          pageSize: this.pageSize,
        }
        this.searchPage(param)
      },
      searchPage(param) {
        //请求检索条件
        var dataParam = this.searchData;
        var newParam = Object.assign(param, dataParam);
        if (this.radio == 1) {
          kpi.selectPage(newParam).then((response) => {
            const data = response.data;
            if (data.code == "1") {
              this.selectPageDayList = data.data.records;
              this.pageNum = data.data.current;
              this.pageCount = data.data.total;
              this.$refs['pageModel'].setCurrent(this.pageNum);
            }
          }).catch((response) => {
            console.log(response)
          });
        } else {
          kpi.selectPageDay(newParam).then((response) => {
            const data = response.data;
            if (data.code == "1") {
              this.selectPageDayList = data.data.records;
              this.pageNum = data.data.current;
              this.pageCount = data.data.total;
              this.$refs['pageModel'].setCurrent(this.pageNum);
            }
          }).catch((response) => {
            console.log(response)
          });
        }
        this.selectExcel(newParam);
      },
      // 调用Excle接口
      selectExcel(param) {
        if (this.radio == 1) {
          kpi.selectExcel(param).then((response) => {
            const data = response.data;
            if (data.code == "1") {
              this.tableDataTreeTimeExport = data.data;
              this.excle();
            }
          }).catch((response) => {
            console.log(response)
          })
        } else {
          kpi.selectDayExcel(param).then((response) => {
            const data = response.data;
            if (data.code == "1") {
              this.tableDataTreeTimeExport = data.data;
              this.excle();
            }
          }).catch((response) => {
            console.log(response)
          })
         }
        },
        // 循环遍历数组  /3600=时  /60=分(仅限该项目) .toFixed()保留小数  括号内写小数位数
      excle() {
        if (this.radios == 1) {
          for (var i = 0; i < this.tableDataTreeTimeExport.length; i++) {
            this.tableDataTreeTimeExport[i].loadTimeNew =(this.tableDataTreeTimeExport[i].loadTime / 3600).toFixed(2)
            this.tableDataTreeTimeExport[i].shutdownTime =(this.tableDataTreeTimeExport[i].shutdownTime / 3600).toFixed(2)
          }
        } else {
          for (var i = 0; i < this.tableDataTreeTimeExport.length; i++) {
             this.tableDataTreeTimeExport[i].loadTimeNew =(this.tableDataTreeTimeExport[i].loadTime / 60).toFixed(2)
             this.tableDataTreeTimeExport[i].shutdownTime =(this.tableDataTreeTimeExport[i].shutdownTime / 60).toFixed(2)
          }
        }
      },
      newMes(data) {
        this.searchData.type = data.type;
        this.searchData.id = data.id;
        this.search();
      },

      updatatime(time) {
        if (this.radios == '1') {
          return time / 3600000
        } else {
          return time / 60000
        }
      }
    },
    created() {
      // this.selectPage();
      this.defaultDateMonth();
      // this.getTime();
      this.search();
      // this.timer = setInterval(() => {
      //   setTimeout(() => {
      //     this.search()
      //   }, 0)
      // }, 5000)
    },
自用   入行不久  欢迎大佬们吐槽和传授经验  谢谢大家

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1298023.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存