element时间插件,选择中文显示中文

页面效果:
在这里插入图片描述
html:

 <div class="block" :class="{ disableText: showShortcut }">
          <el-date-picker
            v-model="timests"
            ref="datepicker"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            clearable
            :default-time="['00:00:00', '23:59:59']"
            @change="queryFn"
          >
          </el-date-picker>
          <div class="show-shortcut-text" v-if="showShortcut">
            {{ pickUnit }}
          </div>
        </div>

css:

  .disableText {
    width: 200px !important;
    .el-date-editor {
      width: 200px !important;
    }
    .el-range-separator,
    .el-range-input {
      color: #fafafa;
    }
  }

js:
在这里插入图片描述

  pickerOptions() {
      let that = this;
      return {
        format: "HH:mm:ss.SSS",
        shortcuts: [
          {
            text: "最近15分钟",
            class: "active",
            onClick(picker) {
              that.pickUnit = "最近15分钟";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 60 * 1000 * 15);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "今天",
            onClick(picker) {
              that.pickUnit = "今天";
              that.shortClick = true;
              const start = new Date();
              const end = new Date();
              const startTime = moment(moment().startOf("day").valueOf());
              const endTime = moment(moment().endOf("day").valueOf());
              start.setTime(startTime);
              end.setTime(endTime);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "本周",
            onClick(picker) {
              that.pickUnit = "本周";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近30分钟",
            onClick(picker) {
              that.pickUnit = "最近30分钟";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 60 * 1000 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个小时",
            onClick(picker) {
              that.pickUnit = "最近一个小时";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 60 * 1000 * 60);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近24小时",
            onClick(picker) {
              that.pickUnit = "最近24小时";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", [start, end]);
            },
          },

          {
            text: "最近7天",
            onClick(picker) {
              that.pickUnit = "最近7天";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近30天",
            onClick(picker) {
              that.pickUnit = "最近30天";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近90天",
            onClick(picker) {
              that.pickUnit = "最近90天";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一年",
            onClick(picker) {
              that.pickUnit = "最近一年";
              that.shortClick = true;
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      };
    },

再加个监听事件:

 watch: {
    timests() {
      console.log("this.shortcuts", this.shortClick);
      let that = this;
      this.$nextTick(() => {
        let list = document.getElementsByClassName("el-picker-panel__shortcut");
        console.log("list", list);
        let index = -1;
        if (list) {
          for (let i = 0; i < list.length; i++) {
            list[i].className = "el-picker-panel__shortcut";
          }
          if (this.shortClick == true && that.pickUnit) {
            this.showShortcut = true;
            for (let i = 0; i < list.length; i++) {
              if (list[i].innerText === that.pickUnit) index = i;
              else {
                let text = list[i].innerText.replace(/[\r\n]/g, "");
                if (text == that.pickUnit) index = i;
              }
            }
            if (index > -1)
              list[index].className =
                "el-picker-panel__shortcut active_shortcut";
          } else {
            this.showShortcut = false;
          }
        }
        this.shortClick = false;
      });

      console.log("timests", this.timests, this.pickUnit);
    },
  },
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值