jQuery EasyUI DataGrid & Tree Plugin β Tree
Override the default defaults via $.fn.tree.defaults.
The tree displays hierarchical data in a tree structure on a webpage. It provides users with expand, collapse, drag-and-drop, edit, and asynchronous loading functionalities.
Dependencies
- draggable
- droppable
Usage
The tree is defined within a <ul> element. This markup can define leaf nodes and child nodes. Nodes will be <li> elements within the ul list. The following demonstrates the elements used to create tree nodes nested within a <ul> element.
<ul id="tt" class="easyui-tree">
<li>
<span>Folder</span>
<ul>
<li>
<span>Sub Folder 1</span>
<ul>
<li>
<span><a href="#">File 11</a></span>
</li>
<li>
<span>File 12</span>
</li>
<li>
<span>File 13</span>
</li>
</ul>
</li>
<li>
<span>File 2</span>
</li>
<li>
<span>File 3</span>
</li>
</ul>
</li>
<li>
<span>File21</span>
</li>
</ul>
The tree can also be defined in an empty <ul> element, and data can be loaded using JavaScript.
<ul id="tt"></ul>
$('#tt').tree({
url:'tree_data.json'
});
Use loadFilter to process JSON data from an ASP.NET web service.
$('#tt').tree({
url: ...,
loadFilter: function(data){
if (data.d){
return data.d;
} else {
return data;
}
}
});
Tree Data Format
Each node can include the following properties:
id: The node's id, which is important for loading remote data.text: The text to display for the node.state: The node state, 'open' or 'closed', default is 'open'. When set to 'closed', the node has child nodes and they will be loaded from a remote site.checked: Indicates whether the node is checked.attributes: Custom attributes added to a node.children: An array of nodes defining some child nodes.
Example:
[{
"id":1,
"text":"Folder1",
"iconCls":"icon-save",
"children":[{
"text":"File1",
"checked":true
},{
"text":"Books",
"state":"open",
"attributes":{
"url":"/demo/book/abc",
"price":100
},
"children":[{
"text":"PhotoShop",
"checked":true
},{
"id": 8,
"text":"Sub Bookds",
"state":"closed"
}]
}]
},{
"text":"Languages",
"state":"closed",
"children":[{
"text":"Java"
},{
"text":"C#"
}]
}]
Async Tree
The tree supports a built-in asynchronous loading mode, allowing users to create an empty tree and then specify a server-side script that dynamically returns JSON data to asynchronously populate the tree as needed. Here is an example:
<ul class="easyui-tree" data-options="url:'get_data.php'"></ul>
The tree is loaded via the URL 'get_data.php'. Child nodes are loaded based on the parent node's state. When a closed node is expanded, if it has no child nodes loaded, it will send the node's id value to the server as an HTTP request parameter named 'id' via the URL defined above to retrieve the child nodes.
Look at the data returned from the server:
[{
"id": 1,
"text": "Node 1",
"state": "closed",
"children": [{
"id": 11,
"text": "Node 11"
},{
"id": 12,
"text": "Node 12"
}]
},{
"id": 2,
"text": "Node 2",
"state": "closed"
}]
Node 1 and Node 2 are closed. When Node 1 is expanded, its child nodes will be displayed directly. When Node 2 is expanded, a value (2) will be sent to the server to retrieve the child nodes.
The Create Async Tree tutorial in this series demonstrates how to write server code to return tree data as needed.
Properties
| Name | Type | Description | Default |
|---|---|---|---|
| url | string | A URL to retrieve remote data. | null |
| method | string | The HTTP method to retrieve data. | post |
| animate | boolean | Defines if to show animation effect when node expand or collapse. | false |
| checkbox | boolean | Defines if to show a checkbox before each node. | false |
| cascadeCheck | boolean | Defines if to perform cascading check. | true |
| onlyLeafCheck | boolean | Defines if to show checkbox only before leaf nodes. | false |
| lines | boolean | Defines if to show tree lines. | false |
| dnd | boolean | Defines if to enable drag and drop. | false |
| data | array | The node data to be loaded.
|
null |
| formatter | function(node) | Defines how to render the node text. Code example:
|
|
| loader | function(param,success,error) | Defines how to load data from remote server. Return false to cancel this action. This function has the following parameters:
|
json loader |
| loadFilter | function(data,parent) | Return the filtered data to display. The returned data should be in the standard tree format. This function has the following parameters:
|
Events
Many event callback functions require a 'node' parameter, which includes the following properties:
id: The identifier value bound to the node.text: The text to display.iconCls: The CSS class used to display the icon.checked: Whether the node is checked.state: The node state, 'open' or 'closed'.attributes: Custom attributes bound to the node.target: The target DOM object.
| Name | Parameters | Description |
|---|---|---|
| onClick | node | Fires when a user clicks a node. Code example:
|
| onDblClick | node | Fires when a user double-clicks a node. |
| onBeforeLoad | node, param | Fires before the data loading request is made. Return false to cancel the loading action. |
| onLoadSuccess | node, data | Fires when the data is loaded successfully. |
| onLoadError | arguments | Fires when the data loading fails. The arguments parameter is the same as the 'error' function of jQuery.ajax. |
| onBeforeExpand | node | Fires before a node is expanded. Return false to cancel the expand action. |
| onExpand | node | Fires when a node is expanded. |
| onBeforeCollapse | node | Fires before a node is collapsed. Return false to cancel the collapse action. |
| onCollapse | node | Fires when a node is collapsed. |
| onBeforeCheck | node, checked | Fires before a user clicks a checkbox. Return false to cancel the checking action. This event is available since version 1.3.1. |
| onCheck | node, checked | Fires when a user clicks a checkbox. |
| onBeforeSelect | node | Fires before a node is selected. Return false to cancel the selection action. |
| onSelect | node | Fires when a node is selected. |
| onContextMenu | e, node | Fires when a node is right-clicked. Code example:
|
| onBeforeDrag | node | Fires when a node drag is started. Return false to cancel the drag. This event is available since version 1.3.2. |
| onStartDrag | node | Fires when a node drag is started. This event is available since version 1.3.2. |
| onStopDrag | node | Fires when a node drag is stopped. This event is available since version 1.3.2. |
| onDragEnter | target, source | Fires when a node is dragged into a valid drop target node. Return false to cancel the drop.
|
| onDragOver | target, source | Fires when a node is dragged over a valid drop target node. Return false to cancel the drop.
|
| onDragLeave | target, source | Fires when a node is dragged out of a valid drop target node.
|
| onBeforeDrop | target,source,point | Fires before a node is dropped. Return false to cancel the drop.
|
| onDrop | target,source,point | Fires when a node is dropped.
|
| onBeforeEdit | node | Fires before a node is edited. |
| onAfterEdit | node | Fires after a node is edited. |
| onCancelEdit | node | Fires when the edit action is cancelled. |
Methods
| Name | Parameters | Description |
|---|---|---|
| options | none | Return the tree options. |
| loadData | data | Load the tree data. |
| getNode | target | Get the specified node object. |
| getData | target | Get the specified node data, including its children. |
| reload | target | Reload the tree data. |
| getRoot | none | Get the root node, return the node object. |
| getRoots | none | Get the root nodes, return the node array. |
| getParent | target | Get the parent node, the target parameter represents the node's DOM object. |
| getChildren | target | Get the child nodes, the target parameter represents the node's DOM object. |
| getChecked | state | Get the checked nodes. The available state values are: 'checked', 'unchecked', 'indeterminate'. If the state is not assigned, return 'checked' nodes.
|
| getSelected | none | Get the selected node and return it, if no node is selected, return null. |
| isLeaf | target | Determine if the specified node is a leaf node, the target parameter represents the node's DOM object. |
| find | id | Find the specified node and return the node object. Code example:
|
| select | target | Select a node, the target parameter represents the node's DOM object. |
| check | target | Set the specified node to checked. |
| uncheck | target | Set the specified node to unchecked. |
| collapse | target | Collapse a node, the target parameter represents the node's DOM object. |
| expand | target | Expand a node, the target parameter represents the node's DOM object. When the node is closed and has no child nodes, the node's id value (named 'id' parameter) will be sent to the server to request child node data. |
| collapseAll | target | Collapse all nodes. |
| expandAll | target | Expand all nodes. |
| expandTo | target | Expand a specified node from the root. |
| scrollTo | target | Scroll to the specified node. This method is available since version 1.3.4. |
| append | param | Append some child nodes to a parent node, the param parameter has two properties:
|
| toggle | target | Toggle the node's expanded/collapsed state, the target parameter represents the node's DOM object. |
| insert | param | Insert a node before or after the specified node, the param parameter includes the following properties:
|
| remove | target | Remove a node and its children, the target parameter represents the node's DOM object. |
| pop | target | Pop a node and its children, this method is the same as remove, but returns the removed node data. |
| update | param | Update the specified node, the 'param' parameter has the following properties: target (the DOM object of the node to be updated), id, text, iconCls, checked, etc.
|
| enableDnd | none | Enable drag and drop functionality. |
| disableDnd | none | Disable drag and drop functionality. |
| beginEdit | target | Begin editing a node. |
| endEdit | target | End editing a node. |
| cancelEdit | target | Cancel editing a node. |
YouTip