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

效果如下:

在这里插入图片描述

Avue配置方式

通过对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)
           }
         }
       }
     ]
    }
  ]
 },

在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()
    },
}

根据查询条件去切换表头

{
  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]))
  }
},

因为不同的月份日期有不同,比如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)
                      }
                    }
                  }
                ]
              }
            ]
          },

Element-UI三级表头动态写法

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

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>

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()
    }
  }

element-ui渲染的效果

element-ui组件渲染的效果

到此这篇关于Avue和Element-UI动态三级表头的实现的文章就介绍到这了,更多相关Element 动态三级表头内容请搜索Devmax以前的文章或继续浏览下面的相关文章希望大家以后多多支持Devmax!

Avue和Element-UI动态三级表头的实现的更多相关文章

  1. 使用layui实现左侧菜单栏及动态操作tab项的方法

    这篇文章主要介绍了使用layui实现左侧菜单栏及动态操作tab项的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. 在iOS上绘制扭曲的文本

    使用iOS9及更高版本中提供的标准API,如何在绘制文本时实现扭曲效果?

  3. ios – 如果Element符合给定的协议,则扩展阵列以符合协议

    如果是这样,语法是什么?解决方法Swift4.2在Swift4.2中,我能够使用符合这样的协议的元素扩展数组:

  4. ios – 如何在swift中获取2数组的常见元素列表

    (双关语)编辑:,你可以这样做这个实现是丑陋的.

  5. Swift 函数Count,Filter,Map,Reduce

    Count-统计数量文档示例Filter-条件过滤文档示例-过滤长度大于4的字符串也可以简化Map-映射集合类型,返回数组文档示例同样可以简化Reduce-把数组结合到一起文档示例可以简化进一步简化

  6. Swift语法——Swift Sequences 探究

    今天看到Array的API中有这么一个声明的函数:函数名为extend,所需参数是S类型的newElements,而S首先要实现SequenceType协议。看看APTGeneratorType必须要实现一个函数next(),它的作用就是返回一个Element,注释里说的很清楚:它的作用就是一直返回元素,直到最后。1)Swift调用generate()来生成了一个Generator,这个对象是一个私有的变量即__g;2)__g调用了next()函数,返回了一个optional类型对象element?。这个

  7. Swift 中数组和链表的性能

    尽管如此,我觉得链表的例子非常有意思,而且值得实现和把玩,它有可能会提升数组reduce方法的性能。同时我认为Swift的一些额外特性很有趣:比如它的枚举可以灵活的在对象和具体方法中自由选择,以及“默认安全”。这本书未来的版本可能就会用Swift作为实现语言。拷贝数组消耗的时间是线性的。使用链表还有其他的代价——统计链表节点的个数所需要的时间是统计数组元素个数时间的两倍,因为遍历链表时的间接寻址方式是需要消耗时间的。

  8. Swift中集合类型indexOf(Element)提示错误的解决办法

    简单的竟然出错了!其实看一下错误描述,大概就可以猜到Swift此时不知道你自定义类是如何比较的,如果是Swift内置的各种struct和class就不存在这个问题,比如:解决很简单,添加一个==方法即可:最后补充一下,早期版本的Swift还有一个find函数可以完成类似的功能,但是新版本已经没有该函数了,So你懂的…

  9. swift map reduce 获取下标(index)的方法

    原文:http://stackoverflow.com/questions/28012205/map-or-reduce-with-index-in-swiftYoucanuseenumeratetoconvertasequence(Array,String,etc.)toasequenceoftupleswithanintegercounterandandelementpairedtogethe

  10. Swift中的map 和 flatMap 原理及用法

    map和flatMap是Swift中两个常用的函数,它们体现了Swift中很多的特性。对于简单的使用来说,它们的接口并不复杂,但它们内部的机制还是非常值得研究的,能够帮助我们够好的理解Swift语言。map简介首先,咱们说说map函数如何使用。letnumbers=[1,2,3,4]letresult=numbers.map{$0+2}print//[3,4,5,6]map方法接受一个闭包作为参数,然后它会遍历整个numbers数组,并对数组中每一个元素执行闭包中定义的操作。比如咱们这个例子里面的闭包是讲

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部