react表格验证

import logo from './logo.svg';
import { Button, Form, Table, Space, Input, Popconfirm, Select } from 'antd';
import React, { useState } from "react";
import './App.css';

function App() {
  
  const [ form ] = Form.useForm();
  const [ dataList, setDataList ] = useState([]);

  const delRow = (index) => {
      const dataListValues = form.getFieldsValue(true).list;
      dataListValues.splice(index, 1);
      setDataList([...dataListValues]);
  }

  const addRow = () => {
    const dataListValues = form.getFieldsValue(true).list || [];
    const newRow = {
        certificateLevel: '',
        certificateName: '',
        certificateType: '',
    }
    setDataList([
        ...dataListValues,
        newRow
    ])
    form.setFieldsValue({list: [...dataListValues, newRow]})
}

const save = (tip) => {
  if(form) {
      form.validateFields().then(res => {
        let value = form.getFieldsValue(true).list;
        console.log(value, "value");
      })
  }
}

  const tableColumns = [
    {
        key: 'certificateName',
        title: <span><span style={{ color: 'red' }}>*</span>证书名称</span>,
        dataIndex: 'certificateName',
        width: 150,
        render:  (_, record, index) => (
            <Form.Item name={ ["list", index, "certificateName"] } key={ index } initialValue={ _ } rules={[{ 
                "required": true,
                "message": "请输入资料名称" },
                {
                  pattern: /^[^\s]*$/,
                  message: '禁止输入空格',
                }]}>
                <Input maxLength={ 50 }/>
            </Form.Item>
        )  
    },
    {
        key: 'certificateType',
        title: <span><span style={{ color: 'red' }}>*</span>证书类型</span>,
        dataIndex: 'certificateType',
        width: 150,
        render:  (_, record, index)  => (
            <Form.Item name={ ["list", index, "certificateType"] } key={ index } initialValue={ _ } rules={[{ 
                "required": true,
                "message": "请选择证书类型" }]}>
                <Select getPopupContainer={triggerNode => triggerNode.parentNode}>
                  <Select.Option value={"1"}>1</Select.Option>
                  <Select.Option value={"2"}>2</Select.Option>
                  <Select.Option value={"3"}>3</Select.Option>
                </Select>
            </Form.Item>
        )  
    },
    {
        key: 'certificateLevel',
        title: '证书等级',
        dataIndex: 'certificateLevel',
        width: 150,
        render:  (_, record, index)  => (
            <Form.Item name={ ["list", index, "certificateLevel"] } key={ index } initialValue={ _ }>
                <Input maxLength={ 50 }/>
            </Form.Item>
        )  
    },
    {
        key: 'operation',
        title: '操作',
        dataIndex: 'operation',
        width: 150,
        fixed: 'right',
        render: (_, record, index)  => (
            <Space direction="horizontal" size="small">
                <Popconfirm
                    title="确认删除?"
                    onConfirm={ () => delRow(index) }
                    okText="确认"
                    cancelText="取消"
                >
                    <Button type="link">删除</Button>
                </Popconfirm>
            </Space>
        )
    }
]
  return (
    <div className="App">
      <Button type="primary" onClick={ addRow }>新增一行</Button>
      <Form form={ form }>
          <Table rowKey="id" scroll={{ x: 1200 }} dataSource={[...dataList]} pagination={false} columns={tableColumns} />
      </Form>
      <Button onClick={save}>提交</Button>
    </div>
  );
}

export default App;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值