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>
相关推荐
the_flash11 分钟前
如何利用SASS中的颜色函数来简化开发
前端·css
打野赵怀真14 分钟前
Electron如何进行网络状态检测?
前端·javascript
蓝桉柒714 分钟前
安装Webpack并创建vue项目
前端·vue.js·webpack
打野赵怀真15 分钟前
请讲讲Electron的Main进程和Renderer进程
前端·javascript
姚生16 分钟前
CSS 中 margin 的注意事项与常见问题
前端·css
小小宇宙中微子16 分钟前
优秀的 Vue.js 入门开源项目推荐
前端·vue.js·开源
谦谦橘子16 分钟前
手写single-spa,理解微前端原理
前端·javascript
知识分享小能手16 分钟前
CSS3学习教程,从入门到精通,CSS3 弹性盒子(Flexbox)布局全面指南(20)
前端·javascript·css·学习·css3·html5·前端开发
ZhongyiChen19 分钟前
【NEXT JS 之旅】next-auth 助力实现最小登录方案
前端·后端·next.js
冴羽24 分钟前
SvelteKit 最新中文文档教程(14)—— 错误处理
前端·javascript·svelte