Skip to content

Many-to-One Tree Reference REFERENCE_TREE ​

Opens a tree selector for the user to select a related record. Corresponds to the JPA @ManyToOne relationship. Suitable when the referenced data has a hierarchical structure.

reference-treereference-tree-dialog

Basic Usage ​

java
@ManyToOne
@JoinColumn(name = "tree_id")
@EruptField(
    edit = @Edit(
        title = "Tree Reference",
        type = EditType.REFERENCE_TREE,
        referenceTreeType = @ReferenceTreeType(
            id = "id", label = "name",
            pid = "parent.id" // If pid is omitted, data is displayed as a list
        )
    )
)
private Tree tree;

Referenced tree entity class:

java
@Entity
@Table(name = "t_tree")
@Erupt(name = "Tree", tree = @Tree(pid = "parent.id"))
public class Tree extends BaseModel {

    @EruptField(views = @View(title = "Name"), edit = @Edit(title = "Name"))
    private String name;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    @EruptField(
        edit = @Edit(title = "Parent Node", type = EditType.REFERENCE_TREE,
                     referenceTreeType = @ReferenceTreeType(pid = "parent.id"))
    )
    private Tree parent;

}

Configuration ​

java
public @interface ReferenceTreeType {

    String id() default "id";       // Field used for storage

    String label() default "name";  // Field used for display

    String pid() default "";        // Parent node field; if omitted, data is displayed as a list

    Expr rootPid() default @Expr;   // Root node pid condition

    int expandLevel() default 999;  // Default expansion level

    String dependField() default ""; // Field name in the current table that this field depends on

    String dependColumn() default "id"; // The column in the dependency field used for matching

    boolean leafOnly() default false; // Only leaf nodes can be selected (2.4.0+)

}

Leaf Nodes Only: leafOnly v2.4.0+ ​

With leafOnly = true a node that has children can be expanded but not picked, so the user always lands on the last level. Use it where parents such as departments or categories only group and should never be the owner:

java
@ManyToOne
@JoinColumn(name = "dept_id")
@EruptField(
    views = @View(title = "Department", column = "name"),
    edit = @Edit(title = "Department", type = EditType.REFERENCE_TREE,
                 referenceTreeType = @ReferenceTreeType(pid = "parent.id", leafOnly = true))
)
private Dept dept;

Contributors

The avatar of contributor named as YuePeng YuePeng

Changelog

Released under the Apache-2.0 License.