当前位置:首页 > 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 中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定到一个数组,可以实现多选功能。适用于复选框组(…

css日历制作

css日历制作

基础日历结构 使用HTML创建日历的基本框架,通常包含表格元素。月份、星期几和日期通过<table>标签组织,表头显示星期几,表格主体填充日期数字。 <div class="cal…

vue实现多选div

vue实现多选div

实现多选 Div 的基本思路 在 Vue 中实现多选 Div 功能,通常需要结合 v-model 或自定义事件管理选中状态,通过 CSS 类动态切换样式。核心逻辑包括数据绑定、状态管理和样式交互。…

vue实现考试多选功能

vue实现考试多选功能

Vue实现考试多选功能 数据绑定与选项渲染 使用v-model绑定用户选择的答案数组,结合v-for循环渲染选项列表。示例代码: <template> <div>…

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue实现多选答题

vue实现多选答题

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