Easy Tutorial
❮ Ext Edatagrid Plugins Dt Datagrid ❯

jQuery EasyUI DataGrid and Tree Plugin - Tree



The defaults are rewritten by $.fn.tree.defaults.

The tree is displayed in a tree structure on the web page, showing hierarchical data. It provides users with functions such as expand, collapse, drag, edit, and asynchronous loading.

Dependencies

Usage

The tree is defined in the <ul> element. This markup can define leaf nodes and child nodes. The nodes will be <li> elements within the ul list. The following demonstrates the elements that will be used to create tree nodes nested within the 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 ASP.NET web services.

$('#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:

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 built-in asynchronous loading mode, so users can create an empty tree and then specify a server-side that dynamically returns JSON data to populate the tree asynchronously 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 depending on the parent node's state. When expanding a closed node, if the node 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 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 expanding Node 1, its child nodes will be displayed directly. When expanding Node 2, it will send value(2) to the server to retrieve child nodes.

The Creating an Asynchronous Tree in this tutorial demonstrates how to write server code to return tree data on demand.

Properties

Name Type Description Default Value
url string The URL to get remote data. null
method string The http method (method) to retrieve data. post
animate boolean Defines whether to show animation effects when the node is expanded or collapsed. false
checkbox boolean Defines whether to display a checkbox before each node. false
cascadeCheck boolean Defines whether to cascade check. true
onlyLeafCheck boolean Defines whether to display a checkbox only before 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({<br> data: [{<br> text: 'Item1',<br> state: 'closed',<br> children: [{<br> text: 'Item11'<br> },{<br> text: 'Item12'<br> }]<br> },{<br> text: 'Item2'<br> }]<br>}); null
formatter function(node) Defines how to render the node text. <br>Code example: $('#tt').tree({<br> formatter:function(node){<br> return node.text;<br> }<br>});
loader function(param,success,error) Defines how to load data from a remote server. Return false to cancel the action. The function has the following parameters: <br>param: The parameter object to be passed to the remote server. <br>success(data): The callback function to be called when data retrieval is successful. <br>error(): The callback function to be called when data retrieval fails. <br> json loader
loadFilter function(data,parent) Returns the filtered data to be displayed. The returned data should be in standard tree format. The function has the following parameters: <br>data: The original data to be loaded. <br>parent: The DOM object representing the parent node.

Events

Many event callbacks require the 'node' parameter, which includes the following properties:

Name Parameters Description
onClick node Triggered when the user clicks a node. Code example: $('#tt').tree({<br> onClick: function(node){<br> alert(node.text); // alert node text property when clicked<br> }<br>});
onDblClick node Triggered when the user double-clicks a node.
onBeforeLoad node, param Triggered before the request to load data is sent, return false to cancel the load action.
onLoadSuccess node, data Triggered when data is loaded successfully.
onLoadError arguments Triggered when data loading fails, the arguments parameter is the same as jQuery.ajax's 'error' function.
onBeforeExpand node Triggered before the node is expanded, return false to cancel the expand action.
onExpand node Triggered when the node is expanded.
onBeforeCollapse node Triggered before the node is collapsed, return false to cancel the collapse action.
onCollapse node Triggered when the node is collapsed.
onBeforeCheck node, checked Triggered before the user clicks the checkbox, return false to cancel the check action. This event is available since version 1.3.1.
onCheck node, checked Triggered when the user clicks the checkbox.
onBeforeSelect node Triggered before the node is selected, return false to cancel the select action.
onSelect node Triggered when the node is selected.
onContextMenu e, node Triggered when a node is right-clicked. Code example: // right click node and then display the context menu<br>$('#tt').tree({<br>    onContextMenu: function(e, node){<br>        e.preventDefault();<br>        // select the node<br>        $('#tt').tree('select', node.target);<br>        // display context menu<br>        $('#mm').menu('show', {<br>            left: e.pageX,<br>            top: e.pageY<br>        });<br>    }<br>});<br>// the context menu is defined as below:<br><div id="mm" class="easyui-menu" style="width:120px;"><br>    <div onclick="append()" data-options="iconCls:'icon-add'">Append</div><br>    <div onclick="remove()" data-options="iconCls:'icon-remove'">Remove</div><br></div>
onBeforeDrag node Triggered when dragging of a node starts, returning false prohibits dragging. Available since version 1.3.2.
onStartDrag node Triggered when dragging of a node begins. Available since version 1.3.2.
onStopDrag node Triggered when dragging of a node stops. Available since version 1.3.2.
onDragEnter target, source Triggered when a node is dragged into a permissible drop target, returning false prohibits dropping. <br>target: The drop target node element. <br>source: The source node being dragged. <br>Available since version 1.3.2.
onDragOver target, source Triggered when a node is dragged over a permissible drop target, returning false prohibits dropping. <br>target: The drop target node element. <br>source: The source node being dragged. <br>Available since version 1.3.2.
onDragLeave target, source Triggered when a node is dragged out of a permissible drop target. <br>target: The drop target node element. <br>source: The source node being dragged. <br>Available since version 1.3.2.
onBeforeDrop target, source, point Triggered before a node is dropped, returning false prohibits dropping. <br>target: The drop target node as a DOM object. <br>source: The source node. <br>point: Indicates the drop action, possible values are: 'append', 'top', or 'bottom'. <br>Available since version 1.3.2.
onDrop target, source, point Triggered when a node is dropped.

target: The drop target node as a DOM object. <br>source: The source node. <br>point: Indicates the drop action, possible values are: 'append', 'top', or 'bottom'. | | onBeforeEdit | node | Triggered before editing a node. | | onAfterEdit | node | Triggered after editing a node. | | onCancelEdit | node | Triggered when the editing action is canceled. |

Methods

Name Parameters Description
options none Returns the tree options.
loadData data Loads tree data.
getNode target Retrieves the specified node object.
getData target Retrieves the specified node data, including its child nodes.
reload target Reloads tree data.
getRoot none Retrieves the root node, returning the node object.
getRoots none Retrieves the root nodes, returning an array of nodes.
getParent target Retrieves the parent node, where target is the node's DOM object.
getChildren target Retrieves child nodes, where target is the node's DOM object.
getChecked state Retrieves checked nodes. Available states are: 'checked', 'unchecked', 'indeterminate'. If no state is assigned, returns 'checked' nodes. <br>Code example: var nodes = $('#tt').tree('getChecked');    // get checked nodes<br>var nodes = $('#tt').tree('getChecked', 'unchecked');    // get unchecked nodes<br>var nodes = $('#tt').tree('getChecked', 'indeterminate');    // get indeterminate nodes<br>var nodes = $('#tt').tree('getChecked', ['checked','indeterminate']);    // get checked and indeterminate nodes
getSelected none Retrieves the selected node and returns it, or null if no node is selected.
isLeaf target Defines the specified node as a leaf node, where target is the node's DOM object.
find id Finds and returns the specified node object. Code example: // find a node and then select it<br>var node = $('#tt').tree('find', 12);<br>$('#tt').tree('select', node.target);
select target Selects a node, where target is the node's DOM object.
check target Sets the specified node as checked.
uncheck target Sets the specified node as unchecked.
collapse target Collapses a node, where target is the node's DOM object.
expand target Expands a node, where target is the node's DOM object. When the node is closed and has no children, the node's id value (named 'id' parameter) will be sent to the server to request child node data.
collapseAll target Collapses all nodes.
expandAll target Expands all nodes.
expandTo target Expands from the root to a specified node.
scrollTo target Scrolls to the specified node. Available since version 1.3.4.
append param Appends some child nodes to a parent node, where param has two properties: <br>parent: The parent node to append to, if not assigned, appends to the root node. <br>data: Array, node data. <br> <br>Code example: // append some nodes to the selected node<br>var selected = $('#tt').tree('getSelected');<br>$('#tt').tree('append', {<br>    parent: selected.target,<br>    data: [{<br>        id: 23,<br>        text: 'node23'<br>    },{<br>        text: 'node24',<br>        state: 'closed',<br>        children: [{<br>            text: 'node241'<br>        },{<br>            text: 'node242'<br>        }]<br>    }]<br>});
toggle target Toggles the expanded/collapsed state of a node, where target is the node's DOM object.
insert param Inserts a node before or after a specified node, where param includes the following properties: <br>before: The node to insert before. <br>after: The node to insert after. <br>data: Object, node data. <br> <br>The following code demonstrates how to insert a new node before the selected node: var node = $('#tt').tree('getSelected');<br>if (node){<br>    $('#tt').tree('insert', {<br>        before: node.target,<br>        data: {<br>            id: 21,<br>            text: 'node text'<br>        }<br>    });<br>}
remove target Removes a node and its children, where target is the node's DOM object.
pop target Pops a node and its children, this method is the same as remove but returns the removed node data.
update param Updates the specified node, where 'param' has properties such as: <br>target (DOM object, the node to be updated), id, text, iconCls, checked, etc. <br> <br>Code example: // update the selected node text<br>var node = $('#tt').tree('getSelected');<br>if (node){<br>    $('#tt').tree('update', {<br>        target: node.target,<br>        text: 'new text'<br>    });<br>}
enableDnd none Enables drag and drop functionality.
disableDnd none Disables drag and drop functionality.
beginEdit target Begins editing a node.
endEdit target Ends editing a node.
cancelEdit target Cancels editing a node.
❮ Ext Edatagrid Plugins Dt Datagrid ❯