做了一个自己看着还舒服的vue 日历表

直接上图

在这里插入图片描述

在这里我引入的是别的地方的插件

有拖动和点击事件的功能 方便存储

直接看代码吧就


<!--
 * @Descripttion: (工作日历管理/C_Calendar)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2022-12-20)
 * @LastEditors: (admin)
 * @LastEditTime: (2022-12-20)
-->		    
<template>
  <div class="app-container" style="padding: 0px">
    <el-form :model="form" @submit.native.prevent ref="form">
      <el-row :gutter="15">
        <el-col :lg="3">
          <el-form-item label="班次编号" prop="flightNo">
            <el-select placeholder="请选择安排的班次" multiple v-model="form.flightNo" style="width: 70%">
              <el-option v-for="item in glFlightList" :key="item.id" :value="item.flightCode">
                <span :style="{ color: item.flightColor }">{{ item.flightName }}</span></el-option
              >
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="3">
          <el-form-item label="线体编号" prop="lineCode">
            <el-select placeholder="请选择线体" multiple v-model="form.lineCode" style="width: 70%">
              <el-option v-for="item in LineList" :key="item.id" :value="item.lineCode">
                <span> {{ item.lineCode + '  /  ' + item.lineName }}</span></el-option
              >
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="2">
          <el-button type="primary" @click="handleAdd">添加出勤</el-button>
        </el-col>
        <el-col :lg="1">  <el-button type="danger"  @click="deletes">删除出勤</el-button>
         </el-col>
        <el-col :lg="13">
          <p>&nbsp;</p>
        </el-col>
        <!-- <el-col :lg="2">
          <el-button  type="success" @click="seeFlight" >查看现有排班</el-button>
        </el-col> -->
      </el-row>
    </el-form>

    <div class="calendar">
      <div class="select">
        <el-form inline >
          <el-form-item>
            <el-select v-model="curYear" placeholder="请选择">
              <el-option v-for="item in yearOptions" :key="item.key" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="curMonth" placeholder="请选择">
              <el-option v-for="item in monthOptions" :key="item.key" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuickChange('prev')">上一月</el-button>
            <el-button type="primary" @click="handleQuickChange('next')">下一月</el-button>
            <el-button style="background-color: #dcdfe6" @click="TwoChange">滑动</el-button>
          </el-form-item>
        </el-form>
      </div>

      <table class="calendar-table" :style="{ width }">
        <thead>
          <tr>
            <th v-for="(item, i) in weeks" :key="i">{{ item }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(dates, i) in res" :key="i" :style="{ height: tbodyHeight }" align="center" style="font-size: medium" border>
            <td
              v-for="(item, index) in dates"
              :key="index"
              :class="{
                notCurMonth: !item.isCurMonth,
                // currentDay: item.date === curDate,
                selectDay: item.isSelected,
                rangeSelectd: item.isRangeSelected,
                weekend: item.isWeekend,
                seeNow: item.isSele,
              }"
              @click="handleItemClick(item, i, index)"
              @mouseover="handleItemMove(item, i, index)"
            >
              <!-- @click="handleItemClick(item, i, index)" -->
              <!-- <span>{{ item.date.split('-').slice(1).join('-') }}</span> -->
              <span>{{ item.date }}</span>
              <slot :data="item" />
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <!-- <el-form :model="queryParams" size="small"  inline ref="queryForm" 
      @submit.native.prevent>
           
      <el-form-item label="班次名称" prop="flightNo">
        <el-select  v-model="queryParams.flightNo" placeholder="请选择线体" >
        <el-option v-for="item in glFlightList" :key="item.id" :label="item.flightName" :value="item.flightCode"></el-option>      
       </el-select> 
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <div style="margin-top: -33px">
    <el-collapse v-model="subTableCollapse" @change="handleChange"> 
      <el-collapse-item name="1">
            <template slot="title">
              <span class="subTableTitle"><strong>排班详情信息</strong></span>
            </template>
    <el-table :data="detailList" ref="table" border highlight-current-row title="已安排的班次" height="200px"   :row-class-name="tableRowClassName">
      <el-table-column width="250%" prop="flightNo" label="班次编号" align="center" :show-overflow-tooltip="true">

        <!-- <template slot-scope="scope">
                  {{ lineNameFormat(scope.row['flightNo'] )}}
                </template> -->
      </el-table-column>
      <el-table-column width="250%" prop="lineCode" label="线体编号" align="center" :show-overflow-tooltip="true" />
      <el-table-column width="500%" prop="attendanceDate" label="出勤日期" align="center" :show-overflow-tooltip="true"> </el-table-column>

      <el-table-column width="250%" prop="createBy" label="创建人" align="center" :show-overflow-tooltip="true" />
      <el-table-column width="300%" prop="createTime" label="创建时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column  label="操作" align="center" width="200">
            <template slot-scope="scope">
                   
                      <el-button size="mini" v-hasPermi="['schedule:ccalendar:delete']" type="danger" icon="el-icon-delete" title="删除" 
                @click="handleDelete(scope.row)"></el-button>
                </template>
          </el-table-column>
    </el-table>
    </el-collapse-item>
    </el-collapse>
    </div>
  </div>
