基于vue和elementui的简易课表

本文参考基于vue和elementui的课程表_vue实现类似课程表的周会议列表-CSDN博客,原程序在vue3.5.13版本下不能运行,修改两处:

1)slot-cope改为v-slot

2)return 'background-color:rgb(24 144 255 / 80%);color: #fff; border-radius:10px' 改为:

return {'background-color':'rgb(24 144 255 / 80%)', 'color': '#fff', 'border-radius':'20px'}

改进几处:

1)简化表格生成算法makeTable;

2)改进数据填表算法mergeData;

3)改进单元格合并算法objectSpanMethod,由原来的100多行,简化为13行

4)增加中午两节课

源程序如下:

javascript 复制代码
<template>
  <div>
    <div class="panel">
        <el-table :data="timetable" :span-method="objectSpanMethod" border=true
                :header-cell-style="{background:'#d9e5fd', color:'black'}"
                :cell-style="tableCellStyle"
        >
        <el-table-column prop="sjd" label="📅" width="60" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.sjd.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="jc" label="节次" width="60" align="center">
        </el-table-column>
        <el-table-column prop="mon" label="星期一" width="95" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.mon.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="tue" label="星期二" width="95" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.tue.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="wed" label="星期三" width="95" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.wed.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="thu" label="星期四" width="95" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.thu.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="fri" label="星期五" width="95" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.fri.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="sat" label="星期六" width="70" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.sat.course"></div>
          </template>
        </el-table-column>
        <el-table-column prop="sun" label="星期日" width="70" align="center">
          <template v-slot="scope">
            <div v-html="scope.row.sun.course"></div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { colProps } from 'element-plus';
export default {
  props: {length: {type: [String, Number],default: 14}}, // 总节次
  data () {
    return {
    // 课程表数据
    timetable: [],
    events: [
      {xq: 0,course: '上午',start: 1,last:4},
      {xq: 0,course: '中午',start: 5,last:2},
      {xq: 0,course: '下午',start: 7,last:5},
      {xq: 0,course: '晚上',start: 12,last:3},
      {
      xq: 5,start: 1,last:2,
      course: '👨‍🏫<br>·软件工程<br>·3-4节<br>·计师23级<br>·8教406室<br>·1-16周'
      },
      {
      xq: 1, start: 7,last:2,
      course: '🔬<br>·软工实验<br>·11-12节<br>·计师23级<br>·25教704室<br>·3-14周'
      },
      {
      xq: 5,start: 7,last:3,
      course: '👨‍👩‍👦‍👦<br>·集中学习<br>·7-8节<br>·计师23级<br>·25教1-1室<br>·每月第1周'
      }
    ],
    weeks: ['sjd','mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'],
  }
  },
  mounted () {
    this.mergeData()
  },
  created () {
    this.makeTimetable()
  },
  methods: {
    // 单元格添加背景色
    tableCellStyle (row) {
      if (row.row[row.column.property].course !== undefined) {
        return {'background-color':'rgb(24 144 255 / 80%)', 'color': '#fff', 'border-radius':'20px'}
      }
    },
    // 构造课程表完整数据
    makeTimetable () {
      this.timetable = []
      for (let i = 0; i < this.length; i++) {
        let oneRow = {sjd: {},jc: i + 1,mon: {},tue: {},wed: {},thu: {},fri: {},sat: {}, sun: {}}
        this.timetable.push(oneRow)
      }
    },
    mergeData () {
      // 合并数据
        for (let i = 0; i < this.events.length; i++) {
          // 获取星期几
          let week = this.weeks[this.events[i].xq ]
          console.log(this.events[i].last);
          for(var j=0;j< this.events[i].last ;j++)
            this.timetable[this.events[i].start - 1+j][week] = this.events[i]
        }
        console.log(this.timetable);
    },
    objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
      var obj={rowspan: 1,colspan: 1};
      if( row[column.property].course!== undefined){
          if(rowIndex===row[column.property].start-1){
            obj = {
                rowspan: row[column.property].last,
                colspan: 1
            }
          }
          else
            obj = {rowspan: 0,colspan: 0};
      }
      return obj;
    },  
  }
}
</script>

调用:

html 复制代码
<timetable  :length="14" > </timetable>
相关推荐
前端小巷子25 分钟前
Web开发中的文件上传
前端·javascript·面试
翻滚吧键盘1 小时前
{{ }}和v-on:click
前端·vue.js
上单带刀不带妹1 小时前
手写 Vue 中虚拟 DOM 到真实 DOM 的完整过程
开发语言·前端·javascript·vue.js·前端框架
杨进军2 小时前
React 创建根节点 createRoot
前端·react.js·前端框架
ModyQyW2 小时前
用 AI 驱动 wot-design-uni 开发小程序
前端·uni-app
说码解字2 小时前
Kotlin lazy 委托的底层实现原理
前端
Q_970956392 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
爱分享的程序员3 小时前
前端面试专栏-算法篇:18. 查找算法(二分查找、哈希查找)
前端·javascript·node.js
翻滚吧键盘3 小时前
vue 条件渲染(v-if v-else-if v-else v-show)
前端·javascript·vue.js
vim怎么退出3 小时前
万字长文带你了解微前端架构
前端·微服务·前端框架