首页 > 其他分享 >el-date-picker切换的坑

el-date-picker切换的坑

时间:2024-08-05 10:42:21浏览次数:14  
标签:picker el paginationConfig Date queryAllTime date new timeArr type

el-date-picker 日期组件切换的坑

img

img

问题描述:

问题1. 根据类型切换需要的时间选择器组件(搜索栏是一个form组件,里面有input、select、picker等,在使用的页面里传入指定的类型就能展示)第一次按顺序切换日常发,周常发,月常发是没问题的,但是当第二次选择周常发时,选择器无变化,然后再一次切换会出现上一次应该出现的选择器,并且会有 问题2. 选择器错位的现象

解决:

传给后端的值为 startTIme、endTime但实际选择器保存的是一个未格式化的时间,问题1:应该在每次切换select值后删除日期的值(为了处理后端需要的数据格式因此日 周 月保存的时间值是同一个)问题2:在form组件中给每个picker添加key

完整的formComponent组件

<!--
 * @Description: form组件 页面
 * @Use: type可选值:'',numberInput,select,dept,radio,switch,date,daterange,dateNoTime,datetimerange
 * @Author: mhf
 * @Date: 2023-06-29 17:08:33
-->
<!-- eslint-disable vue/no-mutating-props -->
<template>
  <div class="search-component">
    <!-- 表单区域 -->
    <el-form ref="formRef" :model="form" :inline="inline" :rules="formRules">
      <el-form-item
        v-for="(item, index) in formLabel"
        :key="index"
        :label="item.label + ' :'"
        :prop="item.value"
      >
        <el-input
          v-if="!item.type"
          v-model.trim="form[item.value]"
          :placeholder="'请输入' + item.label"
          size="small"
          :type="item.type ? item.type : 'text'"
        />
        <el-input
          v-if="item.type === 'numberInput'"
          v-model.number="form[item.value]"
          :placeholder="'请输入' + item.label"
          size="small"
        />
        <el-select
          v-if="item.type === 'select'"
          v-model.trim="form[item.value]"
          filterable
          :placeholder="'请选择' + item.label"
          clearable
          size="small"
        >
          <el-option
            v-for="(items, index) in item.opts"
            :key="index"
            :label="items[item.optLabel]"
            :value="items[item.optValue]"
            @click.native="selectClick(item, items)"
          />
        </el-select>
        <treeselect
          v-if="item.type === 'dept'"
          v-model="form[item.value]"
          class="treeBox"
          :options="item.opts"
          :show-count="true"
          :placeholder="'请选择' + item.label"
        />
        <template v-if="item.type === 'radio'">
          <el-radio
            v-for="items in item.opts"
            :key="items.value"
            v-model="form[item.value]"
            :label="items.value"
            >{{ items.label }}
          </el-radio>
        </template>
        <el-switch v-if="item.type === 'switch'" v-model="form[item.value]" />
        <el-date-picker
          v-if="item.type === 'date'"
          key="date"
          v-model="form[item.value]"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
        <el-date-picker
          v-if="item.type === 'dateNoTime'"
          key="dateNoTime"
          v-model="form[item.value]"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
        <el-date-picker
          v-if="item.type === 'datetimerange'"
          key="datetimerange"
          v-model="form[item.value]"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
        <el-date-picker
          v-if="item.type === 'daterange'"
          key="daterange"
          v-model="form[item.value]"
          type="daterange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          value-format="yyyy-MM-dd"
          @change="daterangeChange"
        />
        <el-date-picker
          v-if="item.type === 'week'"
          key="week"
          v-model="form[item.value]"
          type="week"
          format="yyyy 第 WW 周"
          placeholder="选择周"
          @change="weekChange"
        >
        </el-date-picker>
        <el-date-picker
          v-if="item.type === 'month'"
          key="month"
          v-model="form[item.value]"
          type="month"
          placeholder="选择月"
          value-format="yyyy-MM-dd"
          @change="monthChange"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <slot name="formSlot" />
      </el-form-item>
      <div v-if="showOperationBtn" class="right-handle">
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleSearch"
            >查询
          </el-button>
          <el-button
            size="small"
            icon="el-icon-refresh"
            @click="handleResetFrom"
            >重置
          </el-button>
        </el-form-item>
        <el-form-item />
        <el-form-item>
          <slot name="handleSlot" />
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SearchComponent",
  props: {
    inline: {
      type: Boolean,
      default: true,
    },
    // eslint-disable-next-line vue/require-default-prop
    form: {
      type: Object,
      required: true,
    },
    // eslint-disable-next-line vue/require-default-prop
    formLabel: {
      type: Array,
      required: true,
    },
    // eslint-disable-next-line vue/require-default-prop
    formRules: {
      type: Object,
      default: () => {
        return {};
      },
    },
    labelWidth: {
      type: Number,
      default: 90,
    },
    showOperationBtn: {
      type: Boolean,
      default: true,
    },
  },
  // 用来以对象方式存放数据
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const startTime = new Date(new Date().setHours(0, 0, 0));
              const endTime = new Date(new Date().setHours(23, 59, 59));
              // const endTime = new Date(); //
              picker.$emit("pick", [startTime, endTime]);
            },
          },
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },

  created() {},
  mounted() {},
  methods: {
    selectClick(item, items) {
      this.$emit("selectChange", { item, items });
    },
    handleSearch() {
      this.$emit("handleSearch", this.form);
    },
    handleResetFrom() {
      this.$emit("handleResetFrom");
    },
    daterangeChange(val) {
      this.$emit("daterangeChange", val);
    },
    weekChange(val) {
      let timeArr = [];
      if (val) {
        let startTime = new Date(val.getTime()); //开始时间
        let endTime = new Date(val.getTime() + 24 * 60 * 60 * 1000 * 6); //结束时间
        timeArr = [
          startTime.toISOString().slice(0, 10),
          endTime.toISOString().slice(0, 10),
        ];
      }
      this.$emit("getTimeArr", timeArr);
    },
    monthChange(val) {
      const [year, month, day] = val.split("-");
      const endDate = new Date(year, month, 0).getDate();
      let timeArr = [`${year}-${month}-${day}`, `${year}-${month}-${endDate}`];
      this.$emit("getMonthArr", timeArr);
    },
  },
};
</script>