</template>
<script>
import { listBLineInfo } from '@/api/factorys/bLineInfo.js'

import {
 getAttendanceData,
  addCCalendar,
  delCCalendar,
  getDaysInMonth,
  handleCrateDate,
  handleCreateDatePicker,
  parseTime,
  listCCalendar,
  DeleteFlightNo,
  getDetail,
} from '@/api/schedule/cCalendar.js'
import { GetBFlightAll } from '@/api/factorys/bFlightInfo.js'
export default {
  components: {},
  props: {
    selectMode: {
      type: String,
      default: 'click',
    },
    startOfWeek: {
      type: Number,
      default: 1,
    },
    canSelect: {
      type: Boolean,
      default: false,
    },
    width: {
      type: String,
      default: '90%',
    },
    tbodyHeight: {
      type: String,
      default: '60px',
    },
  },

  name: 'ccalendar',
  data() {
    return {
      change: undefined,
      //线体数据
      LineList: [],
      subTableCollapse : 1,
     
      // 表单参数
      form: {},
      //线体参数查询
      LineQueryParams: {
        pageNum: 1,
        pageSize: 1000,
        sort: undefined,
        sortType: undefined,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10000,
        sort: undefined,
        sortType: undefined,
        flightNo: undefined,
        lineCode: undefined,
      },
      flightList: [], //班次列表
      monthOptions: [],
      yearOptions: [],
      weeks: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      curYear: new Date().getFullYear(), // 当前年
      curMonth: new Date().getMonth(), // 当前月
      days: 0, // 当前月总共天数
      curDate: parseTime(new Date().getTime()), // 当前日期 yyyy-MM-dd 格式,用来匹配是否是当前日期
      prevDays: [], // 非当前月的上一月展示的日期
      rearDays: [], // 非当前月的下一月展示的日期
      curDays: [], // 当前月的日期
      showDays: [], // 总共展示的42个日期
      res: [], // 二维数组
      arr: [],
      selectedDates: [], // 选中的日期
      selectedMode: false, // true表示点击, false表示滑动
      moveIndex: [], // 两个,第一个是起始,第二个是结束
      canMove: false, // 当moveIndex数组有一个值时,可以触发滑动
      a: '000003',
      downList: [],
      arrDown: [],
      detailList : [],
      glFlightList: [],
      arrFlightCode: [],
    }
  },
  computed: {},
  watch: {
    curMonth: {
      handler(val) {
        this.handleGetDays(this.curYear, val, this.startOfWeek)
      },
    },
    curYear: {
      handler(val) {
        this.handleGetDays(val, this.curMonth, this.startOfWeek)
      },
    },
  },

  created() {
    // 列表数据查询
    this.getDownList()
    this.getLineList()
    this.getFlightList()
    this.getFlightData()
   
    this.weeks.unshift(...this.weeks.splice(this.startOfWeek - 1))
    this.handleGetDays(this.curYear, this.curMonth, this.startOfWeek)
    this.selectedMode = this.selectMode === 'click'
    //this.selectedMode = this.selectMode === this.change
  },
  mounted() {
    this.monthOptions = handleCreateDatePicker().months
    this.yearOptions = handleCreateDatePicker().years
    if (localStorage.selectedDates) this.selectedDates = JSON.parse(localStorage.selectedDates)
  },
 
  methods: {
    handleChange(value) {
      if (value.length > 0) {
        this.props.width = 300
      } else {
        this.props.width = 550
      }
    },
      /** 删除按钮操作 */
      handleDelete(row) {
      const Ids = row.id || this.ids;
      this.$confirm('是否确认删除选中的数据项?')
        .then(function () {
          return delCCalendar(Ids);
        })
        .then(() => {
          this.getDownList() ;
          this.getDetailList() ;
          this.getFlightData() ;
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    //得到线体列表
    getLineList() {
      listBLineInfo(this.LineQueryParams).then((res) => {
        if (res.code == 200) {
          this.LineList = res.data.result
          
        }
      })
    },


    // //查看现有班次
    seeFlight() {
      this.seeNow = true

    },
    //得到存进去的日历数据
    getDownList() {
      listCCalendar(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.downList = res.data.result
        }
      })
    },
//根据日期查列表
    getDetailList() {
   var a = JSON.parse(JSON.stringify(this.selectedDates))
   let c = a.join(",")
   
      getDetail({AttendanceDate : c }).then((res) => {
        if (res.code == 200) {
          this.detailList = res.data
       
    
        }
      })
    },
  //  得到排班的日期

    getFlightData() {
   
     let b = '000004' ;
     let a = '000003' ;
      getAttendanceData({ flightNo: b }).then((res) => {
        if (res.code == 200) {
          this.FlightData = res.data

          for (let i = 0; i < this.FlightData.length; i++) {
            this.arr.push(this.FlightData[i].attendanceDate)
          }

          for (let i = 0; i < this.res.length; i++) {
            for (let j = 0; j < this.res[i].length; j++) {
              if (this.arr.includes(this.res[i][j].date)) {
                this.res[i][j].isSele = true
              }
            }
          }
        }
      })

      getAttendanceData({ flightNo: a }).then((res) => {
        if (res.code == 200) {
          this.FlightData = res.data

          for (let i = 0; i < this.FlightData.length; i++) {
            this.arr.push(this.FlightData[i].attendanceDate)
          }

          for (let i = 0; i < this.res.length; i++) {
            for (let j = 0; j < this.res[i].length; j++) {
              if (this.arr.includes(this.res[i][j].date)) {
                this.res[i][j].isSele = true
              }
            }
          }
        }
      })
    },


    //切换滑动和点击
    TwoChange() {
      if ((this.selectedMode = this.selectMode === 'click')) {
        this.selectedMode = this.selectMode === 'move'
      }
      if ((this.selectedMode = this.selectMode === 'move')) {
        this.selectMode = 'click'
        this.selectedMode = this.selectMode === 'click'
      }
    },
    /** 添加出勤 */
    handleAdd() {
      var a = JSON.parse(JSON.stringify(this.selectedDates))
      var b = JSON.parse(JSON.stringify(this.form.flightNo))
      var c = JSON.parse(JSON.stringify(this.form.lineCode))
      // var a = JSON.parse(JSON.stringify(this.form.flightNo)) // 选中的日期数据
      // //var b = JSON.parse(JSON.stringify(this.form.flightNo)) // 表单的编号
      const mainTable = Object.assign({
        flightNo: b, //['1','2']
        attendanceDate: a, //[{}]
        lineCode: c,
        createBy: null,
        createTime: null,
        updateBy: null,
        upadteTime: null,
        isAttendance: 1,
      })

      addCCalendar(mainTable)
        .then((res) => {
          this.msgSuccess('添加成功')
        })

        .catch((err) => {
          //TODO 错误逻辑
        })
    },
    //班次列表查询
    getFlightList() {
      GetBFlightAll().then((res) => {
        if (res.code == 200) {
          this.flightList = res.data
          this.glFlightList = this.flightList.filter((x) => x.status == 0)

          for (let i = 0; i < this.glFlightList.length; i++) {
            this.arrFlightCode.push(this.glFlightList[i].flightCode)
          }
         
          for (let i = 0; i < this.flightList.length; i++) {
           
          }
        }
      })
    },
    handleGetDays(year, month, startOfWeek) {
      this.showDays = []
      this.days = getDaysInMonth(year, month)
      let firstDayOfWeek = new Date(`${year}-${month + 1}-01`).getDay()

      // 处理周起始日
      const obj = {
        1: '周一',
        2: '周二',
        3: '周三',
        4: '周四',
        5: '周五',
        6: '周六',
        0: '周日',
      }
      const firstDayInCN = obj[firstDayOfWeek]
      const index = this.weeks.indexOf(firstDayInCN)

      if (firstDayOfWeek === 0) {
        // 星期天为0 星期一为1 ,以此类推
        firstDayOfWeek = 7
      }

      this.prevDays = handleCrateDate(year, month, 1, index + 1, 'prev')
      this.rearDays = handleCrateDate(year, month, 1, 42 - this.days - index, 'rear')

      this.curDays = handleCrateDate(year, month, 1, this.days)
      this.showDays.unshift(...this.prevDays)
      this.showDays.push(...this.curDays)
      this.showDays.push(...this.rearDays)
      this.res = this.handleFormatDates(this.showDays)
    },
    handleFormatDates(arr, size = 7) {
      // 传入长度42的原数组,最终转换成二维数组
      const arr2 = []
      for (let i = 0; i < size; i++) {
        const temp = arr.slice(i * size, i * size + size)
        arr2.push(temp)
      }

      return arr2
    },
    handleTableHead(start) {
      const sliceDates = this.weeks.splice(start - 1)
      this.weeks.unshift(...sliceDates)
    },
    handleItemClick(item, i, j) {
      // if (!this.canSelect )
      //  {
      //    return
      // }

      if (this.selectedMode) {
        this.$nextTick(() => {
          // this.$set(this.res[i][j], 'isSelected', )
          this.res[i][j].isSelected = !this.res[i][j].isSelected
          if (this.res[i][j].isSelected) {
            this.selectedDates.push(this.res[i][j].date)
            this.selectedDates = Array.from(new Set(this.selectedDates))
            var a = JSON.parse(JSON.stringify(this.selectedDates))
            var b = JSON.parse(JSON.stringify(this.form.flightNo))
            var c = JSON.parse(JSON.stringify(this.form.lineCode))
            //item.isSee = true
            console.log(a, '这个是日期数组', b, '这个是班次编号', c, '线体编号')
            this.getDetailList();
          } else {
            this.selectedDates.splice(this.selectedDates.indexOf(item.date), 1)
          }
          this.$emit('dateSelected', this.selectedDates)
        })
      } else {
        // 滑动模式下,第一次点击是起始,第二次点击是结束
        const index = i * 7 + j
        this.canMove = true
        if (this.moveIndex.length === 1) {
          this.canMove = false
        }
        if (this.moveIndex.length === 2) {
          this.showDays.forEach((item) => {
            item.isSelected = false
            item.isRangeSelected = false
          })
          this.canMove = true
          this.moveIndex.length = 0
        }
        this.moveIndex.push(index)
        this.moveIndex.sort((a, b) => a - b)
        this.selectedDates = this.showDays.slice(this.moveIndex[0], this.moveIndex[1] + 1)
        this.selectedDates = Array.from(new Set(this.selectedDates))
        this.selectedDates.length !== 0 && this.$emit('dateSelected', this.selectedDates)
      }
    },
    handleItemMove(data, i, j) {
      if (this.canMove && !this.selectedMode) {
        const index = i * 7 + j
        this.showDays.forEach((item) => {
          item.isSelected = false
          item.isRangeSelected = false
        })
        // 让第一个日期和最后一个日期显示蓝色高亮
        this.showDays[index].isSelected = true
        this.showDays[this.moveIndex[0]].isSelected = true

        // 不同情况的判断,当用户的鼠标滑动进日期的索引小于起始日期的索引,要做if else处理
        if (this.moveIndex[0] < index) {
          for (let i = this.moveIndex[0] + 1; i < index; i++) {
            this.showDays[i].isRangeSelected = true
          }
        } else {
          for (let i = index + 1; i < this.moveIndex[0]; i++) {
            this.showDays[i].isRangeSelected = true
          }
        }
      }
    },
    handleQuickChange(type) {
      if (type === 'prev') {
        this.curMonth--
        console.log(this.curMonth)
        if (this.curMonth === -1) {
          this.curMonth = 11
          this.curYear -= 1
        }
      } else if (type === 'next') {
        this.curMonth++
        if (this.curMonth === 12) {
          this.curMonth = 0
          this.curYear += 1
        }
      }
    },
   //  颜色样式 // 遍历班次信息
    // tableRowClassName({ row, rowIndex }) {
    // for(let i = 0 ; i < this.glFlightList.length ; i++)
    // { 
    //    var matchColor = this.flightList[i].color 
    //   if (row.flightNo.indexOf(this.flightList[i].flightCode) != -1) {
    //     return 'box'
    //   }
    //   debugger
    //   var box=this.$el.getElementByClassName('box'); 
    //   box.style.backgroundColor= matchColor;
    // }

    // },
   
      /** 删除按钮操作 */
      deletes() {
      this.$confirm('是否确认删除选中的班次?')
        .then(function () {
          return DeleteFlightNo( {flightNo : '000003'})
        })
        .then(() => {
          this.getDownList() ;
          this.msgSuccess("删除成功");

        })
        .catch(() => {});
    },
    
  },
}
</script>

<style scoped lang="scss">
.calendar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
.calendar-table {
  table-layout: fixed;
  border-collapse: collapse;
  transition: 0.3s;
  thead tr {
    height: 50px;
  }
  tbody tr {
    &:first-child td {
      border-top: 1px solid #82c0f3;
    }
    td {
      cursor: pointer;
      border-right: 1px solid #82c0f3;
      border-bottom: 1px solid #82c0f3;
      &:first-child {
        border-left: 1px solid #82c0f3;
      }
    }
  }
}

.notCurMonth {
  color: #c0c4cc;
}
.currentDay {
  color: #fff;
  background-color: #82c0f3;
}
.selectDay {
  color: #fff;
  background-color: #409eff;
}
.rangeSelectd {
  color: #606266;
  background-color: #dee2e9;
}
.weekend {
  color: #f73131;
}
.seeNow {
  color: #fff;
  background-color: rgba(103, 194, 58, 1);
}
button.el-button.el-button--danger.el-button--default {
    margin-left: -44px;



}
    

.box{   
            background-color: red;
        }
</style>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue是一种用于构建用户界面的渐进式JavaScript框架,它可以很方便地用来创建富交互的日历应用程序并且记录事件。 首先,我们可以使用Vue的组件化思想来创建一个日历组件。这样,我们可以将日历拆分为若干个小组件,分别处理不同的功能。比如,我们可以创建一个日期组件,用于显示单个日期的信息。 接着,我们可以使用Vue的数据绑定功能来动态地渲染日历的日期。我们可以通过定义一个月份变量,并计算出该月份的所有日期。然后,在日历组件中,我们可以循环遍历这些日期,并使用日期组件来显示每一天的信息。 为了记录事件,我们可以在每个日期上添加一个事件列表。在Vue中,我们可以使用数组来存储对应日期的事件,然后在日期组件中,根据事件数组的长度来显示有多少个事件。同时,我们可以在日期组件中添加一个输入框和按钮,用于添加新的事件。当用户点击按钮时,我们可以通过Vue的事件处理机制将事件添加到对应日期的事件数组中。 另外,为了提高用户体验,我们可以使用Vue的过渡效果来为日历添加动画效果。比如,当用户切换月份或者添加事件时,我们可以使用过渡效果来实现日历的平滑切换和事件的渐入效果。 总结起来,使用Vue可以轻松地构建一个日历并记录事件的应用程序。我们可以用组件来重用代码,使用数据绑定来动态渲染日历,使用事件处理来添加事件,使用过渡效果来提高用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是貔貅喔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值