element-plus el-time-picker 时间段选择(可多选)

实现一个如图的时间段选择器

  1. 处理好时间回显逻辑,组件内['',''],后端数据[{startTime:'',endTime:''}]
  2. 处理好加和减的显示逻辑
js 复制代码
<template>
  <div>
    <div v-for="(item, index) in currentChoose" :key="index" class="flex justify-center items-center" :class="index ? 'mt-2': ''">
      <el-time-picker
        v-model="currentChoose[index]"
        v-bind="_options"
        :disabled="_options.disabled"
        @change="handleChange(item, index)"
      />
      <div class="flex ml-2 w-2" v-if="!_options.disabled">
        <el-icon @click="plusTime(item, index)"><Plus /></el-icon>
        <el-icon class="ml-2" v-if="index" @click="minusTime(item, index)"><Minus /></el-icon>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "multipletimepicker",
};
</script>

<script setup>
import { ref, computed, watch } from "vue";
import _ from "lodash";

const emits = defineEmits([
  "update:modelValue"
]);

const props = defineProps({
  disabled: {
    //禁用
    type: Boolean,
    default: false,
  },
  options: {
    type: Object,
    default: () => {},
  },
  modelValue: {
    type: [Array, Object],
    default: () => ([]),
  },
});

// 设置option默认值,如果传入自定义的配置则合并option配置项
const _options = computed(() => {
  const option = {
    'value-format': 'HH:mm',
    'format':"HH:mm",
    'is-range': true
  };
  option.disabled = props.disabled;
  return Object.assign(option, props.options);
});

const currentChoose = ref([]);

const created = () => {
  if (!props.modelValue) {
    return []
  }
  if (props.modelValue instanceof Array) {
    if (props.modelValue.length) {
      currentChoose.value = props.modelValue.map(item => {
        return [item.startTime, item.endTime]
      })
    } else {
      currentChoose.value = [['', '']]
    }
  } else {
    console.log('时间选择插件,非数组格式')
  }
}

const handleChange = () => {
  handleDataEmits()  
}

const minusTime = (item, index) => {
  if (index === 0) {
    return;
  }
  currentChoose.value.splice(index, 1);
  handleDataEmits()
}

const plusTime = (item, index) => {
  currentChoose.value.splice(index + 1, 0, [])
  handleDataEmits()
}

// 统一处理数据回显
const handleDataEmits = () => {
  if (currentChoose.value && currentChoose.value.length) {
    // 将数组处理成后端数据格式,并触发父组件的model更新
    const arrFilter = currentChoose.value.filter(item => item)
    if (!arrFilter.length) {
      emits('update:modelValue', [{startTime: "", endTime: ""}])
      return
    }
    const arr = arrFilter.map(i => {
      return {
        startTime: i[0],
        endTime: i[1]
      }
    })
    emits('update:modelValue', arr)
    return arr
  }
  emits('update:modelValue', [{startTime: "", endTime: ""}])
}

// 数据第一次进入,返显数据
watch(()=>props.modelValue, (val) => {
  created();
}, { immediate: true })
</script>
相关推荐
JosieBook33 分钟前
【SpringBoot】21-Spring Boot中Web页面抽取公共页面的完整实践
前端·spring boot·python
吃饭睡觉打豆豆嘛1 小时前
深入剖析 Promise 实现:从原理到手写完整实现
前端·javascript
前端端1 小时前
claude code 原理分析
前端
GalaxyMeteor1 小时前
Elpis 开发框架搭建第二期 - Webpack5 实现工程化建设
前端
Spider_Man1 小时前
从 “不会迭代” 到 “面试加分”:JS 迭代器现场教学
前端·javascript·面试
我的写法有点潮1 小时前
最全Scss语法,赶紧收藏起来吧
前端·css
小高0071 小时前
🧙‍♂️ 老司机私藏清单:从“记事本”到“旗舰 IDE”,我只装了这 12 个插件
前端·javascript·vue.js
Mo_jon2 小时前
css 遮盖滚动条,鼠标移上显示
前端·css
EveryPossible2 小时前
终止异步操作
前端·javascript·vue.js
Stringzhua2 小时前
setup函数相关【3】
前端·javascript·vue.js