jQuery EasyUI Data Grid and Tree Plugin - Tree
jQuery EasyUI Plugins
Override the default defaults via $.fn.tree.defaults.
The tree displays hierarchical data in a tree structure on web pages. It provides users with expand, collapse, drag-and-drop, edit, and asynchronous loading features.
Depends on
- draggable
- droppable
Usage
The tree is defined in
-
elements. This markup can define leaf nodes and child nodes. Nodes will be
- elements within the ul list. The following demonstrates the elements used to make tree nodes nested within ul elements.
-
Folder
-
Sub Folder 1
- File 11
- File 12
- File 13
- File 2
- File 3
-
Sub Folder 1
- File21
A Tree can also be defined in an empty
-
element, with data loaded using JavaScript.
$('#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 id, which is important for loading remote data.
- text: the node text to display.
- state: the node state, 'open' or 'closed', default is 'open'. When set to 'closed', the node has child nodes that 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#" }] }]Asynchronous Tree
The tree supports a built-in asynchronous loading mode, so users can create an empty tree and then specify a server side that dynamically returns JSON data to asynchronously populate the tree on demand. Below is an example:
The tree is loaded via the URL 'get_data.php'. Child nodes are loaded depending on the parent node's state. When expanding a closed node, if the node has no child nodes loaded, it sends the node's id value to the server via the URL defined above as an HTTP request parameter named 'id' to retrieve the child nodes.
See 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 are displayed directly. When Node 2 is expanded, value(2) is sent to the server to retrieve the child nodes.
In this tutorial, the Creating an Asynchronous Tree section demonstrates how to write server code to return tree data on demand.
property
Name Type Description Default value url string The URL to retrieve remote data. null method string The HTTP method used to retrieve data. post animate boolean Defines whether to show animation when nodes are expanded or collapsed. false checkbox boolean Defines whether to show checkboxes in front of each node. false cascadeCheck boolean Defines whether to enable cascading check. true onlyLeafCheck boolean Defines whether to show checkboxes only in front of leaf nodes. false lines boolean Defines whether to show tree lines. false dnd boolean Defines whether to enable drag and drop. false data array The node data to load. $('#tt').tree({ data: [{ text: 'Item1', state: 'closed', children: [{ text: 'Item11' },{ text: 'Item12' }] },{ text: 'Item2' }] }); | null| onSelect | event | formatter | function(node) | Defines how to render the node text.
Code example:$('#tt').tree({ formatter:function(node){ return node.text; } });| onSelect | event| onSelect | event | loader | function(param,success,error) | Defines how to load data from a remote server. Returning false cancels the action. This function has the following parameters:
param: the parameter object to pass to the remote server.
success(data): the callback function invoked when data is retrieved successfully.
error(): the callback function invoked when data retrieval fails. | json loader| onSelect | event | loadFilter | function(data,parent) | Returns the filtered data to display. The data should be returned in standard tree format. This function has the following parameters:
data: the original data to load.
parent: the DOM object representing the parent node.| onSelect | event |Events
The callback functions of many events 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 an 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 Triggered when the user clicks a node. Code example: $('#tt').tree({ onClick: function(node){ alert(node.text); // alert node text property when clicked } });| onSelect | event | onDblClick | node | Triggered when the user double-clicks a node.| onSelect | event | onBeforeLoad | node, param | Triggered before the request to load data is sent; returning false cancels the load.| onSelect | event | onLoadSuccess | node, data | Triggered when data is loaded successfully.| onSelect | event | onLoadError | arguments | Triggered when data fails to load; the arguments parameter is the same as the 'error' function of jQuery.ajax.| onSelect | event | onBeforeExpand | node | Triggered before a node is expanded; returning false cancels the expansion.| onSelect | event | onExpand | node | Triggered when a node is expanded.| onSelect | event | onBeforeCollapse | node | Triggered before a node is collapsed; returning false cancels the collapse.| onSelect | event | onCollapse | node | Triggered when a node is collapsed.| onSelect | event | onBeforeCheck | node, checked | Triggered before the user clicks a checkbox; returning false cancels the check action. Available since version 1.3.1.| onSelect | event | onCheck | node, checked | Triggered when the user clicks a checkbox.| onSelect | event | onBeforeSelect | node | Triggered before a node is selected; returning false cancels the selection.| onSelect | event | onSelect | node | Triggered when a node is selected.| onSelect | event | onContextMenu | e, node | Triggered when a node is right-clicked. Code example:
// right click node and then display the context menu $('#tt').tree({ onContextMenu: function(e, node){ e.preventDefault(); // select the node $('#tt').tree('select', node.target); // display context menu $('#mm').menu('show', { left: e.pageX, top: e.pageY }); } });| onSelect | event | onBeforeDrag | node | Triggered when dragging a node starts; returning false disables dragging. Available since version 1.3.2.| onSelect | event | onStartDrag | node | Triggered when a node starts being dragged. Available since version 1.3.2.| onSelect | event | onStopDrag | node | Triggered when dragging a node stops. Available since version 1.3.2.| onSelect | event | onDragEnter | target, source | Triggered when a node is dragged into a target node that allows dropping; returning false disables dropping.// the context menu is defined as below:
target: the target node element onto which the node is dropped.
source: the source node being dragged.
Available since version 1.3.2.| onSelect | event | onDragOver | target, source | Triggered when a node is dragged over a target node that allows dropping; returning false disables dropping.
target: the target node element onto which the node is dropped.
source: the source node being dragged.
Available since version 1.3.2.| onSelect | event | onDragLeave | target, source | Triggered when a node is dragged out of a target node that allows dropping.
target: the target node element onto which the node is dropped.
source: the source node being dragged.
Available since version 1.3.2.| onSelect | event | onBeforeDrop | target,source,point | Triggered before a node is dropped; returning false disables dropping.
target: the DOM object of the target node for the drop.
source: the source node.
point: indicates the drop operation; possible values are: 'append', 'top', or 'bottom'.
Available since version 1.3.2.| onSelect | event | onDrop | target,source,point | Triggered when a node is dropped. target: the DOM object of the target node for the drop.
source: the source node.
point: indicates the drop operation; possible values are: 'append', 'top', or 'bottom'.| onSelect | event | onBeforeEdit | node | Triggered before editing a node.| onSelect | event | onAfterEdit | node | Triggered after editing a node.| onSelect | event | onCancelEdit | node | Triggered when the edit action is canceled. |Method
Name Parameters Description options none Returns the tree options. loadData data Loads the tree data. getNode target Gets the specified node object. getData target Gets the specified node data, including its child nodes. reload target Reloads the tree data. getRoot none Gets the root node, returning a node object. getRoots none Gets the root nodes, returning an array of nodes. getParent target Gets the parent node; the target parameter represents the node's DOM object. getChildren target Gets the child nodes; the target parameter represents the node's DOM object. getChecked state Gets the checked nodes. Available state values are: 'checked', 'unchecked', 'indeterminate'. If no state is assigned, the 'checked' nodes are returned. 代码实例:var nodes = $('#tt').tree('getChecked'); // get checked nodes var nodes = $('#tt').tree('getChecked', 'unchecked'); // get unchecked nodes var nodes = $('#tt').tree('getChecked', 'indeterminate'); // get indeterminate nodes var nodes = $('#tt').tree('getChecked', ['checked','indeterminate']); // get checked and indeterminate nodes| onSelect | event | getSelected | none | Get the selected node and return it; if no node is selected, return null.| onSelect | event | isLeaf | target | Determine whether the specified node is a leaf; the target parameter is the node's DOM object.| onSelect | event | find | id | Find the specified node and return the node object. Code example:
// find a node and then select it var node = $('#tt').tree('find', 12); $('#tt').tree('select', node.target);| onSelect | event | select | target | Selects a node. The target parameter indicates the node's DOM object.| onSelect | event | check | target | Sets the specified node as checked.| onSelect | event | uncheck | target | Sets the specified node as unchecked.| onSelect | event | collapse | target | Collapses a node. The target parameter indicates the node's DOM object.| onSelect | event | expand | target | Expands a node. The target parameter indicates the node's DOM object. When a node is closed and has no child nodes, the node's id value (named the 'id' parameter) will be sent to the server to request child node data.| onSelect | event | collapseAll | target | Collapses all nodes.| onSelect | event | expandAll | target | Expands all nodes.| onSelect | event | expandTo | target | Expands a specified node from the root.| onSelect | event | scrollTo | target | Scrolls to the specified node. This method is available since version 1.3.4.| onSelect | event | append | param | Appends some child nodes to a parent node. The param parameter has two properties:
parent: the DOM object of the parent node to append to. If not assigned, the nodes are appended as root nodes.
data: an array of node data.代码实例:// append some nodes to the selected node var selected = $('#tt').tree('getSelected'); $('#tt').tree('append', { parent: selected.target, data: [{ id: 23, text: 'node23' },{ text: 'node24', state: 'closed', children: [{ text: 'node241' },{ text: 'node242' }] }] });| onSelect | event | toggle | target | Toggles the expanded/collapsed state of a node. The target parameter indicates the node's DOM object.| onSelect | event | insert | param | Inserts a node before or after the specified node. The param parameter includes the following properties:
before: the DOM object of the node before which to insert.
after: the DOM object of the node after which to insert.
data: an object with node data.下面of代码演示了如何在选中节点之前插入一个新of节点:var node = $('#tt').tree('getSelected'); if (node){ $('#tt').tree('insert', { before: node.target, data: { id: 21, text: 'node text' } }); }| onSelect | event | remove | target | Removes a node and its child nodes. The target parameter indicates the node's DOM object.| onSelect | event | pop | target | Pops a node and its child nodes. This method is the same as remove, but returns the data of the removed node.| onSelect | event | update | param | Updates the specified node. The 'param' parameter has the following properties:
target (the DOM object of the node to update), id, text, iconCls, checked, etc.代码实例:// update the selected node text var node = $('#tt').tree('getSelected'); if (node){ $('#tt').tree('update', { target: node.target, text: 'new text' }); }| onSelect | event | enableDnd | none | Enable drag and drop.| onSelect | event | disableDnd | none | Disable drag and drop.| onSelect | event | beginEdit | target | Begin editing a node.| onSelect | event | endEdit | target | End editing a node.| onSelect | event | cancelEdit | target | Cancel editing a node. |
jQuery EasyUI Plugins -
Folder