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>
相关推荐
无限大.3 小时前
前端知识速记:节流与防抖
前端
十八朵郁金香3 小时前
【VUE案例练习】前端vue2+element-ui,后端nodo+express实现‘‘文件上传/删除‘‘功能
前端·javascript·vue.js
学问小小谢3 小时前
第26节课:内容安全策略(CSP)—构建安全网页的防御盾
运维·服务器·前端·网络·学习·安全
LCG元3 小时前
Vue.js组件开发-实现全屏图片文字缩放切换特效
前端·javascript·vue.js
还是鼠鼠4 小时前
图书管理系统 Axios 源码__新增图书
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
还是鼠鼠7 小时前
图书管理系统 Axios 源码 __删除图书功能
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
轻口味8 小时前
Vue.js `Suspense` 和异步组件加载
前端·javascript·vue.js
m0_zj9 小时前
8.[前端开发-CSS]Day08-图形-字体-字体图标-元素定位
前端·css
还是鼠鼠9 小时前
图书管理系统 Axios 源码__编辑图书
前端·javascript·vscode·ajax·前端框架
北极象9 小时前
vue3中el-input无法获得焦点的问题
前端·javascript·vue.js