react Ant Design 动态表头添加操作列

模拟后端返回的表头、列表数据

注意:我们要在表头数据中添加一个 render 函数,里面就是你操作列的内容,value是你数据列表每行的对象 ,item 是你表头的对象

页面中去处理这个两个数组

   dataList.forEach((item, index) => {
      item.operationList = arr;// arr = ['编辑','删除']
    });
arr 数组中的数据好像只支持这样格式,也可能是我没研究透

columns.forEach((item, index) => {
      if (item.key === "operation") {
        item.render = (_, value) => (
          <div style={{ display: "flex", justifyContent:'space-around' }}>
            {value.operationList.map((i) => {
              return (
                <Button
                  type="primary"
                  key={i}
                  // 操作列的按钮事件
                  onClick={() => {
                    // i  是操作按钮
                    // value 是每行的数据
                    // item 是表头对象
                    operationEvent(i, value, item);
                  }}>
                  {i}
                </Button>
              );
            })}
          </div>
        );
      }
});


const operationEvent = (i, value, item) => {
    switch (i) {
      case "编辑":
        compileData(i, value, item);
        break;
      case "删除":
        delData(i, value, item);
        break;
    }
  };

html

import React, { useEffect } from "react";
import { Table, Button, message } from "antd";


 
 <Table columns={columns} dataSource={dataList} />

我们的需求是动态表单,表头也是后端返回的数据进行渲染

相关推荐
m0_528723811 小时前
HTML中,title和h1标签的区别是什么?
前端·html
Dark_programmer1 小时前
html - - - - - modal弹窗出现时,页面怎么能限制滚动
前端·html
GDAL1 小时前
HTML Canvas clip 深入全面讲解
前端·javascript·canvas
禾苗种树1 小时前
在 Vue 3 中使用 ECharts 制作多 Y 轴折线图时,若希望 **Y 轴颜色自动匹配折线颜色**且无需手动干预,可以通过以下步骤实现:
前端·vue.js·echarts
贵州数擎科技有限公司1 小时前
使用 Three.js 实现流光特效
前端·webgl
JustHappy1 小时前
「我们一起做组件库🌻」做个面包屑🥖,Vue的依赖注入实战💉(VersakitUI开发实录)
前端·javascript·github
祝鹏2 小时前
前端如何制定监控项
前端
拉不动的猪2 小时前
刷刷题16
前端·javascript·面试
支撑前端荣耀2 小时前
基于 Vue 的响应式瀑布流实现全景解析
前端·javascript·面试
祈澈菇凉3 小时前
如何结合使用thread-loader和cache-loader以获得最佳效果?
前端