当前位置:首页 > 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 限制可选范围
  • 性能优化:大量日期渲染时需使用虚拟滚动
  • 移动端适配:考虑触摸事件和响应式布局

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

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

相关文章

js 多选 实现

js 多选 实现

实现多选的 JavaScript 方法 使用复选框(Checkbox)实现多选 通过 HTML 的 checkbox 元素配合 JavaScript 实现多选功能。用户可以选择多个选项,提交时获取所有…

vue实现多选搜索

vue实现多选搜索

Vue 多选搜索实现方法 使用 Element UI 的 Select 组件 Element UI 提供了带有搜索功能的多选组件,适合快速实现需求。 安装 Element UI: npm inst…

vue级联实现多选

vue级联实现多选

Vue级联多选实现方法 使用el-cascader组件实现多选功能需要结合Element UI的特定配置。以下为具体实现步骤: 安装Element UI 确保项目中已安装Element UI库,可通…

vue 实现多选全选

vue 实现多选全选

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

vue实现icon多选组件

vue实现icon多选组件

Vue 实现 Icon 多选组件 组件设计思路 多选 Icon 组件的核心在于管理选中状态和渲染可交互的图标列表。通常需要以下功能: 支持传入图标列表(本地或远程) 支持单选/多选模式 提供选中状态…

vue实现多选和单选

vue实现多选和单选

Vue 实现单选功能 使用 v-model 绑定到同一个变量,通过 radio 的 value 属性区分选项。单选组会自动处理互斥逻辑。 <template> <div…