# TreeGrid API documentation

Examples

## Options

Name | Type | Description
--- | --- | ---
source | String | When a string is used, the TreeGrid plugin expects that string to point to a URL resource that will return JSON data. Data should be array of strings, that contains HTML of adding nodes.
Function(String id, Function response(Array items)) | Callback, provides the most flexibility and can be used to connect any data source to TreeGrid. The callback gets `id` argument. This is id of node that data is needed. A `response` callback, which expects a single argument: the items to add to the node. It's important when providing a custom source callback to handle errors during the request. If you don't call `response` then loading state of then node will be stay.
enableMove | Boolean | To enable node moving, set this option to `true`. Defaults to false.
moveDistance | Integer | Tolerance, in pixels, for when moving should start. If specified, moving will not start until after mouse is dragged beyond distance. Defaults to 10.
moveHandle | Selector or Element | Restricts moving start click to the specified element. Defaults to false.

## Methods

option() | Gets an object containing key/value pairs representing the current treegrid options. Example: `var options = $('.tree').treegrid('option');`
option(String optionName) | Gets the value currently associated with the specified `optionName`. Example: `var isEnableMove = $('.tree').treegrid('option', 'enableMove');`
option(String optionName, Object value) | Sets the value of the treegrid option associated with the specified `optionName`. Example: `$('.tree').treegrid('option', 'enableMove', false);`
option(Object options) | Sets one or more options for the treegrid. Example: `$('.tree').treegrid('option', {enableMove: false});`
getId() | Get id of the node. Example: `$('.treegrid-1').treegrid('getId');`
getDepth() | Get depth of the node. Example: `var depth = $('.treegrid-1').treegrid('getDepth');`
toggle() | Toggles state of the node. Example: `$('.treegrid-1').treegrid('toggle');`
expand() | Expand node. Example: `$('.treegrid-1').treegrid('expand');`
collapse() | Collapse node. Example: `$('.treegrid-1').treegrid('collapse');`
add(Array items) | Add nodes to parent or as root. If executes from table, nodes adds as roots. If executes from row, nodes adds as children. Every item of array should be HTML of adding nodes. Example: `$('.tree').treegrid('add', ['<tr><td>New root</td></tr>']);`
remove() | Removs node with all descendants. Example: `$('.treegrid-1, .treegrid-2').treegrid('remove');`
move(Element target, Integer position) | Moves node to `target`. To specify where node should be move set `position` param (0-before, 1-inside, 1-after). Example: `$('.treegrid-1').treegrid('move', $('.treegrid-2'), 1);`
getRoots() | Get all roots of TreeGrid. Returns jQuery contains roots. Example: `$roots = $('.tree').treegrid('getRoots');`
getChildNodes() | Get direct children of node. Returns jQuery contains children. Example: `$items = $('.treegrid-1').treegrid('getChildNodes');`
getBranch() | Get all descendants of node. Result contains node that executes method. Returns jQuery contains node with descendants. Example: `$('.treegrid-1').treegrid('getBranch').css('color', 'red');`
getParent() | Get parent of the node. Example: `$parent = $('.treegrid-1').treegrid('getParent');`
isCollapsed() | Get whether node is collapsed. Example: `if ($tr.treegrid('isCollapsed')) { ... }`
isExpanded() | Get whether node is expended. Example: `b = $tr.treegrid('isExpanded');`

## Events

onExpand() | Calling when node expands. Return `false` if you dont want the node been expanded.
onCollapse() | Calling when node collapses. Return `false` if you dont want the node been collapsed.
onAdd(Array items) | Calling when nodes was added. Returns jQuery container that contains all added nodes.
onMoveStart(Element item, Element helper) | This event is triggered when node moving starts. `item` is jQuery of original element. `helper` is jQuery of clone that uses to moving node.
onMoveStop(Element item) | This event is triggered when node moving ends. `item` is jQuery of original element.
onMoveOver(Element item, Element helper, Element target, Integer position) | This event is triggered when node moving over another node that support droping. `item` is jQuery of original element. `helper` is jQuery of clone that uses to moving node. `target` is jQuery of target element. `position` specify moving position (0-before, 1-inside, 1-after). If you dont want target supporting dropping, return `false`.
onMoveOut(Element item, Element helper, Element target) | This event is triggered when node outs another node that support droping. `item` is jQuery of original element. `helper` is jQuery of clone that uses to moving node. `target` is jQuery of target element.
onMove(Element item, Element target, Integer position) | This event is triggered when node drops to another node. `item` is jQuery of original element. `target` is jQuery of target element. `position` specify moving position (0-before, 1-inside, 1-after). If you want to prevent moving, return `false`.