Vue3 使用element-plus多选日期并通过按钮点击实现,并在下方显示具体日期

image.png

封装日期组件(simulateDate.vue)

<template>
    <div>
        <div>
            <el-button type="primary" @click="toggleDatePicker">选择日期</el-button>
            <el-date-picker class="dateStyle1" :clearable="false" v-model="dateTimeValue" ref="timePick" :teleported="true" type="dates"
                format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="选择日期">
                <template #footer>
                </template>
            </el-date-picker>
        </div>
        <el-tag class="dateLabel" size="large" v-for="(tag, index) in dateTimeValue" :key="index" :closable="closable" type="primary"
            :disable-transitions="false" @close="handleClose(index)">
            {{ tag }}
        </el-tag>
    </div>
</template>

组件script

const { proxy } = getCurrentInstance();
const emit = defineEmits();
const dateTimeValue = ref('')
const props = defineProps({
    modelValue: [String, Object, Array],
    closable:{
        // 是否可以删除
        type: Boolean,
        default: true
    }
})

// 数据监听
watch(() => dateTimeValue.value, (newValue) => {
    console.log(props.modelValue);
    console.log(newValue);
    if (newValue) {
        dateTimeValue.value = newValue.sort((a, b) => new Date(a) - new Date(b))
        emit('update:modelValue', dateTimeValue.value)
    }
}, { deep: true, immediate: true });
onMounted(() => {
    dateTimeValue.value = props.modelValue
})
function toggleDatePicker() {
    // 选择日期
    proxy.$refs.timePick.focus();
}
// 删除日期
function handleClose(index) {
    dateTimeValue.value.splice(index, 1);
}

组件样式

<style scoped lang='scss'>
// 隐藏默认日期选择框
::v-deep .dateStyle1 {
    width: 0px;
    height: 0px;
    padding: 0px;

    .el-input__wrapper {
        padding: 0px;
    }

    .el-input__prefix {
        display: none;
    }
}

.dateLabel {
    margin-right: 10px;
    margin-top: 10px;
}
</style>

父组件

<template>
    <el-form :model="priceInventory" ref="ruleFormRef2" label-width="150px">
             <el-form-item label="日期:">
                    <simulateDate v-model="priceInventory.name7"></simulateDate>
                </el-form-item>
    </el-row>
</template>
//引入组件
import simulateDate from '../simulateDate.vue'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容