当前位置:首页 > VUE

vue 实现日期多选

2026-02-20 13:15:15VUE

实现日期多选的基础方法

使用 v-model 绑定一个数组来存储选中的日期,结合 Vue 的 @change@click 事件实现多选逻辑。以下是基于原生 <input type="date"> 的简单实现:

<template>
  <div>
    <input 
      type="date" 
      v-model="selectedDate" 
      @change="handleDateChange"
    />
    <ul>
      <li v-for="(date, index) in selectedDates" :key="index">
        {{ date }}
        <button @click="removeDate(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedDate: '',
      selectedDates: []
    };
  },
  methods: {
    handleDateChange() {
      if (this.selectedDate && !this.selectedDates.includes(this.selectedDate)) {
        this.selectedDates.push(this.selectedDate);
      }
      this.selectedDate = '';
    },
    removeDate(index) {
      this.selectedDates.splice(index, 1);
    }
  }
};
</script>

使用第三方日期组件

对于更复杂的场景,推荐使用成熟的日期选择库如 v-calendarelement-ui 的日期组件。

使用 v-calendar 实现多选

安装依赖:

npm install v-calendar

示例代码:

<template>
  <v-date-picker 
    v-model="selectedDates" 
    is-range 
    is-multiple
    :max-date="new Date()"
  />
</template>

<script>
import { DatePicker } from 'v-calendar';
export default {
  components: {
    'v-date-picker': DatePicker
  },
  data() {
    return {
      selectedDates: []
    };
  }
};
</script>

使用 element-ui 的日期范围选择

安装依赖:

npm install element-ui

示例代码:

<template>
  <el-date-picker
    v-model="selectedDates"
    type="dates"
    placeholder="选择多个日期"
    format="yyyy-MM-dd"
    value-format="yyyy-MM-dd"
  />
</template>

<script>
export default {
  data() {
    return {
      selectedDates: []
    };
  }
};
</script>

自定义日期多选组件

如果需要完全自定义逻辑,可以封装一个组件:

vue 实现日期多选

<template>
  <div class="custom-date-picker">
    <div 
      v-for="day in daysInMonth" 
      :key="day.date"
      :class="{ 'selected': day.selected }"
      @click="toggleDate(day)"
    >
      {{ day.date.getDate() }}
    </div>
  </div>
</template>

<script>
export default {
  props: ['year', 'month'],
  data() {
    return {
      selectedDays: []
    };
  },
  computed: {
    daysInMonth() {
      // 生成当月所有日期的逻辑
    }
  },
  methods: {
    toggleDate(day) {
      const index = this.selectedDays.findIndex(d => 
        d.toDateString() === day.date.toDateString()
      );
      if (index === -1) {
        this.selectedDays.push(day.date);
      } else {
        this.selectedDays.splice(index, 1);
      }
      this.$emit('input', this.selectedDays);
    }
  }
};
</script>

<style>
.selected {
  background-color: #409EFF;
  color: white;
}
</style>

注意事项

  • 日期格式处理:建议使用 moment.jsdate-fns 处理日期格式转换
  • 范围限制:通过 min-datemax-date 限制可选范围
  • 性能优化:大量日期渲染时需使用虚拟滚动
  • 移动端适配:考虑触摸事件和响应式布局

以上方法可根据实际需求组合或调整,第三方组件通常提供更完善的交互和样式支持。

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

相关文章

Vue实现级联多选

Vue实现级联多选

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

vue实现多选日历

vue实现多选日历

Vue 实现多选日历 使用 Vue 实现多选日历功能可以借助现有的日历库或手动构建。以下是两种常见方法: 使用第三方库(如 V-Calendar) 安装 V-Calendar 库: npm ins…

vue 实现多选全选

vue 实现多选全选

实现多选全选功能 在Vue中实现多选全选功能可以通过以下步骤完成: 数据绑定与状态管理 创建数据模型存储选项列表和选中状态。使用v-model绑定复选框,并通过计算属性管理全选状态。 data(…

vue列表多选实现思路

vue列表多选实现思路

Vue列表多选实现思路 基础实现:v-model绑定数组 通过v-model绑定一个数组,配合checkbox的value属性实现多选。选中时自动将值添加到数组,取消选中时从数组中移除。 <t…

vue实现根据日期排序

vue实现根据日期排序

Vue 实现根据日期排序的方法 在 Vue 中实现日期排序可以通过多种方式完成,以下是几种常见的方法: 使用 JavaScript 的 sort 方法 Vue 的响应式系统可以结合 JavaScr…

vue实现select组件多选

vue实现select组件多选

实现多选 Select 组件 在 Vue 中实现多选 Select 组件可以通过原生 <select> 结合 multiple 属性或使用第三方 UI 库(如 Element UI、Ant…