• Avue和Element-UI动态三级表头


    Avue和Element-UI动态三级表头

    需求场景: 业务方希望有表格可以体现员工的考勤信息,要具体到上午下午,统计司机上下班打卡所产生的数据。产品提出想做成三级表头根据页面查询条件的年月去动态生成表格的表头。三级分别是月份日期,对应的星期,以及每天的上午以及下午。

    效果如下:
    在这里插入图片描述

    Avue配置方式

    1. 通过对avue-crud组件的option的配置如下:
    {
      label: `${$this.month}${$this.dateList[0].ri}`,  // 月份
      headerAlign: 'center',
      children: [
       {
         label: `星期${$this.dateList[0].xq}`,
         headerAlign: 'center',
         children: [
          {
            label: '上午',
            prop: 'oneAmAttendance',
            headerAlign: 'center',
            props: { label: 'name', value: 'code' },
            type: 'select',
            dicData: $this.attendanceTypeList,
            formatter: (row, value, label, column) => {
              try {
                let satData = ''
                $this.attendanceTypeList.find((item) => {
                  if (item.code === row.oneAmAttendance) {
                    satData = item.name
                  }
                })
                return satData
              } catch (e) {
                console.log(e)
              }
            }
           },
           {
             label: '下午',
             prop: 'onePmAttendance',
             headerAlign: 'center',
             props: { label: 'name', value: 'code' },
             type: 'select',
             dicData: $this.attendanceTypeList,
             formatter: (row, value, label, column) => {
               try {
                 let satData = ''
                 $this.attendanceTypeList.find((item) => {
                   if (item.code === row.onePmAttendance) {
                     satData = item.name
                   }
                 })
                 return satData
               } catch (e) {
                console.log(e)
               }
             }
           }
         ]
        }
      ]
     },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    1. 在data中声明需要的变量以及获取每个月天数以及对应星期的方法
    
    data(){
      return {
        dateList: [], // 日期list
        month: 0, // 选中的月份
        dayNum: 0 // 选中月的天数
      }
    }
    
    created(){
      this.montInfo(GetYearLastMonth())
      // 当前月的天数
      const arr = GetYearLastMonth().split('-')
      this.month = parseInt(arr[1])
      this.dayNum = this.dayNumFn(parseInt(arr[0]), parseInt(arr[1]))
    }
    methods(){
          // 月日以及对应的星期
       montInfo(res) {
          /**
    	 * 获取一个月多少天,并获取月初星期几
    	 */
          const daxier = ['一', '二', '三', '四', '五', '六', '日'];
          const date = res ? new Date(res) : new Date()
          const y = date.getFullYear()
          const m = date.getMonth() + 1
          var date2 = new Date(y, m, 0)
          var rq = date2.getDate() // 日 本月最后一天
    
          var xq = date2.getDay(); // 星期 本月第一天星期几  new Date(0).getDay()
          var rq2 = rq % 7
          if (rq2 === 0) {
            xq = rq2 + 1
          } else {
            if (rq2 > xq) xq += 7
            xq = xq - rq2
          }
    
          var data = [];
          for (var i = 1; i <= rq; i++) {
            data.push({
              'ri': i,
              'xq': daxier[xq]
            })
    
            xq = (++xq === 7) ? 0 : xq
          }
          this.dateList = data
        },
        // 获取选中月的天数
        dayNumFn(year, month) {
          return new Date(year, month, 0).getDate()
        },
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    1. 根据查询条件去切换表头
    {
      label: '年月',
      search: true,
      hide: true,
      searchPlaceholder: '请选择年月',
      searchClearable: false,
      prop: 'yearMonth',
      type: 'month',
      // 日期组件格式化
      format: 'yyyy-MM', // 展示值
      // 单元格格式化
      valueFormat: 'yyyy-MM', // value
      searchDefault: GetYearLastMonth(),
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() > new Date(GetYearLastMonth()).getTime()
        }
      },
      // 查询条件月份切换的同事重新渲染表头
      change(value) {
        // 当前月的天数
        $this.montInfo(value.value)
        const arr = value.value.split('-')
        $this.month = parseInt(arr[1])
        $this.dayNum = $this.dayNumFn(parseInt(arr[0]), parseInt(arr[1]))
      }
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    1. 因为不同的月份日期有不同,比如2月只有28天而1月有31天。所以大于28的日期需要单独处理一下
    {
                label: $this.dayNum > 28 ? `${$this.month}${$this.dateList[28].ri}` : '',
                headerAlign: 'center',
                hide: $this.dayNum < 28,
                children: [
                  {
                    label: $this.dayNum > 28 ? `星期${$this.dateList[28].xq}` : '',
                    headerAlign: 'center',
                    children: [
                      {
                        label: '上午',
                        prop: 'twentyNineAmAttendance',
                        headerAlign: 'center',
                        props: { label: 'name', value: 'code' },
                        type: 'select',
                        dicData: $this.attendanceTypeList,
                        formatter: (row, value, label, column) => {
                          try {
                            let satData = ''
                            $this.attendanceTypeList.find((item) => {
                              if (item.code === row.twentyNineAmAttendance) {
                                satData = item.name
                              }
                            })
                            return satData
                          } catch (e) {
                            console.log(e)
                          }
                        }
                      },
                      {
                        label: '下午',
                        prop: 'twentyNinePmAttendance',
                        headerAlign: 'center',
                        props: { label: 'name', value: 'code' },
                        type: 'select',
                        dicData: $this.attendanceTypeList,
                        formatter: (row, value, label, column) => {
                          try {
                            let satData = ''
                            $this.attendanceTypeList.find((item) => {
                              if (item.code === row.twentyNinePmAttendance) {
                                satData = item.name
                              }
                            })
                            return satData
                          } catch (e) {
                            console.log(e)
                          }
                        }
                      }
                    ]
                  }
                ]
              },
              {
                label: $this.dayNum > 28 ? `${$this.month}${$this.dateList[29].ri}` : '',
                headerAlign: 'center',
                hide: $this.dayNum < 30,
                children: [
                  {
                    label: $this.dayNum > 28 ? `星期${$this.dateList[29].xq}` : '',
                    headerAlign: 'center',
                    children: [
                      {
                        label: '上午',
                        prop: 'thirtyAmAttendance',
                        headerAlign: 'center',
                        props: { label: 'name', value: 'code' },
                        type: 'select',
                        dicData: $this.attendanceTypeList,
                        formatter: (row, value, label, column) => {
                          try {
                            let satData = ''
                            $this.attendanceTypeList.find((item) => {
                              if (item.code === row.thirtyAmAttendance) {
                                satData = item.name
                              }
                            })
                            return satData
                          } catch (e) {
                            console.log(e)
                          }
                        }
                      },
                      {
                        label: '下午',
                        prop: 'thirtyPmAttendance',
                        headerAlign: 'center',
                        props: { label: 'name', value: 'code' },
                        type: 'select',
                        dicData: $this.attendanceTypeList,
                        formatter: (row, value, label, column) => {
                          try {
                            let satData = ''
                            $this.attendanceTypeList.find((item) => {
                              if (item.code === row.thirtyPmAttendance) {
                                satData = item.name
                              }
                            })
                            return satData
                          } catch (e) {
                            console.log(e)
                          }
                        }
                      }
                    ]
                  }
                ]
              },
              {
                label: $this.dayNum === 31 ? `${$this.month}${$this.dateList[30].ri}` : '',
                headerAlign: 'center',
                hide: $this.dayNum !== 31,
                children: [
                  {
                    label: $this.dayNum === 31 ? `星期${$this.dateList[30].xq}` : '',
                    headerAlign: 'center',
                    children: [
                      {
                        label: '上午',
                        prop: 'thirtyOneAmAttendance',
                        headerAlign: 'center',
                        props: { label: 'name', value: 'code' },
                        type: 'select',
                        dicData: $this.attendanceTypeList,
                        formatter: (row, value, label, column) => {
                          try {
                            let satData = ''
                            $this.attendanceTypeList.find((item) => {
                              if (item.code === row.thirtyOneAmAttendance) {
                                satData = item.name
                              }
                            })
                            return satData
                          } catch (e) {
                            console.log(e)
                          }
                        }
                      },
                      {
                        label: '下午',
                        prop: 'thirtyOnePmAttendance',
                        headerAlign: 'center',
                        props: { label: 'name', value: 'code' },
                        type: 'select',
                        dicData: $this.attendanceTypeList,
                        formatter: (row, value, label, column) => {
                          try {
                            let satData = ''
                            $this.attendanceTypeList.find((item) => {
                              if (item.code === row.thirtyOnePmAttendance) {
                                satData = item.name
                              }
                            })
                            return satData
                          } catch (e) {
                            console.log(e)
                          }
                        }
                      }
                    ]
                  }
                ]
              },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165

    Element-UI三级表头动态写法

    element-ui的写法相对简单一些,因为配置项没办法进行遍历渲染。

    1. template里面的写法
    <el-table
        :data="tableData"
        style="width: 100%" >
        <el-table-column
          prop="month"
          label="月份"
          width="150"
          header-align="center">
        el-table-column>
        
        <template v-for="(item,index) in dateList" >
          <el-table-column :label="`${month}月${item.ri}日`" header-align="center" :key="'date' + index">
            <el-table-column header-align="center" :label="`星期${item.xq}`" >
              <el-table-column header-align="center" :prop="item.sw" label="上午" width="120" >el-table-column>
              <el-table-column header-align="center" :prop="item.xw" label="下午" width="120" >el-table-column>
            el-table-column>
          el-table-column>
        template>
      el-table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. data中还是声明变量,methods中还是应用和上面类似的方法
    data(){
        return {
          dateList: [], // 日期list
          month: 0, // 选中的月份
          dayNum: 0, // 选中月的天数
        }
      }
      created() {
        this.montInfo(GetYearLastMonth())
        // 当前月的天数
        const arr = GetYearLastMonth().split('-')
        this.month = parseInt(arr[1])
        this.dayNum = this.dayNumFn(parseInt(arr[0]), parseInt(arr[1]))
      },
      methods: {
        // 月日以及对应的星期
        montInfo(res) {
          /**
    	 * 获取一个月多少天,并获取月初星期几
    	 */
          const daxier = ['一', '二', '三', '四', '五', '六', '日'];
          //  这里是每个上午下午展示为不同的变量
          const amArr = ['oneAmAttendance', 'twoAmAttendance', 'threeAmAttendance', 'fourAmAttendance', 'fiveAmAttendance', 'sixAmAttendance', 'sevenAmAttendance', 'eightAmAttendance', 'nineAmAttendance', 'tenAmAttendance', 'elevenAmAttendance', 'twelveAmAttendance', 'thirteenAmAttendance', 'fourteenAmAttendance', 'fifteenAmAttendance', 'oneAmAttendance', 'twoAmAttendance', 'threeAmAttendance', 'fourAmAttendance', 'fiveAmAttendance', 'sixAmAttendance', 'sevenAmAttendance', 'eightAmAttendance', 'nineAmAttendance', 'tenAmAttendance', 'elevenAmAttendance', 'twelveAmAttendance', 'thirteenAmAttendance', 'fourteenAmAttendance', 'fifteenAmAttendance', 'sixteenAmAttendance', 'seventeenAmAttendance', 'eighteenAmAttendance', 'nineteenAmAttendance', 'twentyAmAttendance', 'twentyOneAmAttendance', 'twentyTwoAmAttendance', 'twentyThreeAmAttendance', 'twentyFourAmAttendance', 'twentyFiveAmAttendance', 'twentySixAmAttendance', 'twentySevenAmAttendance', 'twentyEightAmAttendance', 'twentyNineAmAttendance', 'thirtyAmAttendance', 'thirtyOneAmAttendance']
          const pmArr = [
            'onePmAttendance', 'twoPmAttendance', 'threePmAttendance', 'fourPmAttendance', 'fivePmAttendance', 'sixPmAttendance', 'sevenPmAttendance', 'eightPmAttendance', 'ninePmAttendance', 'tenPmAttendance', 'elevenPmAttendance', 'twelvePmAttendance', 'thirteenPmAttendance', 'fourteenPmAttendance', 'fifteenPmAttendance', 'onePmAttendance', 'twoPmAttendance', 'threePmAttendance', 'fourPmAttendance', 'fivePmAttendance', 'sixPmAttendance', 'sevenPmAttendance', 'eightPmAttendance', 'ninePmAttendance', 'tenPmAttendance', 'elevenPmAttendance', 'twelvePmAttendance', 'thirteenPmAttendance', 'fourteenPmAttendance', 'fifteenPmAttendance', 'sixteenPmAttendance', 'seventeenPmAttendance', 'eighteenPmAttendance', 'nineteenPmAttendance', 'twentyPmAttendance', 'twentyOnePmAttendance', 'twentyTwoPmAttendance', 'twentyThreePmAttendance', 'twentyFourPmAttendance', 'twentyFivePmAttendance', 'twentySixPmAttendance', 'twentySevenPmAttendance', 'twentyEightPmAttendance', 'twentyNinePmAttendance', 'thirtyPmAttendance', 'thirtyOnePmAttendance'
          ]
          const date = res ? new Date(res) : new Date()
          const y = date.getFullYear()
          const m = date.getMonth() + 1
          var date2 = new Date(y, m, 0)
          var rq = date2.getDate() // 日 本月最后一天
    
          var xq = date2.getDay(); // 星期 本月第一天星期几  new Date(0).getDay()
          var rq2 = rq % 7
          if (rq2 === 0) {
            xq = rq2 + 1
          } else {
            if (rq2 > xq) xq += 7
            xq = xq - rq2
          }
    
          var data = [];
          for (var i = 1; i <= rq; i++) {
            data.push({
              'ri': i,
              'xq': daxier[xq]
            })
    
            xq = (++xq === 7) ? 0 : xq
          }
          data.map((item, index) => {
            item.sw = amArr[index]
            item.xw = pmArr[index]
          })
          this.dateList = data
        },
        // 获取选中月的天数
        dayNumFn(year, month) {
          console.log(new Date(year, month, 0).getDate())
          return new Date(year, month, 0).getDate()
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    1. element-ui渲染的效果
      element-ui组件渲染的效果
  • 相关阅读:
    《向量数据库指南》——完善产品拼图,GBASE南大通用发布向量数据库
    算法基础:贪心策略
    探索SOCKS5与SK5代理在现代网络环境中的应用
    缓冲区设置
    Java集合之Map
    LeetCode //C - 230. Kth Smallest Element in a BST
    【洛谷 P5250】【深基17.例5】木材仓库 题解(集合+upper_bound)
    独立站怎么设置打折活动?独立站新站怎么优化长尾词?——站斧浏览器
    【三维重建】【深度学习】【数据集】基于COLMAP制作个人Gen6D测试数据集
    笔试练习day01
  • 原文地址:https://blog.csdn.net/socaicaicaicai/article/details/125911581