vue自定义日历组件(12日历平铺) pc/移动

文章目录


前言

思路:

  1. 获取每个月天数,把每个月天数给数组中的day
  2. 再day天数给list的name
  3. 获取12个月的年月日
  4. 把获取的年月日和选中的年月日对比
  5. 如果相等让list中的type变为1,不相等为0

疑惑点:
为什么2022-1-1中type改变成了1,到2022-1-2后把2022-1-1中type改成了0,必须要加 break;跳出之后才可以保存之前的type值

效果图:

在这里插入图片描述

代码如下:

<template>
  <div id="calendar">
    <div class="choose-head">
      <div class="choose-year">{{ currentYear }} 年工作日历</div>
      <div class="choose-search">
        <el-input
          class="choose-input"
          v-model.trim="currentYear"
          placeholder=""
        >
          {{ currentYear }}</el-input
        >
        <el-button @click="handleChangePickPre(currentYear)">&lt;</el-button>
        <el-button @click="handleChangePickNext(currentYear)">&gt;</el-button>
      </div>
    </div>
    <div
      v-for="item in calendarDataList"
      :key="item.month"
      class="calendarList"
    >
      <div class="title-month">{{ item.month }}</div>
      <!-- 星期 -->
      <ul class="weekdays">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
      <!-- 日期 -->
      <ul class="days">
        <!-- 空格 -->
        <li v-for="item1 in item.xq" :key="item.month + '-day' + item1"></li>
        <!-- 列表 -->
        <li v-for="item1 in item.list" :key="item.month + '-' + item1">
          <span
            v-if="item1.type == 1"
            @click="handleChangeTime(item1.type)"
            class="active"
            >{{ item1.name }}</span
          >
          <span v-else @click="handleChangeTime(item1.type)">{{
            item1.name
          }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
export default {
  name: "vacationApply",
  data() {
    return {
      currentYear: new Date().getFullYear(), //年
      typeColour: "", //
      dayChecked: "",
      calendar: [
        {
          telit: "2022-1-1",
        },
        {
          telit: "2022-1-3",
        },
        {
          telit: "2022-7-9",
        },
      ],
      calendarDataList: [
        {
          month: "一月",
          months: 1,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "二月",
          months: 2,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "三月",
          months: 3,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "四月",
          months: 4,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "五月",
          months: 5,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "六月",
          months: 6,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "七月",
          months: 7,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "八月",
          months: 8,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "九月",
          months: 9,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "十月",
          months: 10,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "十一月",
          months: 11,
          day: 0,
          xq: 0,
          list: [],
        },
        {
          month: "十二月",
          months: 12,
          day: 0,
          xq: 0,
          list: [],
        },
      ],
      vacation: "",
    };
  },
  created: function () {
    this.handleChangeInitData();
    this.getCurrentDay();
  },
  methods: {
    getCurrentDayCalendar(year) {
      let that = this;
      for (let i = 0; i < that.calendarDataList.length; i++) {
        that.calendarDataList[i].list = [];
        // 获取每月天数
        let thisDate = new Date(year, that.calendarDataList[i].months, 0);
        that.calendarDataList[i].day = thisDate.getDate(); //赋值-获取天数
        for (var j = 1; j <= that.calendarDataList[i].day; j++) {
          //1.拿到了list的天数
          that.calendarDataList[i].list.push({
            name: j,
            type: 0,
          });
        }

        for (var h = 0; h < that.calendarDataList[i].list.length; h++) {
          // 年月日
          that.vacation =
            year +
            "-" +
            that.calendarDataList[i].months +
            "-" +
            that.calendarDataList[i].list[h].name;
          // 用12月的年月日和calendar中年月日对比
          for (let k = 0; k < that.calendar.length; k++) {
            // 如果相等结束循环
            if (this.calendar[k].telit == that.vacation) {
              that.calendarDataList[i].list[h].type = 1;
              break;
            } else {
              that.calendarDataList[i].list[h].type = 0;
            }
          }
        }
        // 获取前月的第一天是星期几,空几格
        let date = new Date(year, this.calendarDataList[i].months - 1, 1);
        this.calendarDataList[i].xq = date.getDay();
      }
    },

    // js获取每个月有多少天
    handleChangeInitData: function (year) {
      if (year != undefined) {
        this.currentYear = year;
        this.getCurrentDayCalendar(year);
      } else {
        let date = new Date();
        let year = date.getFullYear();
        this.getCurrentDayCalendar(year);
      }
    },
    // 点击年
    handleChangePickPre: function (year) {
      year--;
      this.currentYear = year;
      this.handleChangeInitData(year);
    },
    handleChangePickNext: function (year) {
      year++;
      this.currentYear = year;
      this.handleChangeInitData(year);
    },
    // 点击事件
    handleChangeTime(type) {
      debugger;
      let title = "";
      if (type == 1) {
        title = "您确定将假期改成工作日吗?";
      } else {
        title = "您确定将工作日改成假期吗?";
      }
      this.$confirm(title, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          // this.$message({
          //   type: "success",
          //   message: type == 1 ? "修改成功!" : "取消成功!",
          // });
        })
        .catch(() => {});
    },
  },
};
</script>
<style>
#calendar {
  font-size: 12px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin: 0 auto;
  padding: 20px;
}
.title-month {
  color: #ffffff;
  font-size: 18px;
  text-align: center;
  background-color: #000080;
}
.calendarList {
  width: 24%;
}
.choose-head {
  width: 100%;
  padding: 20px;
  background-color: #f0f0f0;
}
.choose-year {
  text-align: center;
  font-size: 25px;
}
.choose-search {
  display: flex;
  flex-flow: flex-end;
  justify-content: end;
}
.choose-input {
  width: 100px !important;
}
.month {
  width: 100%;
  color: #333333;
}
.month ul {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: space-between;
  height: 35px;
}
.year-month {
  display: flex;
  align-items: center;
  justify-content: space-around;
  margin-top: 10px;
}
.choose-month {
  text-align: center;
  font-size: 12px;
}
.arrow {
  padding: 15px;
  color: #999999;
}

