封装一个简单的小日历组件,可以在日历上点击任务并展示

当时接到的需求就是有一个日历组件,上面可以展示当前这个月的任务列表,点击任务展示具体详情
如果任务超过两个,会自动隐藏并展示 …

<template>
  <div>
    <el-calendar ref="calender" v-model="nowDay">
      <template slot="dateCell" slot-scope="{date,data}">
        <div :style="isIncludes(data,scheduleData[data.day]) ? { backgroundColor : generateRandomColors(Number(data.day.split('-').slice(2).join('-'))) } : {}" class="dataContent"
             @click="handleCalendarClick">
          <p style="text-align: center">{{ data.day.split('-').slice(1).join('-') }}</p>
          <div v-for="(item,index) in scheduleData[data.day]" :key="item.id">
            <p v-if="index <= 1 && (item.workingDay.indexOf(data.day.split('-').slice(2).join('-')) !== -1)" class="mission-items"
               @click.prevent="gotoMissionDetail(item)">
              {{ item.content }}
              <!--如果有任务状态-->
              <span v-if="item.isStatus">:{{ item.statusName }}</span>
            </p>
            <!--超过两个时候如何展示-->
            <i @click.stop="showAllMission(scheduleData[data.day])" v-if="index > 1 && (item.workingDay.indexOf(data.day.split('-').slice(2).join('-')) !== -1)" class="el-icon-caret-bottom calender-caret" ></i>
          </div>
        </div>
      </template>
    </el-calendar>
    <p style="color:#F0973D;">含有<i  class="el-icon-caret-bottom calender-caret"></i>: 当日任务超过两个,可以点击查看箭头查看所有任务</p>
    <el-dialog
      title="任务"
      :visible.sync="centerDialogVisible"
      width="30%"
      >
      <span>这里展示所有的任务列表?</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "calender-mission",
  props: {
    nowDay: {
      type: String || Number,
      default: new Date()
    },
    scheduleData: {
      type: Object,
      default: () => {}
    }
  },
  data(){
    return {
      centerDialogVisible:false,
    }
  },
  mounted() {
    this.$nextTick(() => {
      const t = this.$refs.calender.$el && this.$refs.calender.$el.getElementsByClassName('el-calendar-table')[0].children[0].children
      for (let i = 0; i < t.length; i++) {
        t[i].innerHTML = '周' + t[i].innerHTML
      }
    })
  },
  methods: {
    showAllMission(allDayMission){
      console.log(allDayMission)
      this.centerDialogVisible = true
    },
    isIncludes(data, arr = []) {
      const nowDay = data.day.split('-').slice(2).join('-')
      const arrDay = arr.map(i => i.workingDay)
      return arrDay.indexOf(nowDay) !== -1
    },
    gotoMissionDetail(item) {
      console.log(item, 'item')
    },
    handleCalendarClick() {
      // 阻止事件冒泡和默认行为
      event.stopPropagation();
      event.preventDefault();
    },
    generateRandomColors(num) {
      let colors = []; // 存放生成的颜色数组
      for (let i = 0; i < num; i++) {
        let red = Math.floor(Math.random() * 256);
        let green = Math.floor(Math.random() * 256);
        let blue = Math.floor(Math.random() * 256);
        let color = red + ", " + green + ", " + blue;
        if (!colors.includes(color)) {
          colors.push(color);
        } else {
          i--;
        }
      }

      return `rgba(${colors[0].split(',')[0]},${colors[0].split(',')[1]},${colors[0].split(',')[2]},.5)`;
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-calendar-table:has(.is-range) td.prev,
::v-deep .el-calendar-table:has(.is-range) td.next {
  cursor: not-allowed;
  pointer-events: none;
  // display: none;
}

::v-deep .el-calendar-table:not(.is-range) td.prev,
::v-deep .el-calendar-table:not(.is-range) td.next {
  cursor: not-allowed;
  pointer-events: none;
  // display: none;
}

.prev > .el-calendar-day > .dateContent {
  display: none;
}

.next > .el-calendar-day > .dateContent {
  display: none;
}

// 隐藏月份后多一行
::v-deep .el-calendar-table:not(.is-range) td.next { /*隐藏下个月的日期*/
  visibility: hidden;
}

// 隐藏上个月 今天 下个月按钮
::v-deep .el-calendar__button-group {
  display: none;
}

::v-deep .el-calendar-table:not(.is-range) td.prev { /*隐藏上个月的日期*/
  visibility: hidden;
}

::v-deep {
  .el-calendar__header {
    justify-content: center;
    border-bottom: 0;
  }

  .el-calendar-table {
    .el-calendar-table__row {
      .current {
        .el-calendar-day {
          padding: 0;

          .dataContent {
            width: 100%;
            height: 100%;
            padding: 8px;
            box-sizing: border-box;

            & > p {
              line-height: 1;
              margin: 0 0 5px 0;
              padding: 0;
            }

            & > div {
              text-align: center;

              .mission-items {
                padding: 0;
                margin: 0;

                &:nth-child(1) {
                  margin-top: 4px;
                }
              }
            }
          }
        }

      }

      .next {
        .el-calendar-day {
          display: none !important;
        }
      }
    }
  }
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
二次封装ElementUI中的日历组件可以使代码更加简洁和易于维护,同时也能提高开发效率。以下是一个简单的二次封装ElementUI中日历组件的示例: 1. 创建一个自定义的日历组件 ```javascript <template> <el-date-picker v-model="value" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" ></el-date-picker> </template> <script> export default { name: 'MyDatePicker', props: { value: { type: Array, default: () => [null, null] }, pickerOptions: { type: Object, default: () => ({}) } } } </script> ``` 2. 在父组件中使用自定义的日历组件 ```javascript <template> <div> <my-date-picker v-model="dateRange" :picker-options="pickerOptions" /> </div> </template> <script> import MyDatePicker from '@/components/MyDatePicker.vue' export default { components: { MyDatePicker }, data() { return { dateRange: [null, null], pickerOptions: { disabledDate: (time) => { return time.getTime() > Date.now() } } } } } </script> ``` 在以上代码中,我们创建了一个名为MyDatePicker的组件,该组件接受一个value属性和一个pickerOptions属性,其中value属性是一个数组,用于绑定选中的日期范围,pickerOptions属性是一个选项对象,用于配置日期选择器的一些选项,例如禁用未来日期等。在父组件中使用MyDatePicker组件时,我们可以通过v-model指令绑定选中的日期范围,并通过picker-options属性向MyDatePicker组件传递选项对象。 通过以上的二次封装,我们可以在多个页面中复用MyDatePicker组件,同时也可以通过修改pickerOptions属性来改变日期选择器的行为,这样可以大大提高代码的复用性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值