Vue+Element-ui 树型+懒加载<根据相同字段合并表格>

本文介绍如何使用Vue实现表格的合并与懒加载功能,包括处理不同版本的Vue组件、利用expand-change事件进行折叠展开控制及通过span-method实现单元格合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人处理合并踩坑小记

踩坑注意事项:expand-change  树形展开折叠 至少要在 2.9.2 以上版本才有返回

2.8.2

​​​​​​​

 

2.9.2+

 

首先看官网的table 树形懒加载案例

需求:1、根据num 返回值  为表格序列号 ,父子级中的num值相同 ,序列号相同  合并单元格

思路: 1、利用官网树形数据与懒加载例子

            2、无序序号,将type=""设置为空  防止箭号防止第一格

           3、 expand-change折叠展开触发事件、:span-method 处理合并单元格

            4、考虑是可多出展开折叠转态,遍历数组 父级数据都添加expanded:false默认折叠,根据点击当前行expand-change  返回状态  修改对应的expanded值 达到管理展开折叠的目的

            5、如果是load懒加载  可根据lload返回row的值 遍历对应当前二级数据 对应的父级 添加cound记录子级长度;如果数组中直接children时,直接遍历获取children的长度即可。

            6、综上,根据expanded和cound计算第一列相同序列号合并的目的

代码如下:

<template>
    <div>
        <!--<el-table-->
                <!--ref="refTable"-->
                <!--:data="tableData1"-->
                <!--style="width: 100%"-->
                <!--row-key="id"-->
                <!--border-->
                <!--lazy-->
                <!--:load="load"-->
                <!--:tree-props="{children: 'children', hasChildren: 'hasChildren'}"-->
                <!--@expand-change="expandChange"-->
                <!--:span-method="objectSpanMethod">-->
            <!--<el-table-column type="" width="50" label="序号" fixed align="center">-->
                <!--<template slot-scope="scope">-->
                    <!--<span>{{scope.row.num}}</span>-->
                <!--</template>-->
            <!--</el-table-column>-->
            <!--<el-table-column-->
                    <!--prop="date"-->
                    <!--label="日期"-->
                    <!--align="center" fixed-->
                    <!--width="180">-->
            <!--</el-table-column>-->
            <!--<el-table-column-->
                    <!--prop="name"-->
                    <!--label="姓名" fixed-->
                    <!--width="120">-->
            <!--</el-table-column>-->
            <!--<el-table-column label="配送信息">-->
                <!--<el-table-column-->
                        <!--prop="name"-->
                        <!--label="姓名">-->
                <!--</el-table-column>-->
                <!--<el-table-column label="地址">-->
                    <!--<el-table-column-->
                            <!--prop="province"-->
                            <!--label="省份"-->
                            <!--width="120">-->
                    <!--</el-table-column>-->
                    <!--<el-table-column-->
                            <!--prop="city"-->
                            <!--label="市区"-->
                            <!--width="120">-->
                    <!--</el-table-column>-->
                    <!--<el-table-column-->
                            <!--prop="address"-->
                            <!--label="地址"-->
                            <!--width="300">-->
                    <!--</el-table-column>-->
                    <!--<el-table-column-->
                            <!--prop="zip"-->
                            <!--label="邮编"-->
                            <!--width="520">-->
                    <!--</el-table-column>-->
                <!--</el-table-column>-->
            <!--</el-table-column>-->
        <!--</el-table>-->
        <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                @expand-change="expandChange"
                :span-method="objectSpanMethod">
            <el-table-column type="" width="50" label="序号" fixed align="center">
                <template slot-scope="scope">
                    <span>{{scope.row.num}}</span>
                </template>
            </el-table-column>
            <el-table-column
                    prop="date"
                    label="日期"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="姓名"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址">
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
  export default {
    name: 'ReportTable',
    data() {
      return {
        expandNum: 0, // 折叠 展开序列号
        expandTrue: false, // 展开 true  折叠 false
        tableData1: [
          {
            id: 100,
            num: '',
            date: '净利润',
            name: '王小虎0',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          },
          {
            id: 101,
            num: '',
            date: '营业收入',
            name: '王小虎1',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }, {
            id: 102,
            num: '',
            date: '一级费用总合计',
            name: '王小虎2',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }, {
            id: 1,
            num: 1,
            date: '2016-05-03',
            name: '王小虎3',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }, {
            id: 2,
            num: 2,
            date: '2016-05-03',
            name: '王小虎4',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }, {
            id: 3,
            num: 3,
            date: '2016-05-01',
            name: '王小虎5',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1519 弄',
            hasChildren: true
          }, {
            id: 4,
            num: 4,
            date: '2016-05-03',
            name: '王小虎6',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }],
        tableData: [
          {
            id: 100,
            num: '',
            date: '净利润',
            name: '王小虎0',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333,

          },
          {
            id: 101,
            num: '',
            date: '营业收入',
            name: '王小虎1',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          },
          {
            id: 102,
            num: '',
            date: '一级费用总合计',
            name: '王小虎2',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          },
          {
            id: 1,
            num: 1,
            expandTrue: false,
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
          },
          {
            id: 2,
            num: 2,
            expandTrue: false,
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1517 弄',
            children: [
              {
                id: 21,
                num: 2,
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄'
              }
            ]
          },
          {
            id: 3,
            num: 3,
            expandTrue: false,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄',
            children: [
              {
                id: 31,
                num: 3,
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄'
              },
              {
                id: 32,
                num: 3,
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄'
              }
            ]
          },
          {
            id: 4,
            num: 4,
            expandTrue: false,
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1516 弄'
          }
        ],
        newTableData: [],
      }
    },
    mounted(){
      this.newTableData = JSON.parse(JSON.stringify(this.tableData));
      this.changeData();
    },
    methods: {
      expandChange(row, expanded) {
        this.expandNum = row.num;
        this.expandTrue = expanded;
        let i = this.findExpandChange(row);
        this.tableData[i].expandTrue = this.expandTrue; // expandTrue 折叠/展开的状态
      },
      // 返回当前折叠/展开  在数组下标
      findExpandChange(row) {
        let id1 = this.tableData.findIndex((arr) => arr.num == row.num); //id1为对象的数组下标
        return id1
      },
      load(tree, treeNode, resolve) {
        let arr = [
          {
            id: 31,
            num: 3,
            date: '2016-05-01',
            name: '王小虎31',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }, {
            id: 32,
            num: 3,
            date: '2016-05-01',
            name: '王小虎32',
            province: '上海',
            city: '普陀区',
            address: '上海市普陀区金沙江路 1518 弄',
            zip: 200333
          }
        ]
        resolve(arr);

      },
      // changeData  用于记录子级长度 来计算合并单元格个数
      changeData () {
        this.newTableData.forEach((item, itemIndex) => {
          let count = 0;
          if (item.children && item.children.length) {
            count += item.children.length;
          }
          this.tableData[itemIndex].count = count;
        })
      },
      objectSpanMethod({row, column, rowIndex, columnIndex}) {
        if (row.num !== '') {
          if (columnIndex === 0) {
                if(row.count==0 || row.count > 0) {
                  let rowspan = 0
                  if (row.expandTrue) {
                    rowspan = row.count + 1;
                  } else {
                    rowspan = 1;
                  }
                  return [rowspan, 1]
                } else {
                  return [0, 0];
                }
          }
        } else {
          if (columnIndex === 0) {
            return [0, 0];
          } else if (columnIndex === 1) {
            return [1, 2];
          } else if (columnIndex > 1) {
            return [1, 1];
          } else {
            return [0, 0];
          }
        }
      }
    }
  }
</script>

<style scoped>

</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值