当前位置:首页 > VUE

vue实现多选日历

2026-01-19 11:32:34VUE

Vue 实现多选日历

使用 Vue 实现多选日历功能可以借助现有的日历库或手动构建。以下是两种常见方法:

使用第三方库(如 V-Calendar)

安装 V-Calendar 库:

npm install v-calendar

在 Vue 组件中引入并使用:

vue实现多选日历

<template>
  <v-calendar
    :attributes="attributes"
    @dayclick="onDayClick"
    is-range
  />
</template>

<script>
import { ref } from 'vue';
import VCalendar from 'v-calendar';

export default {
  components: {
    VCalendar,
  },
  setup() {
    const selectedDates = ref([]);
    const attributes = ref([]);

    const onDayClick = (day) => {
      if (selectedDates.value.length === 2) {
        selectedDates.value = [];
      }
      selectedDates.value.push(day.date);

      if (selectedDates.value.length === 2) {
        attributes.value = [{
          key: 'selected',
          highlight: true,
          dates: selectedDates.value,
        }];
      }
    };

    return { selectedDates, attributes, onDayClick };
  },
};
</script>

手动实现基础多选日历

创建一个简单的日历组件,支持多选日期:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">Previous</button>
      <h2>{{ currentMonth }}</h2>
      <button @click="nextMonth">Next</button>
    </div>
    <div class="days">
      <div v-for="day in days" :key="day.date" 
           @click="toggleDate(day)"
           :class="{ selected: selectedDates.includes(day.date) }">
        {{ day.day }}
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date());
    const selectedDates = ref([]);

    const days = computed(() => {
      const year = currentDate.value.getFullYear();
      const month = currentDate.value.getMonth();
      const firstDay = new Date(year, month, 1);
      const lastDay = new Date(year, month + 1, 0);
      const daysInMonth = lastDay.getDate();

      const daysArray = [];
      for (let i = 1; i <= daysInMonth; i++) {
        daysArray.push({
          day: i,
          date: new Date(year, month, i).toDateString(),
        });
      }
      return daysArray;
    });

    const currentMonth = computed(() => {
      return currentDate.value.toLocaleString('default', { month: 'long', year: 'numeric' });
    });

    const toggleDate = (day) => {
      const index = selectedDates.value.indexOf(day.date);
      if (index === -1) {
        selectedDates.value.push(day.date);
      } else {
        selectedDates.value.splice(index, 1);
      }
    };

    const prevMonth = () => {
      currentDate.value = new Date(
        currentDate.value.getFullYear(),
        currentDate.value.getMonth() - 1,
        1
      );
    };

    const nextMonth = () => {
      currentDate.value = new Date(
        currentDate.value.getFullYear(),
        currentDate.value.getMonth() + 1,
        1
      );
    };

    return { currentDate, selectedDates, days, currentMonth, toggleDate, prevMonth, nextMonth };
  },
};
</script>

<style>
.calendar {
  font-family: Arial, sans-serif;
  max-width: 400px;
  margin: 0 auto;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.days div {
  padding: 10px;
  text-align: center;
  cursor: pointer;
}
.selected {
  background-color: #42b983;
  color: white;
}
</style>

处理日期范围选择

如果需要选择日期范围而非独立多选日期,可以修改 toggleDate 方法:

vue实现多选日历

const toggleDate = (day) => {
  if (selectedDates.value.length >= 2) {
    selectedDates.value = [day.date];
  } else {
    selectedDates.value.push(day.date);
    if (selectedDates.value.length === 2) {
      selectedDates.value.sort((a, b) => new Date(a) - new Date(b));
    }
  }
};

使用 Vue DatePicker

另一种方法是使用专门的日期选择器库,如 vue-datepicker:

npm install vue-datepicker-next

示例代码:

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

<script>
import { ref } from 'vue';
import DatePicker from 'vue-datepicker-next';

export default {
  components: { DatePicker },
  setup() {
    const date = ref([]);
    return { date };
  },
};
</script>

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

相关文章

vue实现多选答题

vue实现多选答题

Vue 实现多选答题功能 实现多选答题功能需要结合 Vue 的数据绑定和事件处理机制,以下是一个完整的实现方案: 数据结构设计 data() { return { questions:…

vue 实现日历

vue 实现日历

实现基础日历结构 使用 Vue 的模板语法构建日历的网格布局,通常以 7 列(一周)和 5-6 行(月份天数)的表格形式展示。通过 v-for 循环渲染日期单元格,动态绑定样式和事件。 &l…

vue日历实现

vue日历实现

Vue 日历组件实现方法 基础日历实现 使用第三方库如v-calendar快速搭建日历功能: npm install v-calendar 引入并注册组件: import VCalendar f…

css制作日历

css制作日历

使用CSS Grid布局制作日历 日历的布局适合使用CSS Grid实现,因其天然的行列结构。以下是一个基础日历的HTML和CSS代码: <div class="calendar">…

vue实现多选答题

vue实现多选答题

实现多选答题功能 在Vue中实现多选答题功能,可以通过以下方法完成: 数据准备 定义一个questions数组,每个问题包含题目和选项,选项是一个对象数组,每个选项有文本和是否被选中的状态。 da…

Vue实现级联多选

Vue实现级联多选

Vue 实现级联多选的方法 使用 Element UI 的 Cascader 组件 Element UI 提供了 el-cascader 组件,支持多选功能。通过设置 props 的 multiple…