vue 自定义日历

接了一个需求,实现日历功能,但是并不完全是正常看到的日历,需要用接口返回的数据进行展示,并且需要和星期对上,用element UI上的日历并不能实现 想要的效果,所以自己写了一个组件,供大家参考。

首先下载 moment 包 npm i moment

// html

     <div class="date-list">
            <div class="date-items" v-for="(item) in week" :key="item">{{item}}</div>
            <div class="date-item" v-for="(item,index) in timeData" :key="index" :class="[item.allocationDate && item.allocationDate === time ? 'on' : '']">
              <el-link @click="selectDay(item)" :underline="false" :disabled='array.includes(item.allocationDate)'>
                <div :class="colorList[item.status]" v-if="item.allocationDate">
                  <div>{{item.allocationDate.slice(5).replace('-', '月')}}日</div>
                  <div v-if="item.status === 1">有号</div>
                  <div v-if="item.status === 0">无号</div>
                  <div v-if="item.status === 2">约满</div>
                </div>
              </el-link>
            </div>
          </div>


// 引入 moment
import moment from 'moment'

data () {
    return {
      time: '', // 用来高亮日期
      colorList: { 0: 'black', 1: 'blue', 2: 'red' }, // 不同状态展示不同颜色
      timeData: [], // 日历
      week: ['一', '二', '三', '四', '五', '六', '日'],
      array: [], // 补日历的数据
    }
  }


methods:{
    
    btn () {
         // 这里写请求接口
        const res = await getDailyAllocation(params)
        if (res.code === 200) {

        this.timeData = res.data

        // 数据处理展示成日历
        let startDate = moment(res.data[0].allocationDate)
        let endDate = null

        let week = moment(startDate).weekday()

        // week 为0 说明是周日
        if (week === 0) {
          week = 7
        }
        if (week > 1) {
          week = week - 1
          startDate = moment(startDate).subtract(week, 'days')
          endDate = moment(res.data[0].allocationDate)
          // 存储大于接口返回的数据日期
          const arr = []
           // 如果想展示大于接口返回的数据初始日期,那就放开这段代码,我这里只需要展示接口的数                    
           据,所以注释了
          // arr.push({ allocationDate: startDate.format('YYYY-MM-DD') })
          arr.push({ allocationDate: '' })
          while (startDate.add(1, 'days').isBefore(endDate)) {
            // 注意这里add方法处理后SDate对象已经改变。
           // arr.push({ allocationDate: endDate.format('YYYY-MM-DD') })
            arr.push({ allocationDate: '' })
          }
          this.timeData = [...arr, ...this.timeData]
          // 拿到补的数据
          this.array = arr.map(item => item.allocationDate)
        }
      } 
    },
    
    // 点击日期高亮
     selectDay (item) {
      this.time = item.allocationDate
    },

}

<style lang="less" scoped>
.date-list {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .date-item,
  .date-items {
    width: calc(100% / 7);
    text-align: center;
  }
  .date-items {
    line-height: 40px;
    height: 40px;
    background: #f5f7fa;
  }
  .date-item {
    height: 60px;
    cursor: pointer;
    padding-top: 10px;
  }
}

.black {
  color: black;
}
.blue {
  color: #00c0bf;
}
.red {
  color: #bb3d52;
}
.on {
  background: #f2f8fe;
}
</style>

//以下是接口返回的数据 

res.data = [
    {orgId: 107, screenType: null, allocationSum: 10, bookedSum: 2, allocationDate: "2023-04-16",…}
1
: 
{orgId: 107, screenType: null, allocationSum: 10, bookedSum: 0, allocationDate: "2023-04-17",…}
2
: 
{orgId: 107, screenType: null, allocationSum: 10, bookedSum: 2, allocationDate: "2023-04-18",…}
3
: 
{orgId: 107, screenType: null, allocationSum: 10, bookedSum: 2, allocationDate: "2023-04-19",…}
4
: 
{orgId: 107, screenType: null, allocationSum: 10, bookedSum: 2, allocationDate: "2023-04-20",…}
]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 Vue.js自定义日历组件,并通过拖拽功能实现拖动事件的功能。下面是一个简单的示例代码: ```html <template> <div class="calendar"> <div class="weekdays"> <div v-for="day in 7" :key="day" class="weekday">{{ getWeekday(day) }}</div> </div> <div class="days"> <div v-for="(date, index) in dates" :key="index" class="day" @mousedown="startDrag(index)"> {{ date }} </div> </div> </div> </template> <script> export default { data() { return { dates: [1, 2, 3, 4, 5, 6, 7], // 替换为你的日期数组 draggingIndex: -1, draggingOffset: 0, }; }, methods: { getWeekday(day) { // 获取星期几的逻辑 // 可根据具体需求进行实现 }, startDrag(index) { this.draggingIndex = index; this.draggingOffset = 0; // 添加鼠标移动和松开事件监听 document.addEventListener("mousemove", this.handleDrag); document.addEventListener("mouseup", this.endDrag); }, handleDrag(event) { // 计算鼠标在X轴上的位移 this.draggingOffset = event.pageX - event.target.getBoundingClientRect().left; }, endDrag() { // 更新拖动后的日期数组 // 可根据具体需求进行实现 // 清除事件监听 document.removeEventListener("mousemove", this.handleDrag); document.removeEventListener("mouseup", this.endDrag); this.draggingIndex = -1; this.draggingOffset = 0; }, }, }; </script> <style> .calendar { width: 300px; border: 1px solid #ccc; } .weekdays { display: flex; } .weekday { flex: 1; text-align: center; } .days { display: flex; flex-wrap: wrap; } .day { width: calc(100% / 7); height: 50px; border: 1px solid #ccc; display: flex; align-items: center; justify-content: center; } </style> ``` 上述代码是一个简单的日历组件,通过 `@mousedown` 监听鼠标按下事件,`@mousemove` 监听鼠标移动事件,`@mouseup` 监听鼠标松开事件。在拖动过程中,计算鼠标在X轴上的位移,并更新拖动后的日期数组。你可以根据具体需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学不会•

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

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

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

打赏作者

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

抵扣说明:

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

余额充值