当前位置:首页 > VUE

vue实现日历多选

2026-02-19 00:19:58VUE

Vue 实现日历多选功能

安装依赖

使用 v-calendar 库可以快速实现日历多选功能。通过 npm 或 yarn 安装:

npm install v-calendar

基本配置

在 Vue 项目中引入 v-calendar 并注册组件:

import VCalendar from 'v-calendar';
import 'v-calendar/dist/style.css';

Vue.use(VCalendar);

实现多选功能

在模板中使用 <v-date-picker> 组件,并通过 mode="multiple" 开启多选模式:

<template>
  <v-date-picker
    v-model="selectedDates"
    mode="multiple"
    :min-date="new Date()"
    :attributes="attrs"
  />
</template>

<script>
export default {
  data() {
    return {
      selectedDates: [],
      attrs: [
        {
          highlight: {
            color: 'blue',
            fillMode: 'light',
          },
        },
      ],
    };
  },
};
</script>

自定义样式与交互

通过 :attributes 绑定高亮样式,标记已选日期。selectedDates 数组会动态更新用户选择的日期。

vue实现日历多选

处理日期范围

如果需要选择日期范围而非离散日期,将 mode 改为 'range'

<v-date-picker v-model="range" mode="range" />

禁用特定日期

通过 :disabled-dates 属性禁用不可选的日期(如周末):

vue实现日历多选

data() {
  return {
    disabledDates: [
      { weekdays: [1, 7] } // 禁用周末
    ]
  };
}

事件监听

监听 input 事件以处理日期选择变化:

<v-date-picker 
  @input="handleDateChange" 
  v-model="selectedDates" 
  mode="multiple" 
/>

移动端适配

v-calendar 默认支持响应式设计,若需进一步优化移动端体验,可添加 CSS 媒体查询:

@media (max-width: 640px) {
  .vc-container {
    width: 100%;
  }
}

完整示例

以下是一个完整的多选日历组件示例:

<template>
  <div>
    <v-date-picker
      v-model="selectedDates"
      mode="multiple"
      :min-date="new Date()"
      :disabled-dates="disabledDates"
      :attributes="attrs"
      @input="logDates"
    />
    <p>已选日期: {{ selectedDates.join(', ') }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDates: [],
      attrs: [
        {
          highlight: {
            color: 'green',
            fillMode: 'solid',
          },
        },
      ],
      disabledDates: [
        { weekdays: [1, 7] },
      ],
    };
  },
  methods: {
    logDates() {
      console.log('Selected Dates:', this.selectedDates);
    },
  },
};
</script>

标签: 多选日历
分享给朋友:

相关文章

vue实现日历插件

vue实现日历插件

Vue 日历插件实现方案 使用现成组件库 推荐直接使用成熟的 Vue 日历组件,如: V-Calendar:专为 Vue 设计的轻量级日历组件 FullCalendar:功能强大的日历库,有 Vue…

vue表格实现多选

vue表格实现多选

实现多选表格的基础方法 在Vue中实现表格多选功能,通常需要结合el-table组件(Element UI)或v-data-table组件(Vuetify)。以下是基于Element UI的实现示例:…

vue怎么实现多选

vue怎么实现多选

使用 v-model 绑定数组实现多选 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选功能。当用户选择多个选项时,选中的值会自动添加到数组中。 <template>…

vue实现多选列表

vue实现多选列表

Vue 实现多选列表的方法 使用 v-model 绑定数组 在 Vue 中,可以利用 v-model 指令绑定一个数组来实现多选功能。当用户选择多个选项时,选中的值会自动添加到数组中。 <te…

vue 实现多选按钮

vue 实现多选按钮

实现多选按钮的基本方法 在Vue中实现多选按钮可以通过v-model绑定数组来管理选中状态。以下是一个基础示例: <template> <div> <lab…

基于vue实现日历

基于vue实现日历

实现基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含星期标题和日期格子。月份切换按钮通过v-on绑定事件。 <template> <div class="ca…