# Tree Component
The Tree Component is a powerful UI component that integrates functionalities such as selection, dragging, editing, and lazy loading. This section will introduce how to create a tree component and the usage of its key interfaces and events through examples.
The article will involve some APIs and event names of the Tree Component. For related information, please refer to the API Reference as below:
TreeComponent (opens new window) TreeNodeComponent (opens new window) TreeNodeData (opens new window)
# Getting Started
# Create a Tree Component
As the code snippet below shows, you can create a Tree Component by using the <tree>
tag in the template. The Tree Component supports three attributes:
draggable
: Indicates whether the dragging feature is enabled. It is disabled by default. Once enabled, tree nodes can be dragged onto other tree nodes, triggering thedragend
event.editable
: Indicates whether the editing feature is enabled. It is disabled by default. Once enabled, the text on the tree nodes can be edited by double-clicking, and theconfirm-edit
event is triggered upon hitting enter to confirm the update.lazy-mode
: Represents the mode for lazy loading child nodes. The default is"none"
, which indicates that delayed loading is not enabled. When set to"passive"
, child nodes are only created and rendered when they are within the visible area, which can improve performance when there are a large number of child nodes.
<tree
draggable="true"
editable="true"
lazy-mode="passive"
></tree>
# Display Data in the Tree Component
The previous example code simply creates a tree component and does not display any content, as no data is provided. There are two ways to display data in the tree component:
Use the
@setter.data="expression"
directive in the component template to specify the data binding relationship. The result returned by the expression will be used for the tree component's data display.Update the data of the tree component through the
[TreeComponent.setData]
API.
Please note that these two methods cannot be used simultaneously to avoid unexpected results due to synchronization order issues.
Below are two examples of tree components demonstrated separately by directive and setData
, please click run
to execute:
Display data using the
@setter.data
directive:Display data using the
setData
API
# Create an editable tree
To create an editable tree, you need to set the editable
attribute to true
on the <tree>
tag. This will allow the node name to be edited by double-clicking on the tree node text.
For example:
<tree editable="true"></tree>
After enabling the editing feature, when the tree node text is double-clicked, it will trigger an edit
event. You can make the node data editable by updating it in the event listener function.
Once the editing is complete, you can listen to the confirm-edit
event to get the text edited by the user.
Example:
If you want to set a specific node to be uneditable, you can individually specify its data parameter editable
as false
in treeData
.
Example:
Please note that if you set the editable
parameter of the tree component to false
, you will not be able to individually specify whether a node is editable.
# Create a draggable tree
To make the tree component draggable, you need to enable the draggable
attribute on the <tree>
component:
<tree draggable="true"></tree>
In this way, you can drag tree nodes into other nodes. When the dragging ends, a dragend
event will be triggered. Through the event parameters, you can obtain the data of the dragged node, the ID of the target node, and the relationship between nodes after dragging. Typically, at this point, the application needs to update the backend data based on this information.
Example:
If you want to set a specific node to be non-draggable, you can individually specify its data parameter draggable
as false
in treeData
.
Example:
Please note that if the draggable
of the tree component is specified as false
, you will not be able to individually specify whether a node is draggable.
# Selectable tree node
To make the tree nodes selectable, you need to enable the selectable
attribute on the <tree>
component:
<tree selectable="true"></tree>
In this way, when a user clicks on a tree node, the fv__ui-tree-node-selected
CSS class will be added to the node. The default appearance will not change, but your application can customize the appearance after selection. At the same time, the select
event will be triggered.
If you want to specify a node as unselectable, you can individually set its data parameter selectable
to false
in treeData
.
Example:
Please note that if the selectable
of the tree component is specified as false
, you will not be able to individually specify whether a node is selectable.
# The states of the tree node
Tree nodes have the following states:
disabled
: Whether it is disabled or not, default isfalse
. A disabled tree node cannot be edited, selected, or dragged.activated
: Whether the subtree is expanded or not, default isfalse
. If the node is a leaf node, setting this value will have no effect.selected
: Whether it is selected or not, default isfalse
. If the node is unselectable, setting this value will have no effect.editing
: Whether editing is enabled or not, default isfalse
. If the node is uneditable, setting this value will have no effect.
Example:
# Lazy Mode
Lazy mode does not refer to data delay, but rendering delay. Considering that there may be a large number of tree nodes, inserting too many DOM nodes into the browser at once can cause display stuttering. To address this, the tree component provides a lazy mode lazy-mode="passive"
, which can display tree nodes as needed.
Example of using Lazy Mode:
Example without using Lazy Mode:
When running the above two examples and clicking to expand a subtree with over 10,000 nodes, it can be observed that without the use of lazy mode, the browser exhibits noticeable stuttering at the outset.