vue2 使用element-ui table组件以当前月的每天为表头动态生成表

使用element-ui table开发表格时,遇到通过选择的月份 获取月份的每一天为表头,同时后端赋值
此时就需要动态生成表头,因为每月天数不是固定的。

如图:

image.png

直接上代码

  1. html
<el-table
        id="print"
        v-loading="loading"
        size="mini"
        border
        :max-height="tableHeight"
        :header-cell-style="{ background: '#006fbf', color: '#fff' }"
        :cell-style="addClass"
        :data="tableOldData" >
      <el-table-column label="录入科目" fixed="left" width="200px" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          <div>{{ row.decs}}</div>
        </template>
      </el-table-column>
      <el-table-column label="录入口径" width="180px" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          <div>{{ row.lrkj }}</div>
        </template>
      </el-table-column>
      <el-table-column :label="this.time.month+1 + '月合计'" width="180px" :show-overflow-tooltip="true">
        <template slot-scope="{row}">
          <div>{{ row.monthSum == 0 ? '-' : row.monthSum  }}</div>
        </template>
      </el-table-column>
      <el-table-column :label="item.month + '月' + item.day + '日'" v-for="item in visibleCalendar"
                       :key="item.keyDate" width="100">
        <template slot-scope="{row}">
          {{ getDateName(row.dayData,item.keyDate) }}
        </template>
      </el-table-column>
    </el-table>

重点看里

 <el-table-column :label="item.month + '月' + item.day + '日'" v-for="item in visibleCalendar"
                       :key="item.keyDate" width="100">
        <template slot-scope="{row}">
          {{ getDateName(row.dayData,item.keyDate) }}
        </template>
</el-table-column>

通过computed中方法得到当前时间的年月日以及自定义字段名称信息

computed: {
    // 获取当前月份时间日期
    visibleCalendar: function () {
      // 获取今天的年月日
      const today = new Date()
      today.setHours(0)
      today.setMinutes(0)
      today.setSeconds(0)
      today.setMilliseconds(0)

      const calendarArr = []
      // 获取当前月份第一天
      const currentFirstDay = new Date(this.time.year, this.time.month, 1)
      const d = new Date(this.time.year, this.time.month + 1, 0);
      const currentMonthDay = d.getDate()
      // 获取第一天是周几
      const weekDay = currentFirstDay.getDay() === 0 ? 7 : currentFirstDay.getDay()
      // 以当前月份的第一天作为第一天
      const startDay = currentFirstDay.getTime()

      // 利用当前月份一共有多少天来显示日历表头
      for (let i = 0; i < currentMonthDay; i++) {
        const date = new Date(startDay + i * 24 * 3600 * 1000)
        let Y = date.getFullYear() + "-";//年
        let M = (date.getMonth() + 1 < 10  ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";//月
        let D =  (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + "";//日
        let nowDate = Y + M + D
        //最终返回的数据,包含年月日以及自定义字段	  
        calendarArr.push({
          date: new Date(startDay + i * 24 * 3600 * 1000),
          year: date.getFullYear(),
          month: date.getMonth()+1,
          day: date.getDate(),
          keyDate: nowDate,
        })
      }
      return calendarArr
    }
  },

后端数据赋值的话还需要转换,因为列表头不是固定的需要匹配到后端传过来的数据,此例中根据keyDate来匹配后端的值

    //tableData就是得到的接口返回的数据
    //tableNewData就是通过计算属性得到的最终的渲染表格的数据集合
    //visibleCalendar是在上一步骤中得到的日期时间集合
    //keyDate是日期时间集合里面的一个字段,用来作为判断依据的
    tableNewData: function (){
      const newData = []
      this.tableData.forEach(item => {
        const newItem = { ...item }
        const dayData = newItem.dayData
        newItem.dayData = {}
        this.visibleCalendar.forEach(date => {
          let newData = date.keyDate
          if (dayData[newData]) {
            newItem.dayData[newData] = dayData[newData]
          } else {
            newItem.dayData[newData] = {}
          }
        })
        newData .push(newItem)
      })
      return newData 
    },

后端返回值:

[
    {
        "decs":"①进仓卸货(件)",
        "lrkj":"当日到达仓库完成卸货的件数",
        "dayData":{
            "2023-09-01":{
                "name":"测试1",
                "num":256
            },
            "2023-09-02":{
                "name":"测试1",
                "num":256
            },
            "2023-09-03":{
                "name":"测试1",
                "num":256
            },
            "2023-09-04":{
                "name":"测试1",
                "num":256
            },
            "2023-09-05":{

            },
            "2023-09-06":{

            },
            "2023-09-07":{

            },
            "2023-09-08":{

            },
            "2023-09-09":{

            },
            "2023-09-10":{

            },
            "2023-09-11":{

            },
            "2023-09-12":{

            },
            "2023-09-13":{

            },
            "2023-09-14":{

            },
            "2023-09-15":{

            },
            "2023-09-16":{

            },
            "2023-09-17":{

            },
            "2023-09-18":{

            },
            "2023-09-19":{

            },
            "2023-09-20":{

            },
            "2023-09-21":{

            },
            "2023-09-22":{

            },
            "2023-09-23":{

            },
            "2023-09-24":{

            },
            "2023-09-25":{

            },
            "2023-09-26":{

            },
            "2023-09-27":{

            },
            "2023-09-28":{

            },
            "2023-09-29":{

            },
            "2023-09-30":{

            }
        }
    },
    {
        "decs":"①进仓卸货(件)",
        "lrkj":"当日到达仓库完成卸货的件数",
        "dayData":{
            "2023-09-02":{
                "name":"测试2",
                "num":223
            }
        }
    },
    {
        "decs":"①进仓卸货(件)",
        "lrkj":"当日到达仓库完成卸货的件数",
        "dayData":{
            "2023-09-03":{
                "name":"测试3",
                "num":456
            }
        }
    }
]

注意scope中getDateName方法,因为传进来的是对象,需要根据日期获取其中的具体值
其中参数scope就是

"2023-09-03":{
                "name":"测试3",
                "num":456
            }

参数s就是keyDate的值

    /** 格式化并赋值 */
    getDateName(scope,s){
      return  scope[s].dataNum
    },

以上过程查阅网上教程并自己优化得到。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改 Element UI 组件的样式,可以使用自定义样式类和覆盖默认样式。以下是一些基本的步骤: 1. 在 Vue 组件中引入 Element UI 组件。 ```javascript <template> <el-table :data="tableData"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </template> <script> import { ElTable, ElTableColumn } from 'element-ui'; export default { components: { ElTable, ElTableColumn, }, data() { return { tableData: [ { name: 'John Doe', age: 30, address: '123 Main St', }, // ... ], }; }, }; </script> ``` 2. 在组件中定义自定义样式类。 ```css .table-wrapper { border: 1px solid #ccc; } .el-table__header { background-color: #f5f5f5; } .el-table__body { background-color: #fff; } ``` 3. 将自定义样式类应用于组件。 ```javascript <template> <div class="table-wrapper"> <el-table :data="tableData" class="my-table"> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> <el-table-column prop="address" label="Address"></el-table-column> </el-table> </div> </template> <style> .my-table { width: 100%; } .el-table__header { background-color: #f5f5f5; } .el-table__body { background-color: #fff; } </style> ``` 在上面的示例中,我们在 `div` 元素上应用了 `.table-wrapper` 样式类,并在组件上应用了 `.my-table` 样式类。然后,我们覆盖了组件的默认样式,例如表头体的背景颜色。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值