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

相关推荐
无风听海11 分钟前
Promise 与 Async Await 深度解析
前端·javascript
牛奶14 分钟前
AI 永远说好,于是我们只会说 yes
前端·aigc·ai编程
浩风祭月14 分钟前
把前端项目的 CI 构建时间从 15 分钟压到了 2 分钟
前端·ai编程
牛奶18 分钟前
黑客是怎么看到你数据的?
前端·安全·黑客
ihuyigui20 分钟前
国际企业办公短信接口
前端·后端·架构
lpd_lt33 分钟前
服务端类vue等页面AI测试方向
前端·vue.js·人工智能
AugustRed35 分钟前
A2UI 完整学习指南(含 Java 后端 + 前端实战示例)
java·开发语言·前端
王莎莎-MinerU39 分钟前
Agent 时代,科学数据 API 需要重新设计
大数据·前端·数据库·人工智能·个人开发
jingling55542 分钟前
自建技术博客实战(三):工具专栏——地图定位、声音复刻与 rembg 抠图
android·开发语言·前端·ai·nextjs
小小小小宇1 小时前
Chrome 插件在新开页生效
前端