<style lang="scss" scoped>
.search-component {
  .right-handle {
    display: inline-block;
    margin-left: 30px;
  }
}
.treeBox {
  width: 242px;
}
</style>

使用的完整的页面

<!--
 * @Description: 常发拥堵路段 页面
 * @Author: mhf
 * @Date: 2023-09-14 15:46:36
-->
<template>
  <div>
    <newPageCompYt
      ref="newPageCompYt"
      idName="id"
      :formLabel="formLabel"
      :btnList="btnList"
      :tableData="tableData"
      :tableConfig="tableConfig"
      :tableDataColumn="tableDataColumn"
      :total="total"
      :paginationConfig.sync="paginationConfig"
      :pageSizes="pageSizes"
      :exportTypeAndUrl="exportTypeAndUrl"
      :customObj="customObj"
      @on-response="getBtnType"
      @resetForm="resetForm"
      @getTableData="getTableData"
      @daterangeChange="daterangeChange"
      @getTimeRange="getTimeRange"
      @getMonthArr="getMonthArr"
      @formSelectChange="formSelectChange"
    >
      <template slot="roadType" slot-scope="scope">
        <dict-tag
          :options="dict.type.traffic_road_type"
          :value="scope.row.roadType"
        />
      </template>

      <template slot="roadDirection" slot-scope="scope">
        <dict-tag
          :options="dict.type.traffic_road_direction"
          :value="scope.row.roadDirection"
        />
      </template>
    </newPageCompYt>
  </div>
</template>

<script>
import { getLastNumDay } from "@/utils/publicFun";
import { getRoadBlockList } from "@/api/statisticAnalysis/statisticAnalysis";

