elementui table表格动态生成多级表头

本文展示了如何使用Vue.js封装一个可扩展的多级表格组件。组件包含`test1.vue`用于创建表格列,`test2.vue`作为主要的表格组件,而`test3.vue`演示了如何在实际应用中使用这个组件。组件支持悬浮显示详情,通过`v-for`遍历多级表头数据,并动态渲染表格内容。表格数据和表头配置可以通过props传递给组件。

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

1.实现效果

2.封装table表格组件

test1.vue

<template>
  <el-table-column :label="coloumnHeader.label" :prop="coloumnHeader.label">
    <template v-for="item in coloumnHeader.children">
      <tableColumn
        v-if="item.children && item.children.length"
        :key="item.id"
        :coloumn-header="item"
      ></tableColumn>
      <el-table-column
        v-else
        :key="item.name"
        :label="item.label"
        :prop="item.prop"
      >
         //悬浮展示详情
       <template slot-scope="scope">
             <el-popover
                placement="top-start"
                width="200"
                trigger="click"
                content="这是一段内容,这是一段内容,这是一段内容,这是一段内容。"
              >
                  <el-button type="text" slot="reference">                
                      {{scope.row[scope.column.property]}}
                  </el-button>
              </el-popover>
          </template>
      </el-table-column>
    </template>
  </el-table-column>
</template>

<script>
export default {
  name: "tableColumn",
  props: {
    coloumnHeader: {
      type: Object,
      required: true,
    },
  },
};
</script>

<style scoped>
</style>

  test2.vue

<template>
  <el-table :data="tableData">
    <template v-for="item in tableHeader">
      <table-column
        v-if="item.children && item.children.length"
        :key="item.id"
        :coloumn-header="item"
      ></table-column>
      <el-table-column
        v-else
        :key="item.id"
        :label="item.label"
        :prop="item.prop"
      >
     </el-table-column>
    </template>
  </el-table>
</template>

<script>
import TableColumn from "./test1.vue";
export default {
  props: {
    // 表格的数据
    tableData: {
      type: Array,
      required: true,
    },
    // 多级表头的数据
    tableHeader: {
      type: Array,
      required: true,
    },
  },
  components: {
    TableColumn,
  },
};
</script>

<style scoped>
.el-button{
  border: none;
  padding: 0;
  color: #606266;
}
</style>

test3.vue 调用组件使用

<template>
  <div>
    <dynamic-table
      :table-data="tableData"
      :table-header="tableConfig"
    ></dynamic-table>
  </div>
</template>

<script>
import DynamicTable from "./test2.vue";
export default {
  components: {
    DynamicTable,
  },
  data() {
    return {
      // 表数据
      tableData: [
        {
          date: "2021-09-06",
          name: "张三",
          phone: "15739951445",
          province: "河北省",
          city: "张家口",
          address: "河北省张家口市桥西区",
        },
      ],
      // 表头数据
      tableConfig: [
        {
          id: 100,
          label: "日期",
          prop: "date",
        },
        {
          id: 200,
          label: "配送信息",
          prop: "",
          children: [
            {
              id: 210,
              label: "姓名",
              prop: "",
              children:[
                  {
                      id:300,
                      label:"姓名1",
                      prop:"name"
                  },
                  {
                      id:301,
                      label:"姓名2",
                      prop:"name"
                  },
                  {
                      id:302,
                      label:"姓名3",
                      prop:"name"
                  },
              ]
            },
            {
              id: 220,
              label: "电话",
              prop: "",
              children:[
                  {
                      id:303,
                      label:"电话1",
                      prop:"phone"
                  },
                  {
                      id:304,
                      label:"电话2",
                      prop:"phone"
                  },
                  {
                      id:305,
                      label:"电话3",
                      prop:"phone"
                  },
              ]
            },
            {
              id: 200,
              label: "地址",
              prop: "",
              children: [
                {
                  id: 210,
                  label: "省份",
                  prop: "province",
                },
                {
                  id: 220,
                  label: "城市",
                  prop: "city",
                },
                {
                  id: 220,
                  label: "详细地址",
                  prop: "address",
                },
              ],
            },
          ],
        },
        {
            id:400,
            label:"测试表头",
            prop:"",
            children:[
                {
                    id:401,
                    label:"测试二级表头1",
                    prop:"",
                    children:[
                        {
                            id:402,
                            label:"测试三级表头1",
                            prop:"name"
                        },
                        {
                            id:403,
                            label:"测试三级表头2",
                            prop:"phone"
                        }
                    ]
                },
                {
                    id:404,
                    label:"测试二级表头2",
                    prop:"",
                    children:[
                        {
                            id:405,
                            label:"测试三级表头",
                            prop:"phone"
                        },
                        {
                            id:406,
                            label:"测试三级表头2",
                            prop:"name"
                        }
                    ]
                }
            ]
        }
      ],
    };
  },
};
</script>

<style scoped>
</style>

在 `el-table` 组件中,可以使用 `el-table-column` 组件生成表格列,而 `el-table-column` 组件的一个重要属性是 `children`,可以用来设置多级表头。因此,可以通过动态生成 `el-table-column` 组件来动态生成多级表头。 以下是一个示例代码: ```html <el-table :data="tableData" style="width: 100%"> <el-table-column v-for="(column, index) in columns" :key="index" :label="column.label" :prop="column.prop" :width="column.width" :align="column.align"> <template v-if="column.children" slot-scope="scope"> <el-table-column v-for="(subColumn, subIndex) in column.children" :key="subIndex" :label="subColumn.label" :prop="subColumn.prop" :width="subColumn.width" :align="subColumn.align"></el-table-column> </template> </el-table-column> </el-table> ``` 在上述代码中,`columns` 是一个数组,用于存储表头列的属性。如果一个表头列具有子列,则在该列对象中添加 `children` 属性,该属性的值也是一个数组,存储子列的属性。 例如,下面是一个包含两级表头的 `columns` 数组: ```js data() { return { columns: [ { label: '姓名', prop: 'name', width: 120, align: 'center' }, { label: '详细信息', children: [ { label: '年龄', prop: 'age', width: 80, align: 'center' }, { label: '性别', prop: 'gender', width: 80, align: 'center' } ] }, { label: '地址', prop: 'address', width: 300, align: 'left' } ], tableData: [ { name: '张三', age: 18, gender: '男', address: '北京市朝阳区' }, { name: '李四', age: 20, gender: '女', address: '上海市浦东新区' }, { name: '王五', age: 22, gender: '男', address: '广州市天河区' } ] } } ``` 在 `el-table-column` 组件中,如果一个列具有子列,则使用 `v-if` 动态生成子列。在子列中,使用 `v-for` 循环遍历子列属性,动态生成子列。 这样就可以通过动态生成 `el-table-column` 组件来动态生成多级表头
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值