使用element 实现一个嵌套表格

今天和之前的同事聊天,说他们公司提了一个需求,类似于这样

但是呢 没有数据的地方不能显示空白,要显示表格 讨论了一下得出来一个解决方案
使用嵌套表格,首先创建两个数组 tableHead 和 tableData 存储表头数据和表格数据,
 

tableHead:[
        {type:'dong',name:'东部战区'},
        {type:'xi',name:'西部战区'},
        {type:'nan',name:'南部战区'},
        {type:'bei',name:'北部战区'},
        {type:'zhong',name:'中部战区',},
      ],
      tableData: [
        {
          type: '5g设备',
          dong: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 },
            { subject: '英语', score: 85 },
            { subject: '英语', score: 85 },
          ],
          xi: [
            { subject: '语文', score: 80 },
            { subject: '--', score: '--' },
          ],
          nan: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
          bei: [
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
          zhong: [
          
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
        },
        {
          type: '4g设备',
          dong: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
          
         
          bei: [
            { subject: '语文', score: 80 },
           
          ],
          zhong: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
          ],
        },
        // ... 可以添加更多的数据项
      ]

在外层的table使用tableData的数据,设置第一列的表头,第二列之后的表头通过便利tableHead获取,在第二个el-table-column里边嵌套一个table,此时的scope.row就是tableData数组中的对象,
 

<el-table :data="tableData" style="width: 100%" border :cell-style="{verticalAlign:'baseline',padding:0,}" height="100%">
    <el-table-column align="center" prop="type" label="设备类型" width="180"></el-table-column>
    <el-table-column align="center" v-for="item in tableHead" :key="item.type" :label="item.name">
      <template slot-scope="scope" style="height: 100%;">

          <el-table :data="scope.row.dong"  :show-header="false" height="100%">
            <el-table-column align="center" prop="subject" label="编号">
              <template slot-scope="scope2">
                {{ scope.row[item.type][scope2.$index]?scope.row[item.type][scope2.$index].subject:'' }}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="score" label="天数">
              <template slot-scope="scope2">
                {{ scope.row[item.type][scope2.$index]?scope.row[item.type][scope2.$index].score:'' }}
              </template>
            </el-table-column>
          </el-table>
      </template>
    </el-table-column>
  </el-table>

因为不允许出现空白,所以在渲染列表的时候必须用对象的五个数组中长度最长的那个渲染(这里使用的是scope.row.dong),在填充数据的时候,使用scope.row[item][scoope2.$index]的方式去获取当前战区的数据而不是东部战区的数据(虽然渲染的时候使用的是scope.row.dong,但是数据还是要用各自战区的数据),首先判断scope.row[item.type][scope2.$index]是否存在,如果存在就使用它,不存在的话就把数据值改为空,这样就解决了没有数据的时候显示“暂无数据”的问题,

最后结果如下:

完整代码:

<template>
  <el-table :data="tableData" style="width: 100%" border :cell-style="{verticalAlign:'baseline',padding:0,}" height="100%">
    <el-table-column align="center" prop="type" label="设备类型" width="180"></el-table-column>
    <el-table-column align="center" v-for="item in tableHead" :key="item.type" :label="item.name">
      <template slot-scope="scope" style="height: 100%;">

          <el-table :data="scope.row.dong"  :show-header="false" height="100%">
            <el-table-column align="center" prop="subject" label="编号">
              <template slot-scope="scope2">
                {{ scope.row[item.type][scope2.$index]?scope.row[item.type][scope2.$index].subject:'' }}
              </template>
            </el-table-column>
            <el-table-column align="center" prop="score" label="天数">
              <template slot-scope="scope2">
                {{ scope.row[item.type][scope2.$index]?scope.row[item.type][scope2.$index].score:'' }}
              </template>
            </el-table-column>
          </el-table>
      </template>
    </el-table-column>
  </el-table>
</template>
 
<script>
export default {
  data() {
    return {
      tableHead:[
        {type:'dong',name:'东部战区'},
        {type:'xi',name:'西部战区'},
        {type:'nan',name:'南部战区'},
        {type:'bei',name:'北部战区'},
        {type:'zhong',name:'中部战区',},
      ],
      tableData: [
        {
          type: '5g设备',
          dong: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 },
            { subject: '英语', score: 85 },
            { subject: '英语', score: 85 },
          ],
          xi: [
            { subject: '语文', score: 80 },
            { subject: '--', score: '--' },
          ],
          nan: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
          bei: [
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
          zhong: [
          
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
        },
        {
          type: '4g设备',
          dong: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
            { subject: '英语', score: 85 }
          ],
          
         
          bei: [
            { subject: '语文', score: 80 },
           
          ],
          zhong: [
            { subject: '语文', score: 80 },
            { subject: '数学', score: 90 },
          ],
        },
        // ... 可以添加更多的数据项
      ]
    };
  }
};
</script>
<style lang="scss" >
.el-table .cell{
  padding: 0;
}
</style>

因为之前没有遇到过这种需求,所以记录一下加深一下印象,有问题欢迎指出
第一次发 (下手轻点)

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rebirth_lemon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值