Vue + Element:实现前端日历统计功能


前言

在项目开发过程中,需要新增一个日历,来显示每日任务数据及完成情况的功能需求。由于之前没有开发过类似需求,通过查阅element官网以及网上资料。最终完成该功能需求。


一、显示效果

在这里插入图片描述

二、需求实现

1. 当天之前的时间,显示框置灰,禁止点击

实现代码如下:

              <div slot="reference" class="div-Calendar" :class="(show(data.day))?{disabled:true}:{disabled:false}" @click="calendarOnClick(data)">
                <p align="center" :class="data.isSelected ? 'is-selected' : ''" style="height: 10px;">
                  {{ data.day.split('-').slice(2).join('-') }}
                </p>
                <div v-for="item in calendarData" :key="item.dateStr">
                  <div v-if="item.dateStr === data.day">
                    <div class="is-selected" align="center">任务数: {{ dataCheckout(item.taskTotal) }}</div>
                    <div class="is-selected" align="center">完成情况: {{ dataCheckout(item.cenaNum) }} / {{ dataCheckout(item.taskTotal) }}</div>
                  </div>
                </div>
              </div>
  methods: {
    show(day) {
      if (day < moment(new Date()).format('YYYY-MM-DD')) {
        return true
      } else {
        return false
      }
    }
    }

通过 show方法,来判断 div的 样式

2. 隐藏 上月日期和下月日期 并 时间框高度调节

上月日期和下月日期 (隐藏):

<style scoped>
._calendar .el-calendar-table .el-calendar-day {
  padding: 0px;
  height: 100px;
}
.el-calendar-table:not(.is-range) td.prev, .el-calendar-table:not(.is-range) td.next {
    visibility: hidden;
}

上月日期和下月日期 (显示,禁止点击):

.el-calendar-table:not(.is-range) td.prev, .el-calendar-table:not(.is-range) td.next {
    pointer-events: none
}

3. 移入时间框显示 提示框 并 延时弹出

实现代码如下:

            <el-popover
              slot="reference"
              placement="top-start"
              title="任务"
              width="220"
              trigger="hover"
              :open-delay="openDelay"
              @show="getTaskInfo(data)"
            >
            
  		   <!-- 业务代码块  -->
  		
            </el-popover>
  data: function() {
    return {
      openDelay: 1000
      }
  }

4. 上月,今天,下月 按钮实现

实现代码如下:

  created: function() {
    this.$nextTick(() => {
      // 点击前一个月
      let prevBtn = document.querySelector(
        '.el-calendar__button-group .el-button-group>button:nth-child(1)'
      )
      prevBtn.addEventListener('click', e => {
        this.row.date = this.value
        getCalendarDate(this.row).then(response => {
          this.calendarData = response.data
          this.list = response.data
        }).catch(_ => {
          this.emptyText = '接口异常,请稍后访问'
        })
      })

      // 点击下一个月
      let nextBtn = document.querySelector(
        '.el-calendar__button-group .el-button-group>button:nth-child(3)'
      )
      nextBtn.addEventListener('click', () => {
        this.row.date = this.value
        getCalendarDate(this.row).then(response => {
          this.calendarData = response.data
          this.list = response.data
        }).catch(_ => {
          this.emptyText = '接口异常,请稍后访问'
        })
      })

      // 点击今天
      let todayBtn = document.querySelector(
        '.el-calendar__button-group .el-button-group>button:nth-child(2)'
      )
      todayBtn.addEventListener('click', () => {
        this.row.date = this.value
        getCalendarDate(this.row).then(response => {
          this.calendarData = response.data
          this.list = response.data
        }).catch(_ => {
          this.emptyText = '接口异常,请稍后访问'
        })
      })
    })
  },

完整代码:
完整日历实现代码

总结

以上就是在开发过程中做的全部,希望大家多多学习,交流!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
因为价格日历需要与数据库进行交互,并且涉及到前后端交互,所以需要编写前后端代码。 1. 前端代码 使用Vue.js框架编写前端代码,主要包括以下几个部分: (1)页面布局:使用HTML和CSS编写价格日历的布局,可以使用element-ui等UI框架进行美化。 (2)数据绑定:使用Vue.js的双向数据绑定功能前端页面与后端数据进行交互。 (3)事件处理:为价格日历添加事件处理方法,如点击日期时弹出价格详情等。 (4)AJAX请求:使用axios等工具向后端发送请求获取数据,并将数据渲染到页面上。 示例代码: ```vue <template> <div> <el-calendar v-model="date" :range="range" :first-day-of-week="1" :cell-style="cellStyle" @change="handleDateChange"></el-calendar> <el-dialog title="价格详情" :visible.sync="dialogVisible"> <p>日期:{{ selectedDate }}</p> <p>价格:{{ price }}</p> </el-dialog> </div> </template> <script> import axios from 'axios' export default { data() { return { date: null, range: [new Date(2020, 0, 1), new Date(2021, 11, 31)], dialogVisible: false, selectedDate: '', price: '' } }, methods: { cellStyle({ date, row, column }) { return { backgroundColor: 'white' } }, handleDateChange() { const year = this.date.getFullYear() const month = this.date.getMonth() + 1 const day = this.date.getDate() axios.get(`/api/price?date=${year}-${month}-${day}`).then(res => { this.price = res.data.price }).catch(() => { this.price = '无价格数据' }) this.selectedDate = `${year}-${month}-${day}` this.dialogVisible = true } } } </script> ``` 2. 后端代码 使用Spring Boot框架编写后端代码,主要包括以下几个部分: (1)数据模型:定义价格数据模型,包括日期和价格两个字段。 (2)数据访问:使用JPA等工具访问数据库,实现对价格数据的增删改查等功能。 (3)控制器:编写RESTful API控制器,接收前端发送的请求,并根据请求参数返回对应的数据。 示例代码: ```java @Entity public class Price { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private LocalDate date; private double price; // getters and setters } @Repository public interface PriceRepository extends JpaRepository<Price, Long> { Optional<Price> findByDate(LocalDate date); } @RestController public class PriceController { @Autowired private PriceRepository priceRepository; @GetMapping("/api/price") public ResponseEntity<?> getPriceByDate(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) { Optional<Price> price = priceRepository.findByDate(date); if (price.isPresent()) { return ResponseEntity.ok(price.get()); } else { return ResponseEntity.notFound().build(); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值