.month ul li {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 3px;
}
.weekdays {
  margin: 0;
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  color: #999;
  justify-content: space-around;
}
.weekdays li {
  display: inline-block;
  width: 13.6%;
  text-align: center;
}
.days {
  padding: 10px;
  background: #ffffff;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
}
.days li {
  /* calendardatalist-style-type: none; */
  display: inline-block;
  width: 14.2%;
  text-align: center;
  padding-bottom: 4px;
  padding-top: 10px;
  font-size: 12px;
  color: #000;
}
.days li .active {
  padding: 6px 6px;
  background: green;
  color: #fff;
}
.days li .other-month {
  padding: 5px;
  color: gainsboro;
}
.days li:hover > span > span {
  padding: 6px 6px;
  background: #e1e1e1;
  color: #fff;
}
</style>



  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
我很抱歉,但是我无法直接通过引用内容查看具体的图片和代码。然而,根据你提供的引用信息,我可以告诉你关于vue日历自定义组件的一些信息。 根据引用,这个组件目录可以提供给你一些关于vue日历自定义组件的相关代码和文件。 根据引用,这个vue日历组件支持农历和假期展示,并且可以自定义可选择的日期范围。 根据引用,在组件中使用这个vue日历组件,你需要在main.js中引入它。组件默认情况下会显示一个自定义的左侧icon。某一个日期可以出现选中状态,也可以使用圆点模式来表示选中状态。你可以通过传递一个包含active属性的数组对象来自定义某一天的数据。 总的来说,这个vue日历自定义组件提供了一些可定制化的功能,包括农历展示、假期展示以及可选择日期范围等。你可以根据你的需求在组件中进行相应的配置和使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue自定义日历组件](https://blog.csdn.net/weixin_38644883/article/details/88067612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [vue日历组件,支持农历以及假期展示,可以自定义可选择日期范围](https://download.csdn.net/download/qq_29597215/86267518)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值