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_920931702 小时前
React Native鸿蒙跨平台采用ScrollView的horizontal属性实现横向滚动实现特色游戏轮播和分类导航
javascript·react native·react.js·游戏·ecmascript·harmonyos
0思必得04 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
摘星编程4 小时前
React Native鸿蒙版:Drawer抽屉导航实现
react native·react.js·harmonyos
东东5164 小时前
智能社区管理系统的设计与实现ssm+vue
前端·javascript·vue.js·毕业设计·毕设
catino4 小时前
图片、文件的预览
前端·javascript
2501_920931706 小时前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
layman05286 小时前
webpack5 css-loader:从基础到原理
前端·css·webpack
半桔6 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
AI老李6 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
_OP_CHEN6 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化