react antdesign实现表格嵌套表单

一直使用elementui,后期使用antDesign个人感觉封装的很死没有elementui灵活,也可能是自己不熟吧。做个表格嵌套表单弄半天了。记录下这一过程。官网地址反正我是看半天了。官网地址

主要实现是在每列添加对应的表单控件使用受控组件来实现 <Input value={text} onChange={(e) => handleFieldChange(e, 'paramName', index) } 改变当前表格数据来实现数据的改变

js 复制代码
const [dataSource, setDataSource] = useState<any[]>([])
// 获取表格行数据
 const getRowByKey = (key, newData) => {
   return (newData || dataSource)[key];
 }
 // 改变表格数据
 const handleFieldChange = (e, fileName, key) => {
   const newData = [...dataSource];
   const target = getRowByKey(key, newData);
   if (target) {
     target[fileName] = e.target.value || e.target.checked;
     setDataSource(newData);
   }
 }

 // 新增
 const addData = () => {
   setDataSource([...dataSource, { 
     paramName: "",
     paramDesc: "",
     paramType: "",
     sampleItem: "",
     mandatory: true,
     requiredFlag: 1);
 };
// 删除
 const handleRemove = (index) => {
   const newData = [...dataSource];
   newData.splice(index, 1);
   setDataSource(newData);
 }
const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'key',
      align: 'center',
      width: 70,
      render: (_, __, index) => index + 1
    },
    {
      title: '参数名',
      align: 'center',
      dataIndex: 'paramName',
      minWidth: 100,
      render: (text, record, index) => {
        return <Input value={text} onChange={(e) => handleFieldChange(e, 'paramName', index) } />
      }
    },
    {
      title: '描述',
      align: 'center',
      dataIndex: 'paramDesc',
      minWidth: 100,
      render: (text, record, index) => {
        return <Input value={text} onChange={(e) => handleFieldChange(e, 'paramDesc', index) } />
      }
    },
    {
      title: '数据类型',
      align: 'center',
      dataIndex: 'paramType',
      minWidth: 100,
      render: (text, record, index) => {
        return <Input value={text} onChange={(e) => handleFieldChange(e, 'paramType', index) } />
      }
    },
    {
      title: '示例值',
      align: 'center',
      dataIndex: 'sampleItem',
      minWidth: 100,
      render: (text, record, index) => {
        return <Input value={text} onChange={(e) => handleFieldChange(e, 'sampleItem', index) } />
      }
    },
    {
      title: '校验',
      align: 'center',
      dataIndex: 'mandatory',
      minWidth: 100,
      render: (text, record, index) => {
        return <Space>
            <Checkbox checked={text} onChange={(e) => handleFieldChange(e, 'mandatory', index)} />
            <Button type="link" onClick={() => handleSetRule()}>高级规则</Button>  
        </Space>
      }
    },
    {
      title: '操作',
      align: 'center',
      minWidth: 100,
      fixed: 'right',
      render: (_, record, index) => (
        <Space>
          <Button type="link" onClick={() => addData()} >追加</Button>
          <Button type="link" danger onClick={() => handleRemove(index)}>删除</Button>
        </Space>  
      )
    }
  ]
  
<Table
    style={{ width: '100%' }}
    rowKey="key"
    columns={columns}
    dataSource={dataSource} // 从表单中获取行数据
    pagination={false} // 关闭分页(如需分页需额外处理)
  />
相关推荐
2501_9418779815 小时前
从配置热更新到运行时自适应的互联网工程语法演进与多语言实践随笔分享
开发语言·前端·python
云上凯歌15 小时前
01 ruoyi-vue-pro框架架构剖析
前端·vue.js·架构
华仔啊16 小时前
JavaScript 如何准确判断数据类型?5 种方法深度对比
前端·javascript
毕设十刻17 小时前
基于Vue的迅读网上书城22f4d(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
程序员小寒17 小时前
从一道前端面试题,谈 JS 对象存储特点和运算符执行顺序
开发语言·前端·javascript·面试
爱健身的小刘同学17 小时前
Vue 3 + Leaflet 地图可视化
前端·javascript·vue.js
神秘的猪头17 小时前
Ajax 数据请求:从零开始掌握异步通信
前端·javascript
稀饭5218 小时前
用changeset来管理你的npm包版本
前端·npm
TeamDev18 小时前
基于 Angular UI 的 C# 桌面应用
前端·后端·angular.js
Komorebi゛18 小时前
【CSS】斜角流光样式
前端·css