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

相关推荐
kyriewen11 小时前
我手写了一个 EventEmitter,面试官追问了 6 个问题——第 4 个我没答上来
前端·javascript·面试
IT_陈寒11 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
小林攻城狮12 小时前
使用 Transport 节流解决 Vercel AI SDK 流式渲染卡死问题
前端·react.js
前端缘梦12 小时前
告别 TS 运行时类型漏洞!Zod 完整入门实战教程(前端 / 全栈必备)
前端·react.js·全栈
the_answer12 小时前
Webpack vs Vite 深度对比分析
前端·webpack
转转技术团队12 小时前
验证码识别实战:前端不写页面,改训模型了?
前端
MomentYY12 小时前
Temperature:AI 的“脑洞旋钮”
前端·llm·ai编程
远航_13 小时前
OpenSpec 完整详细介绍
前端·后端
召钱熏13 小时前
状态枚举正确≠渲染正确:一个语音按钮的状态机边界修复实录
android·前端
SkyWalking中文站13 小时前
认识 Horizon UI · 1/17:SkyWalking 新一代可观测性控制台
运维·前端·监控