Vue动态多级表头+行列合计+可编辑表格

新建组件:Table.vue

javascript 复制代码
<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"
      />
      <el-table-column
        v-else
        :key="item.name"
        :label="item.label"
        :prop="item.prop"
      />
    </template>
  </el-table-column>
</template>
<script>
export default {
  name: 'TableColumn',
  props: {
    coloumnHeader: {
      type: Object,
      required: true
    }
  }
}
</script>

新建组件:

javascript 复制代码
<template>
  <div class="app-container">
    <el-table :data="tableData" style="width: 100%" :span-method="objectSpanMethod" border show-summary>
      <template v-for="item in tableConfig">
        <table-column
          v-if="item.children && item.children.length"
          :key="item.id"
          :coloumn-header="item"
        />
        //编辑操作按钮
        <el-table-column
          v-else-if="item.label == '操作'"
          :key="item.id + 'r'"
          align="center"
          :label="item.label"
          width="200"
        >
          <template #default="{ row }">
            <el-button
              v-if="row.edit"
              size="small"
              type="success"
              @click="confirmEdit(row)"
            >
              保存
            </el-button>
            <el-button
              v-else
              icon="el-icon-edit"
              size="small"
              type="primary"
              @click="row.edit = !row.edit"
            >
              编辑
            </el-button>
          </template>
        </el-table-column>
        
        //一级表头
        <el-table-column
          v-else
          :key="item.id + 1"
          :label="item.label"
          :prop="item.prop"
        >
        //编辑表格
        <template slot-scope="scope">
            <input
              type="text"
              v-model="scope.row[item.prop]"
              v-show="scope.row.edit"
            />
            <span v-show="!scope.row.edit">{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
      </template>
    </el-table>
  </div>
</template>
<script>
// 引入api
import TableColumn from './Table.vue'
export default {
  // 定义页面数据
  components: { Treeselect, DynamicTable, TableColumn },
  data() {
    return {
      // 表数据
      tableData: [
      	{
          parkName: '花木',
          date: '2023',
          count0: '1',
          money0: '2',
          count1: '3',
          money1: '4',
          edit: false,
        }
      ],
      // 表头数据
      tableConfig: [
            {
                "id": 2, 
                "label": "停车场名称", 
                "prop": "parkName", 
                "children": null
            }, 
            {
                "id": 85, 
                "label": "日期", 
                "prop": "date", 
                "children": null
            }, 
            {
                "id": 0, 
                "label": "微信支付", 
                "prop": "0", 
                "children": [
                    {
                        "id": 12, 
                        "label": "交易笔数", 
                        "prop": "count0", 
                        "children": null
                    }, 
                    {
                        "id": 89, 
                        "label": "交易金额", 
                        "prop": "money0", 
                        "children": null
                    }
                ]
            }, 
            {
                "id": 1, 
                "label": "支付宝支付", 
                "prop": "1", 
                "children": [
                    {
                        "id": 40, 
                        "label": "交易笔数", 
                        "prop": "count1", 
                        "children": null
                    }, 
                    {
                        "id": 61, 
                        "label": "交易金额", 
                        "prop": "money1", 
                        "children": null
                    }
                ]
            }
        ],
    }
  }
}
</script>

可参考链接

https://blog.csdn.net/weixin_45275107/article/details/127509100

https://blog.csdn.net/weixin_39166851/article/details/130765957

https://blog.csdn.net/m0_67841039/article/details/131308126

https://blog.csdn.net/weixin_40881970/article/details/124699566

相关推荐
晚霞的不甘9 分钟前
Flutter for OpenHarmony 引力弹球游戏开发全解析:从零构建一个交互式物理小游戏
前端·flutter·云原生·前端框架·游戏引擎·harmonyos·骨骼绑定
春日见11 分钟前
Docker中如何删除镜像
运维·前端·人工智能·驱动开发·算法·docker·容器
码农六六24 分钟前
前端知识点梳理,前端面试复习
前端
打小就很皮...26 分钟前
React 合同审查组件:按合同标题定位
前端·react.js·markdown
CHU72903542 分钟前
智慧陪伴新选择:陪诊陪护预约小程序的暖心功能解析
java·前端·小程序·php
奔跑的web.1 小时前
TypeScript namespace 详解:语法用法与使用建议
开发语言·前端·javascript·vue.js·typescript
倾国倾城的反派修仙者1 小时前
鸿蒙开发——使用弹窗授权保存媒体库资源
开发语言·前端·华为·harmonyos
泰勒疯狂展开1 小时前
Vue3研学-组件的生命周期
开发语言·前端·vue
Charlie_lll1 小时前
学习Three.js–基于GeoJSON绘制2D矢量地图
前端·three.js
计算机学姐1 小时前
基于SpringBoot的自习室座位预定系统【预约选座+日期时间段+协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·spring·信息可视化·tomcat