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>
相关推荐
前端小巷子17 分钟前
Web开发中的文件上传
前端·javascript·面试
翻滚吧键盘1 小时前
{{ }}和v-on:click
前端·vue.js
上单带刀不带妹1 小时前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架
杨进军2 小时前
React 创建根节点 createRoot
前端·react.js·前端框架
ModyQyW2 小时前
用 AI 驱动 wot-design-uni 开发小程序
前端·uni-app
说码解字2 小时前
Kotlin lazy 委托的底层实现原理
前端
爱分享的程序员2 小时前
前端面试专栏-算法篇:18. 查找算法(二分查找、哈希查找)
前端·javascript·node.js
翻滚吧键盘3 小时前
vue 条件渲染(v-if v-else-if v-else v-show)
前端·javascript·vue.js
vim怎么退出3 小时前
万字长文带你了解微前端架构
前端·微服务·前端框架
你这个年龄怎么睡得着的3 小时前
为什么 JavaScript 中 'str' 不是对象,却能调用方法?
前端·javascript·面试