export default {
  name: "roadCongested",
  components: {},
  props: {},
  dicts: ["traffic_often_type", "traffic_road_type", "traffic_road_direction"],
  computed: {
    customObj() {
      let flag = false;
      if (this.paginationConfig.queryAllTime) {
        flag =
          this.paginationConfig.queryAllTime.length > 0 ||
          this.paginationConfig.queryAllTime !== [];
      } else {
        flag = false;
      }

      return {
        startTime: flag
          ? this.paginationConfig.queryAllTime[0] + " 00:00:00"
          : null,
        endTime: flag
          ? this.paginationConfig.queryAllTime[1] + " 23:59:59"
          : null,
        oftenType: this.paginationConfig.oftenType,
        pageNum: this.paginationConfig.pageNum,
        pageSize: this.paginationConfig.pageSize,
      };
    },
  },
  data() {
    return {
      formLabel: [
        {
          label: "常发类型",
          value: "oftenType",
          type: "select",
          opts: [],
          dict: "traffic_often_type",
          optLabel: "dictLabel",
          optValue: "dictValue",
        },
        {
          label: "日期",
          value: "queryAllTime",
          type: "daterange",
        },
      ],
      btnList: [
        {
          name: "导出",
          icon: "iconfont if-daochu",
          color: "#1492FF",
          hasPermi: "system:user:resetPwd",
        },
      ],
      tableData: [],
      tableConfig: {
        isLoading: false,
      },
      tableDataColumn: [
        { name: "序号", value: "index" },
        { name: "路段名称", value: "roadName" },
        { name: "道路方向", value: "roadDirection", isSlot: true },
        { name: "道路等级", value: "roadType", isSlot: true },
      ],
      total: 0,
      paginationConfig: {
        pageNum: 1,
        pageSize: 10,
      },
      pageSizes: [5, 10, 15, 20],
      exportTypeAndUrl: {
        isCustom: true,
        exportType: "trafficTrend",
        exportUrl: "/trafficRun/dataReport/congestedRoad",
      },
    };
  },
  methods: {
    getTableData() {
      this.tableConfig.isLoading = true;
      const { queryAllTime, ...obj } = this.paginationConfig;
      getRoadBlockList(obj).then((response) => {
        this.tableData = response.data.rows !== null ? response.data.rows : [];
        this.total = response.data.total;
        this.tableConfig.isLoading = false;
      });
    },

    getSETime(timeArr) {
      if (timeArr.length > 0) {
        this.$set(this.paginationConfig, "startTime", timeArr[0] + " 00:00:00");
        this.$set(this.paginationConfig, "endTime", timeArr[1] + " 23:59:59");
      } else {
        this.$set(this.paginationConfig, "startTime", null);
        this.$set(this.paginationConfig, "endTime", null);
      }
    },

    daterangeChange(timeRange) {
      this.getSETime(timeRange);
    },

    getTimeRange(timeRange) {
      this.getSETime(timeRange);
    },

    getMonthArr(timeRange) {
      this.getSETime(timeRange);
    },

    weekChange(val) {
      // let timeArr = []
      // if (val) {
      //   let startTime = new Date(val.getTime()); //开始时间
      //   let endTime = new Date(val.getTime() + (24 * 60 * 60 * 1000) * 6); //结束时间
      //   timeArr = [startTime.toISOString().slice(0, 10), endTime.toISOString().slice(0, 10)];
      // }
    },

    formSelectChange(obj) {
      if (obj.item.label === "常发类型") {
        let arr = [
          {
            label: "日期",
            value: "queryAllTime",
            type: "daterange",
          },
          {
            label: "日期",
            value: "queryAllTime",
            type: "week",
          },
          {
            label: "日期",
            value: "queryAllTime",
            type: "month",
          },
        ];
        this.formLabel[1] = arr[obj.items.dictValue - 1]
        this.$set(this.paginationConfig, 'queryAllTime', null)
        delete this.paginationConfig.queryAllTime  // 主要是这步 清空原先保存的值(问题1)
        this.getSETime([])
      }
    },

    getBtnType(type) {
      console.log(type);
    },

    resetForm() {
      let timeArr = getLastNumDay(30, false);
      this.$set(this.paginationConfig, "queryAllTime", timeArr);
      this.$set(this.paginationConfig, "startTime", timeArr[0]);
      this.$set(this.paginationConfig, "endTime", timeArr[1]);
      this.$set(this.paginationConfig, "oftenType", "1");
      this.formLabel[1] = {
        label: "日期",
        value: "queryAllTime",
        type: "daterange",
      };
    },
  },

  created() {
    this.resetForm();
  },

  mounted() {},
};
</script>

