iis服务器助手广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Avue和Element-UI动态三级表头的实现
  • 694
分享到

Avue和Element-UI动态三级表头的实现

2024-04-02 19:04:59 694人浏览 八月长安
摘要

目录AVue配置方式Element-UI三级表头动态写法需求场景: 业务方希望有表格可以体现员工的考勤信息,要具体到上午下午,统计司机上下班打卡所产生的数据。产品提出想做成三级表头根

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

效果如下:

在这里插入图片描述

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 动态三级表头内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Avue和Element-UI动态三级表头的实现

本文链接: https://www.lsjlt.com/news/164901.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Avue和Element-UI动态三级表头的实现
    目录Avue配置方式Element-UI三级表头动态写法需求场景: 业务方希望有表格可以体现员工的考勤信息,要具体到上午下午,统计司机上下班打卡所产生的数据。产品提出想做成三级表头根...
    99+
    2024-04-02
  • avue-crud多级复杂的动态表头怎么实现
    这篇文章主要讲解了“avue-crud多级复杂的动态表头怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“avue-crud多级复杂的动态表头怎么实现”吧!Avue.js 是基于现有的e...
    99+
    2023-06-25
  • avue-crud多级复杂的动态表头的实现示例
    目录前言后台数据拼接前台数据展示页面效果展示Avue.js 是基于现有的element-ui库进行的二次封装,从而简化一些繁琐的操作,核心理念为数据驱动视图,主要的组件库针对tabl...
    99+
    2024-04-02
  • vue element-ui动态横向统计表格的实现
    目录element-ui动态横向统计表格关于element-ui表格问题表格图片显示问题表格中多出一条线element-ui动态横向统计表格 表格结构 <el-table ...
    99+
    2022-11-13
    vue element-ui 动态横向统计表格 vue element-ui 动态表格
  • element-ui实现表格边框的动态切换并防抖
    目录需求实现过程解决抖动抖动原因解决抖动的实现过程再优化后记固定列需求 需求是这样的: 先前的需求,要求表格按UI设计图来,表格无边框。新来的需求,要求能支持表格列宽的能够支持拖动。...
    99+
    2022-11-13
    element-ui表格 表格边框动态切换 表格边框防抖
  • element 表格多级表头子列固定的实现
    element 中 table 固定列使用fixed 属性;但是多级表头时只能固定第一列; 设置对应子列表头的宽度和父系表头的宽度可以使对应的列固定; 不需要固定的列则不需要设置宽度...
    99+
    2024-04-02
  • vue如何实现基于element-ui的三级CheckBox复选框
    小编给大家分享一下vue如何实现基于element-ui的三级CheckBox复选框,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧...
    99+
    2024-04-02
  • Element怎么实现动态表格
    本篇内容介绍了“Element怎么实现动态表格”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!目录【代码背景】【代码实现】#1# -> ...
    99+
    2023-06-20
  • element表格多级表头子列固定怎么实现
    这篇“element表格多级表头子列固定怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“element表格多级表头子列...
    99+
    2023-07-02
  • vue实现三级联动动态菜单
    本文实例为大家分享了vue实现三级联动动态菜单的具体代码,供大家参考,具体内容如下 三级联动动态菜单展示:一级菜单选中,生成二级菜单数据,二级菜单选中,生成三级菜单数据(根据上一级菜...
    99+
    2024-04-02
  • Element实现动态表格的示例代码
    目录【代码背景】【代码实现】#1# -> 代码复用的基础是你需要一个可复用的组件#2# -> 在展示页面使用动态表格组件#3# -> 如何给动态表格根据需求动态添加...
    99+
    2024-04-02
  • element表格去掉表头的实现方法
    文档提示用属性show-header <el-table :data="tableData1" :span-method...
    99+
    2024-04-02
  • element-ui表单提交自动清空隐藏表单值实现
    目录需求的开始初步思路:标记方案有别的超简单的方案?继续标记方案分析如何实现具体如何实现在表单项组件添加显隐逻辑事件结尾 需求的开始 一个表单,里面有很多表单项,然后需求通过特定的条...
    99+
    2024-04-02
  • vue中怎么利用mint-ui实现三级联动
    本篇文章给大家分享的是有关vue中怎么利用mint-ui实现三级联动,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Ⅰ 、html组件<d...
    99+
    2024-04-02
  • poi+easypoi实现表头多层循环,多级动态表头、树形结构动态表头、纵向合并单元格、多个sheet导出
    前言 我前面也写过几篇关于easypoi复杂表格导出的文章,什么一对多纵向合并、多级表头、动态表头、多个sheet等,这些我写那几篇文章之前做项目都遇到过,并且都实现出来了。 感兴趣的可以看看: ea...
    99+
    2023-10-02
    java easypoi poi
  • 关于Element-UI可编辑表格的实现过程
    目录一、 可编辑单元格的实现二、 Input框编辑时动态查询(下拉列表)三、 点击图标显示下拉、点击图标显示Dialog的实现一、 可编辑单元格的实现 实现效果:点击可编辑 实现原...
    99+
    2024-04-02
  • vue element 表头添加斜线的实现代码
    <template> <div class="app-container"> <el-table :data="tableD...
    99+
    2024-04-02
  • Element-ui Layout布局(Row和Col组件)的实现
    目录基本说明以及用法 Row组件的分析render函数源码分析Col组件的分析组件分析响应式布局我们在实际开发中遇到一些布局的时候会用到Layout布局,这个布局只要配置一些参数就能...
    99+
    2024-04-02
  • 使用Element实现表格表头添加搜索图标和功能
    目录Element 表格表头添加搜索图标和功能主要实现 table的slot=‘header’element ui表格el-tabel给表头加icon图标设置...
    99+
    2024-04-02
  • 基于element-ui表格的二次封装怎么实现
    这篇文章主要讲解了“基于element-ui表格的二次封装怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于element-ui表格的二次封装怎么实现”吧!在项目中经常会使用到ele...
    99+
    2023-07-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作