Antd uses complex dynamic forms. Times error.

The structure of the

data is like this, and you need to use this to generate a form like this

{
    name: "", // 
    groups: [ // 
 
        {
            groupName: "", // 
            state: 1, //   1   0 
            isOpen: 1, //   1   0 
            fields: [ // 
 
                {
                    fieldNameCn: "", // 
                    tableName: "", // 
                    fieldName: "", // 
                    state: 1 //   1   0 
                            }
            ]
                    }
    ]
}

clipboard.png
Click the add Group button to add a piece of data to groups , click the new field to add a piece of data to groups [x] .fields , and vice versa

now loop out the initial form, no problem, use a method similar to the following to register a field for antd

{getFieldDecorator(`groups[${index}].groupName`, {})()}

now, when you click add, use the following method to set field to report You cannot set field before registering it. , because I did not register groups [1] .groupName , because it is new, it is impossible for me to register it when render traverses.

appGroup = () = > {
    const { form } = this.props;
    const groups = form.getFieldValue("groups");
    const group_empty = this.data_form_empty.groups[0];    // 
    const key = `groups[${groups.length}]`
    form.setFieldsValue({    //  You cannot set field before registering it.
        [key]: group_empty
    });
}

has also tried to register a groups directly with getFieldDecorator , and then directly setFieldsValue the entire groups. However, due to the complexity of the structure, people will register things like groups [${index}] .groupName when getFieldDecorator , so they will report Warning: One field name cannot be part of another, rooma and a.b.

.
Feb.27,2021

say something you don't like to hear. We've done forms that are 10 times more complex than yours.

most of the problems that you describe phenomena are problems of solving ideas. When you click add, don't try to insert data into the page, but insert data into the data.
when state data changes, render will be re-rendered.
when you think of all operations as operations on data, you will find that a lot of complex things become very simple.

the most common problem with form insertion is not what you mentioned, but the problem with the display of the entered data when deleting and inserting the form. The
solution is to make sure that every time you add, an id that will never be repeated is used as key .

Menu