<style lang="scss" scoped></style>

问题2 看此段即可

  <el-date-picker
          v-if="item.type === 'datetimerange'"
          key="datetimerange"
          v-model="form[item.value]"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          value-format="yyyy-MM-dd HH:mm:ss"
        />

标签:picker,el,paginationConfig,Date,queryAllTime,date,new,timeArr,type
From: https://www.cnblogs.com/ProgrammerMao-001/p/18342761

相关文章

  • div中添加el-loading(局部loading的使用)
    div中添加el-loading(局部loading的使用)效果:在div中实现el-loadinghttps://img-blog.csdnimg.cn/c2870e74bd344b06ad1ccb0844b8e8ce.png<divclass="content-main">{{hotList}}</div>getHotList(columnType){this.$nextTic......
  • css实现el-select右侧箭头向上向下动画
    css实现el-select右侧箭头向上向下动画<divclass="chooseTag-tip"><i:class="['el-icon-arrow-up',tipFlag?'chooseTag-tip-up':'chooseTag-tip-down']"></i></div><script>tipFla......
  • Delphi 快捷键
    分类快捷键解释备注组件设计类Escape选择当前组件容器 Shift+Click选择多个组件;选择窗体 Tab选择下一个组件 Shift+Tab选择上一个组件 方向键选择此方向的下一个组件 Ctrl+方向键将所选组件的位置移动1个像素 Shift+......
  • Delphi ExtractFilePath
    1、取路径1、取路径。 1.1 ExtractFilePath之类的返回路径。原文链接(https://www.cnblogs.com/ZhouXiHong/archive/2007/01/30/634210.html)ExtractFileDrive:返回完整文件名中的驱动器,如"C:"ExtractFilePath:返回完整文件名中的路径,最后带“/”,如"C:\test\"ExtractFileDir:返......
  • excel函数的学习
    1、学习excelSUM :求和函数AVERAGE:平均值函数IFROUNDMAXMININTVLOOKUPSUMIFSUMSIFCOUNTCOUNTIFNOWTODAYMIDPHONETICLENRIGHT二、实操(1)SUM :求和函数 条件判断函数四舍五入函数最大值函数最小值函数数据取整函数条件查找函数按条件求和函数按多个条件求和函数统计数字个......
  • Delphi 线程
    不是原创,只是看到好的内容复制了保存下来,留着学习。 CreadteThred参考,同步参考,WaitForSingleObject参考,互斥参考, 一、在Delphi中使用多线程有两种方法:调用API、使用TThread类;使用API的代码更简单.1、调用API:CreateThread()functionCreateThread( lpThr......
  • 使用 Sobel 算子理解梯度组合策略输出
    我尝试实现一个SobelEdge检测器,如下所示:defsobel(img):img=cv2.GaussianBlur(img,(5,5),0)filter_x=np.array([[-1,0,1],[-2,0,2],[-1,0,1]])filter_y=np.array([[-1,-2,-1],......
  • 从零体检一个魔塔社区模型(modelscope)最简单demo
    从社区拿一个模型,比如以下这个链接https://www.modelscope.cn/models/iic/cv_mobilenet-v2_bad-image-detecting它的代码样例如下frommodelscope.pipelinesimportpipelinefrommodelscope.utils.constantimportTasksfrommodelscope.outputsimportOutputKeysimg='......
  • “命令行利器:sort、uniq、date、ntpdate详解与实战“
    当今操作系统中的命令行工具不仅是管理和调试系统的利器,也是程序员和系统管理员的重要工具。在Unix和类Unix系统中,sort、uniq、date和ntpdate是几个常用的命令,它们各自拥有独特的功能,可以在日常工作中极大地提高效率。本文将深入探讨这些命令的用法和实际应用。1. sort命令s......
  • TELNET命令的使用技巧及其在网络故障排查中的作用
    TELNET命令的使用技巧及其在网络故障排查中的作用大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!TELNET是一种简单的网络协议和工具,用于远程访问计算机系统。在网络故障排查中,TELNET可以帮助我们验证网络连接、测试端口、诊断服务问题等。本文将探......