el-table多级表格

本文介绍了如何使用ElementUI中的el-table组件在Vue应用中实现多级表格结构,展示了如何配置和渲染嵌套数据,以及如何使用span-method方法处理行合并。
摘要由CSDN通过智能技术生成

1.el-table实现多级表格展示数据

效果:

 实现代码:

<template>
  <div>
    <div style="padding: 5px 10px">
      <el-table :data="tableData" height="calc(50vh - 80px)" :span-method="spanMethod"
                row-key="id" :tree-props="{children:'children',hasChildren:'hasChildren'}">
        <el-table-column label="列0" prop="column0">
          <template slot-scope="scope">
            <span
                v-if="scope.row.isParent">数据1:{{ scope.row.columnF0}}  数据2:{{ scope.row.columnF1 }}  数据3:{{ scope.row.columnF2 }}  </span>
            <span v-if="!scope.row.isParent">{{ scope.row.column1 }}</span>
          </template>
        </el-table-column>
        <el-table-column v-for="item in columnArr" :key="item.prop" :label="item.label" :prop="item.prop"
                         show-overflow-tooltip>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表格数据
      tableData: [
        {
          id: 21,
          columnF0: 'f001',
          columnF1: 'f002',
          columnF2: 'f003',
          isParent: true,
          children: [
            {
              id: 1,
              column0: '000',
              column1: '111',
              column2: '222',
              column3: '333',
              column4: '444',
              column5: '555',
              column6: '666',
              column7: '777',
              column8: '888',
              column9: '999',
              isParent: false,
            },
            {
              id: 2,
              column0: '000',
              column1: '111',
              column2: '222',
              column3: '333',
              column4: '444',
              column5: '555',
              column6: '666',
              column7: '777',
              column8: '888',
              column9: '999',
              isParent: false,
            },
          ]
        },
        {
          id: 22,
          columnF0: 'f001',
          columnF1: 'f002',
          columnF2: 'f003',
          isParent: true,
          children: [
            {
              id: 3,
              column0: '000',
              column1: '111',
              column2: '222',
              column3: '333',
              column4: '444',
              column5: '555',
              column6: '666',
              column7: '777',
              column8: '888',
              column9: '999',
              isParent: false,
            },
          ]
        },
        {
          id: 23,
          columnF0: 'f001',
          columnF1: 'f002',
          columnF2: 'f003',
          isParent: true,
          children: [
            {
              id:4,
              column0: '000',
              column1: '111',
              column2: '222',
              column3: '333',
              column4: '444',
              column5: '555',
              column6: '666',
              column7: '777',
              column8: '888',
              column9: '999',
              isParent: false,
            },
          ]
        }
      ],
      columnArr: [
        {label: '列1', prop: 'column1'},
        {label: '列2', prop: 'column2'},
        {label: '列3', prop: 'column3'},
        {label: '列4', prop: 'column4'},
        {label: '列5', prop: 'column5'},
        {label: '列6', prop: 'column6'},
        {label: '列7', prop: 'column7'},
        {label: '列8', prop: 'column8'},
        {label: '列9', prop: 'column9'},
      ],

    };
  },
  methods: {
    spanMethod({row, column, rowIndex, columnIndex}) {
      console.log('{row,column,rowIndex,columnIndex}:', {row, column, rowIndex, columnIndex})
      if (row.isParent) {
        return [1, 11]
      }
    },
  },
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现多级表格展开的方法有很多种,下面我介绍一种基于 Vue 的实现方法: 1. 首先,定义一个表格组件,并且在数据中添加一个 `children` 属性,用于存储子级数据。 2. 针对每一行数据,添加一个展开/收起的按钮,点击按钮时触发一个方法。 3. 在方法中,判断当前行数据是否有子级数据,如果有,则切换展开/收起状态,并且将子级数据合并到当前表格数据中。如果没有,则不做任何操作。 4. 在表格模板中,根据当前行数据的展开/收起状态,来决定是否显示子级表格。 下面是一个简单的示例代码: ```vue <template> <div> <table> <thead> <tr> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> </thead> <tbody> <tr v-for="item in tableData" :key="item.id"> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.gender }}</td> <td> <button @click="toggleChildren(item)"> {{ item.expanded ? 'Collapse' : 'Expand' }} </button> </td> </tr> <tr v-if="hasChildren" v-for="child in currentChildren" :key="child.id"> <td>{{ child.name }}</td> <td>{{ child.age }}</td> <td>{{ child.gender }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { tableData: [ { id: 1, name: 'Alice', age: 20, gender: 'Female', expanded: false, children: [ { id: 2, name: 'Bob', age: 25, gender: 'Male' }, { id: 3, name: 'Charlie', age: 30, gender: 'Male' } ] }, { id: 4, name: 'David', age: 35, gender: 'Male', expanded: false, children: [ { id: 5, name: 'Eva', age: 40, gender: 'Female' }, { id: 6, name: 'Frank', age: 45, gender: 'Male', expanded: false, children: [ { id: 7, name: 'Grace', age: 50, gender: 'Female' } ] } ] } ] }; }, computed: { currentChildren() { return this.tableData.filter((item) => item.expanded).flatMap((item) => item.children); }, hasChildren() { return this.currentChildren.length > 0; } }, methods: { toggleChildren(item) { if (item.children) { item.expanded = !item.expanded; if (item.expanded) { this.tableData.splice( this.tableData.indexOf(item) + 1, 0, ...item.children.map((child) => ({ ...child, level: item.level + 1 })) ); } else { let index = this.tableData.indexOf(item); let count = 0; while (index < this.tableData.length && this.tableData[index].level > item.level) { this.tableData.splice(index, 1); count++; } } } } } }; </script> ``` 在上面的代码中,我们定义了一个表格组件,并且添加了一个 `toggleChildren` 方法,用于切换展开/收起状态。同时,我们使用 `computed` 属性来计算当前展开的子级数据,并在模板中使用 `v-if` 来控制子级表格的显示。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值