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} // 关闭分页(如需分页需额外处理)
  />
相关推荐
小p5 小时前
react学习4:CSS Modules 样式
前端·react.js
东华帝君5 小时前
小型列表是否需要拆分成服务器组件
前端
卍郝凝卍6 小时前
物联网卡摄像头从前端至后台的实现过程
前端·物联网·视频解决方案
QuantumLeap丶6 小时前
《Flutter全栈开发实战指南:从零到高级》- 10 -状态管理setState与InheritedWidget
flutter·前端框架·dart
疯狂暴龙GG帝6 小时前
项目中使用el-table实现行合并及合并后序号不连续解决方案
前端·vue.js
东华帝君6 小时前
React Hook Form —— useForm 和 FormProvider+useFormContext
前端
小p6 小时前
react学习3: 闭包陷阱
前端·react.js
该用户已不存在6 小时前
Vibe Coding 入门指南:从想法到产品的完整路径
前端·人工智能·后端
Pedro6 小时前
Flutter - 日志不再裸奔:pd_log 让打印有型、写盘有序
前端·flutter