Java implements the custom field function of the form (dynamic management of different types of field data)

demo address

User list (custom field test)

Description of Requirement

In business projects, the content of some table data is not fixed, and we can reserve fields. If you want to expand later, you may need to add fields to save data, which will be troublesome and difficult to maintain.

现在我们可以使用自定义字段,可以由我们在线上管理临时添加的字段并保存在数据内
finished function

On the page, you can directly add fields and configure data types. After adding, you can set the data of the custom field in the data list.

advantage

  1. No need to deal with background logic anymore
  2. Complete online
  3. Flexible and scalable
  4. Multiple tables can be expanded at the same time
  5. Fields that support multiple data types

table design

1: Custom Field Template Table

insert image description here
type: Configure to expand different data tables (for example, user table and role table must be extended, you can use type to distinguish)
template : Use json format data to store custom field template (will configure the attributes of the field)
specifically saved json The parsing is as follows: from (List<FieldTemplate> to json, you can use the json tool)
insert image description here

When saving, you can use the entity class to convert to json. The entity class code is as follows

@Data
@AllArgsConstructor
@NoArgsConstructor
public class FieldTemplate {
    
    
    /**
     * 字段key名
     */
    private String field;

    /**
     * 字段显示名
     */
    private String label;

    /**
     * 是否必填
     */
    private boolean required;

    /**
     * 1:自字符串类型     2:boolean类型     3:日期时间类型    4:int类型 5:单选 6:多选
     */
    private Integer type;

    /**
     * 单选或多选选项
     */
    private List<Map<Object,Object>> map;

    /**
     * 用于接受前端编辑自定义字段设置的值
     */
    private Object value;
}

2: Template data table

insert image description here
field_data: Save the specific value of the field in the custom template, the storage format is a json string in the form of map(key:value)

The key is: the field in the custom template, and the value is the set value

insert image description here

When the user modifies the data: To save the data, first modify the data in the original table, and then modify the data in the custom field, and the data in the custom field is stored in the template data table. Because obj_id will be associated with specific data, querying and modifying custom fields are all operations on the template data table.

demo address

User list (custom field test)

Demonstration project project address

Front-end engineering gitee (code cloud) address

Back-end project gitee (code cloud) address

The code used in the demo has been uploaded to the code cloud for your reference. ❤️❤️

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324188509&siteId=291194637