Form.List 实现行内表格编辑

Form.List +table 实现行内表格直接编辑以及删除功能

import React from 'react';
import { PlusOutlined, } from '@ant-design/icons';
import { Button, Form, Input, Space, Table } from 'antd';

const mockData = [
    {
        name: '糖果',
        code: 'code1',
    },
    {
        name: '饮料',
        code: 'code2'
    }
]
interface Iprops {
    onCancel: () => void
    onSumbit: () => void
    type: 'add' | 'edit' | 'show'
    data: []
}

const App = ({ onCancel, onSumbit, type, data }: Iprops) => {
    const [form] = Form.useForm();

    const disbled = type === 'show'

    const onFinish = (values: any) => {
        console.log('表单数据', values);
    };



    const columns = [
        {
            title: "名称",
            dataIndex: 'name',
            render(_, field) {
                const name = form.getFieldValue(['users', field.name, 'name']);
                return (
                    <Form.Item required name={[field?.name, 'name']} noStyle>
                        {
                            type !== 'show' ? <Input allowClear placeholder='请输入' disabled={disbled} /> : name
                        }
                    </Form.Item>
                )
            }
        },
        {
            title: "code",
            dataIndex: 'code',
            render(_, field) {
                const name = form.getFieldValue(['users', field.name, 'code']);
                return (
                    <Form.Item required name={[field?.name, 'code']} noStyle>
                        {
                            type !== 'show' ? <Input allowClear placeholder='请输入' disabled={disbled} /> : name
                        }
                    </Form.Item>
                )
            }
        },
    ]

    if (type !== 'show') {
        columns.push({
            title: "操作",
            dataIndex: '',
            render: (_, field) => {
                return (
                    <Space>
                        <a onClick={() => field.remove(field?.name)}>删除</a>
                    </Space>
                );
            },
        })
    }

    return (
        <Form
            form={form}
            initialValues={{ users: mockData }}
            onFinish={onFinish}
            style={{ maxWidth: 800 }}
        >
            <Form.List name="users">
                {(fields, { add, remove }, { errors }) => (
                    <>
                        <Table
                            dataSource={fields?.map((i) => ({ ...i, remove }))}
                            columns={columns}
                            rowKey='key'
                        />

                        <Form.Item>
                            <Button
                                type="dashed"
                                style={{ width: '100%', marginTop: '8px' }}
                                icon={<PlusOutlined />}
                                onClick={() => { add() }}
                            >
                                添加一行
                            </Button>
                        </Form.Item>
                    </>
                )}
            </Form.List>
            <Form.Item>
                <Button type="primary" htmlType="submit">
                    提交
                </Button>
            </Form.Item>
        </Form>
    );
};

export default App;

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值