自学内容网 自学内容网

vue框架

以下是一个简单的基于Vue框架的日历组件示例:

<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth"><</button>
      <h2>{{ currentMonth }}</h2>
      <button @click="nextMonth">></button>
    </div>
    <table>
      <thead>
        <tr>
          <th v-for="day in daysOfWeek" :key="day">{{ day }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="week in calendar" :key="week">
          <td v-for="date in week" :key="date" :class="{ 'today': isToday(date), 'selected': isSelected(date) }" @click="selectDate(date)">
            {{ date.getDate() }}
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentMonth: new Date(),
      daysOfWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    };
  },
  computed: {
    calendar() {
      const calendarData = [];
      const year = this.currentMonth.getFullYear();
      const month = this.currentMonth.getMonth();
      const firstDayOfMonth = new Date(year, month, 1);
      const lastDayOfMonth = new Date(year, month + 1, 0);
      const numberOfDaysInMonth = lastDayOfMonth.getDate();
      const firstWeekday = firstDayOfMonth.getDay();
      let currentDay = 1;

      for (let week = 0; week < 6; week++) {
        const rowData = [];

        if (week === 0) {
          for (let i = 0; i < 7; i++) {
            if (i >= firstWeekday) {
              rowData.push(new Date(year, month, currentDay++));
            } else {
              rowData.push(null);
            }
          }
        } else {
          for (let i = 0; i < 7; i++) {
            if (currentDay <= numberOfDaysInMonth) {
              rowData.push(new Date(year, month, currentDay++));
            } else {
              rowData.push(null);
            }
          }
        }

        calendarData.push(rowData);
      }

      return calendarData;
    },
  },
  methods: {
    prevMonth() {
      const prevMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() - 1);
      this.currentMonth = prevMonth;
    },
    nextMonth() {
      const nextMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + 1);
      this.currentMonth = nextMonth;
    },
    isToday(date) {
      const today = new Date();
      return date.toDateString() === today.toDateString();
    },
    isSelected(date) {
      // Your logic to check if a date is selected or not
    },
    selectDate(date) {
      // Your logic to handle when a date is selected
    },
  },
};
</script>

<style scoped>
.calendar {
  font-family: Arial, sans-serif;
}

.header {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 5px;
  text-align: center;
}

.today {
  background-color: lightblue;
}

.selected {
  background-color: yellow;
}
</style>

这个日历组件包括以下功能:

  • 显示当前月份的日历
  • 可以通过左右箭头按钮切换月份
  • 当天日期用不同颜色标识
  • 可以选择日期,并用不同颜色标识选择的日期

你可以根据自己的需求进行修改和扩展。


原文地址:https://blog.csdn.net/m0_73767377/article/details/144334400

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!